reactjs 我一键入,Mui文本输入就失去焦点

ryoqjall  于 2023-02-12  发布在  React
关注(0)|答案(1)|浏览(148)

我在网上看到了很多解决这个问题的方法,但是我不知道这些方法如何适用于我的情况。表单看起来像这样(我删除了一些不相关的内容,包括Submit按钮,以使它简短):

interface FormState {
  firstName: string;
  lastName: string;
}

const EnterDetails = () => {
  const [formState, setFormState] = React.useState<FormState>({
    firstName: '',
    lastName: '',
  });

  const onChangeText = (fieldName: string, newValue: any) => {
    const newFormState = { ...formState };
    newFormState[fieldName] = newValue.target.value;
    setFormState(newFormState);
  };

  return (
      <Box>
        <FormRow>
          <My_TextField
            label="First name"
            value={formState.firstName}
            onChange={(v) => onChangeText('firstName', v)}
          />
          <My_TextField
            label="Surname"
            value={formState.lastName}
            onChange={(v) => onChangeText('lastName', v)}
          />
        </FormRow>
      </Box>
  );
};

export default EnterDetails;

FormRowMy_TextField定义如下(styledBoxTypography等从@mui/material导入):

const StyledRow = styled(Box)(() => ({
    display: 'flex',
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'stretch',
    width: '100%',
}));

const FormRow = ({ children }: { children: any }) => {

  return <StyledRow>{children}</StyledRow>;
};

export default FormRow;

以及

const BootstrapLabel = styled(InputLabel)(({ theme }) => ({
  color: theme.palette.mainText.main,
}));

const BootstrapInput = styled(InputBase)(({ theme }) => ({
  color: theme.palette.altText.main,
}));

const My_TextField= ({
  label,
  value,
  onChange,
}: IProps) => {
  return (
    <Box>
      <BootstrapLabel shrink={false}>{label}</BootstrapLabel>
      <FormControl>
        <BootstrapInput
          value={value}
          onChange={onChange}
        />
      </FormControl>
    </Box>
  );
};

export default My_TextField;

表单看起来很好,但是当我试图在其中一个字段中输入文本时,它一次只会接受一个字符--每输入一个字符后,它就会失去焦点,所以我必须单击回到输入字段才能键入下一个字符。
在我在线看到的解决方案中,人们提到当一个组件被定义在另一个组件中时会出现这个问题,但我看不到我在哪里做过,除非我没有正确理解它。

b1zrtrql

b1zrtrql1#

发生这种情况是因为每次formState更新EnterDetails重新渲染并返回一个新的<FormRow>,因此它失去了焦点,您需要做的是只进行<FormRow>渲染,并将formState放入其中:

const StyledRow = styled(Box)(() => ({
  display: "flex",
  flexDirection: "row",
  flexWrap: "wrap",
  justifyContent: "stretch",
  width: "100%",
}));

const FormRow = () => {
  const [formState, setFormState] =
    React.useState <
    FormState >
    {
      firstName: "",
      lastName: "",
    };

  const onChangeText = (fieldName: string, newValue: any) => {
    const newFormState = { ...formState };
    newFormState[fieldName] = newValue.target.value;
    setFormState(newFormState);
  };
  return (
    <StyledRow>
      <My_TextField
        label="First name"
        value={formState.firstName}
        onChange={(v) => onChangeText("firstName", v)}
      />
      <My_TextField
        label="Surname"
        value={formState.lastName}
        onChange={(v) => onChangeText("lastName", v)}
      />
    </StyledRow>
  );
};

export default FormRow;

和表单EnterDetails jsx:

return (
  <Box>
    //...
    <FormRow/>
    //...
  </Box>
);

相关问题