I've been using the "@material-ui/core": "4.11.2" package with "react": "16.8.6". In the TextField component when the select prop is true, I can't get the name property of the TextField from the event object in handleFocus function.
<TextField
        id="standard-select-currency"
        name="mySelectBox"
        select
        label="Select"
        value={currency}
        onChange={handleChange}
        onFocus={handleFocus}
        helperText="Please select your currency"
      >
        {currencies.map((option) => (
          <MenuItem key={option.value} value={option.value}>
            {option.label}
          </MenuItem>
        ))}
</TextField>
Here is handleFocus and handleChange functions.
  const handleChange = (event) => {
    console.log(
      "event.target ==>",
      event.target,
      "event.target.name ==>", // mySelectBox
      event.target.name
    );
  };
  const handleFocus = (event) => {
    console.log(
      "event.target ==>",
      event.target,
      "event.target.name ==>", // undefined
      event.target.name
    );
  };
I create a complete example in this sandbox.
The Texfield does not directly pass the onFocus to the input. From MUI,
The ref is forwarded to the root element.
Any other props supplied will be provided to the root element (FormControl).
The onFocus is passed to the FormControl element, which is a div, not an input, and doesn't have a name. You can use the NativeSelect or pass the onFocus property to the input props, which didn't play out very well when I tried in the sandbox you provided.
The quickest solution for this is to pass a ref to the input, and on focus, get the name from the reference. This is what your code will look like then.
export default function MultilineTextFields() {
  const [currency, setCurrency] = React.useState("EUR");
  const inputRef = React.useRef()
  const handleChange = (event) => {
    setCurrency(event.target.value);
    console.log(
      "event.target ==>",
      event.target,
      "event.target.name ==>",
      event.target.name
    );
  };
  const handleFocus = (event) => {
    console.log(
      "event.target ==>",
      inputRef,
      "event.target.name ==>",
      inputRef.current.node.name
    );
  };
  return (
    <form noValidate autoComplete="off">
      <TextField
      inputRef= {inputRef}
        id="standard-select-currency"
        name="mySelectBox"
        select
        label="Select"
        value={currency}
        onChange={handleChange}
        onFocus={handleFocus}
        helperText="Please select your currency"
      >
        {currencies.map((option) => (
          <MenuItem key={option.value} value={option.value}>
            {option.label}
          </MenuItem>
        ))}
      </TextField>
    </form>
  );
}
Here is the sandbox I used https://codesandbox.io/s/material-demo-forked-zjlz3?file=/demo.js
You can pass a SelectProps to the TextField and use <option> tag instead of <MenuItem> component as I am saying below:
<TextField
        id="standard-select-currency"
        name="mySelectBox"
        select
        label="Select"
        value={currency}
        onChange={handleChange}
        onFocus={handleFocus}
        helperText="Please select your currency"
        SelectProps={{ native: true }} // ************
      >
        {currencies.map((option) => (
          <option key={option.value} value={option.value}>
            {option.label}
          </option>
        ))}
</TextField>
I modified your sandbox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With