简体   繁体   English

在 Label 旁边创建 MUI TextField

[英]Create MUI TextField Next to Label

I've searched all over, but I can't make it work.我已经搜索了所有内容,但我无法使其工作。 I want to create a TextField Element Next to a label我想在 label 旁边创建一个 TextField 元素
(and not anywhere inside or touching the border of a TextField) like this one: Desired Result (而不是在 TextField 内部的任何地方或接触 TextField 的边界),例如: Desired Result

Labe <____> TextField Such a scenario is not covered in the documentation . Labe <____> TextField文档中没有涉及这种情况。 The best i could get was to use the startAdornment in the TextField Component.我能得到的最好的结果是在 TextField 组件中使用 startAdornment。

InputProps={{
        startAdornment: (
          <InputAdornment position="start">Name</InputAdornment>
        )
      }}

Also I tried, to use the FormControlLabel component and pass inside the TextField as a Control , although TextField can't be anymore fullWidth我也尝试使用FormControlLabel组件并在 TextField 内部作为Control传递,尽管 TextField 不能再是fullWidth

  <FormControlLabel
    control={<TextField
      variant="standard"
      margin="normal"
      id="tags"
      helperText="service."
      fullWidth
    />}
    label="Start"
    labelPlacement="start"
  />

But it doesn't seem to work properly and I'm not even sure if I should use a FormControlLabel with a TextField.但它似乎无法正常工作,我什至不确定是否应该使用带有 TextField 的 FormControlLabel。 Thank you in advance.先感谢您。 This seems as something very basic but I can't get it to work and I find no other relative problems posted.这似乎是非常基本的东西,但我无法让它工作,我发现没有发布其他相关问题。

you can apply a flex style to your Form or wrap your Textfield in a div with an InputLabel in it like and add style to match the desire result for example like this:您可以将 flex 样式应用于您的表单或将您的Textfield包装在一个带有InputLabel的 div 中,并添加样式以匹配所需的结果,例如:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import InputLabel from "@material-ui/core/InputLabel";
import { FormHelperText } from "@material-ui/core";
const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex"
  },
  TextFieldDiv: {
    marginLeft: "15px"
  },
  labelName: {
    paddingTop: "10px"
  },
  optionalTag: {
    fontSize: "13px"
  },
  labelTag: {
    textAlign: "right"
  }
}));

export default function BasicTextFields() {
  const classes = useStyles();

  return (
    <form className={classes.root} noValidate autoComplete="off">
      <InputLabel className={classes.labelName}>
        <div className={classes.labelTag}>Name</div>{" "}
        <div className={classes.optionalTag}>(optional)</div>
      </InputLabel>
      <div className={classes.TextFieldDiv}>
        <TextField id="standard-basic" />
        <FormHelperText>The service name</FormHelperText>
      </div>
    </form>
  );
}

here a sandBox Example这是一个沙盒示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM