Change arrow function into list of components

Viewed 212

I wanted to make and outlined div with label and found this answer, which has exactly what I was looking for. However there is a problem when I try to use some inputs inside OutlinedDiv. After typing any character it loses focus. I found that the problem is an arrow function used in OutlinedDiv:

inputComponent: ({ className }) => (
      <div key="divKey" className={className}>{children}</div>)

and if I use a simple div instead of OutlinedDiv everything works as expected. So can I rewrite OutlinedDiv in a way that it will work with inputs? I don't even have a feeling what are my options here.

Edit Custom Outlined Component

index.js

import React from "react";
import ReactDOM from "react-dom";

import OutlinedDiv from "./OutlinedDiv";
import TextField from "@material-ui/core/TextField";

function App() {
  const [state, setState] = React.useState({ inputValue: "", inputValue2: "" });

  function handleChange(event) {
    setState({ ...state, [event.target.name]: event.target.value });
  }

  return (
    <div className="App">
      <OutlinedDiv key="outlinedDivKey" label="OutlinedDivTest">
        <div>
          <TextField
            id="inputValue"
            key="inputValueKey"
            name="inputValue"
            label="Some input"
            inputProps={{ style: { textAlign: "right" } }}
            value={state.inputValue}
            onChange={event => handleChange(event)}
          />
        </div>
        <div>
          <TextField
            id="inputValue2"
            key="inputValueKey2"
            name="inputValue2"
            label="Some input2"
            inputProps={{ style: { textAlign: "right" } }}
            value={state.inputValue2}
            onChange={event => handleChange(event)}
          />
        </div>
      </OutlinedDiv>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

outlinedDiv.js

import React from "react";

import TextField from "@material-ui/core/TextField";

const OutlinedDiv = ({ children, label }) => {
  return (
    <TextField
      variant="outlined"
      label={label}
      multiline
      InputLabelProps={{ shrink: true }}
      InputProps={{
        inputComponent: ({ className }) => (
          <div key="divKey" className={className}>
            {children}
          </div>
        )
      }}
    />
  );
};
export default OutlinedDiv;
1 Answers

I've updated my previous answer to use the following for the OutlinedDiv implementation:

import React from "react";

import TextField from "@material-ui/core/TextField";

const InputComponent = ({ inputRef, ...other }) => <div {...other} />;
const OutlinedDiv = ({ children, label }) => {
  return (
    <TextField
      variant="outlined"
      label={label}
      multiline
      InputLabelProps={{ shrink: true }}
      InputProps={{
        inputComponent: InputComponent
      }}
      inputProps={{ children: children }}
    />
  );
};
export default OutlinedDiv;

This avoids re-mounting of the input component when re-rendering occurs.

Here's a modified version of your sandbox:

https://codesandbox.io/s/custom-outlined-component-sv2bi

Related