0

This is my custom textfield component CustomTextField.tsx

import React from "react";
import { TextField, TextFieldProps } from "@mui/material";
import { styled } from "@mui/material/styles";

type ICustomTextFieldProps = {
  theme: Theme;
} & TextFieldProps;
export default function CustomTextField({
  theme,
  ...props
}: ICustomTextFieldProps) {
  const CustomTextField = styled(TextField)({
    "& .MuiOutlinedInput-root": {
      
      "&.Mui-focused fieldset": {
        borderColor: "white",
      },
    },
    "& .MuiInputLabel-root": {
      color: darkValue("rgba(255, 255, 255, 0.7)", theme),
    },
    
  }) as typeof TextField;
  return <CustomTextField {...props} />;
}

When I use it with react hook form the field loses focus after each letter

My form file:

Import CustomTextField from './CustomTextField'

    <Controller
                        {...register(field.name, { required: true })}
              name={"somename"}
              control={control}
              render={({ field: { onChange, value } }) => (
                
                  <CustomTextField
                    // theme={theme}
                    onChange={onChange}
                    value={value}
                    label={"somelabel"}
                  />
                  
              )}
            />

Although if I wrote the custom textfield in the same form file .. it works properly

  • Does this answer your question? [In React ES6, why does the input field lose focus after typing a character?](https://stackoverflow.com/questions/42573017/in-react-es6-why-does-the-input-field-lose-focus-after-typing-a-character) – indyteo Feb 26 '23 at 17:49

0 Answers0