Skip to content

[bug] An input initializes without visible value when autofocus is enabled  #3

@mirus-ua

Description

@mirus-ua

Hello. When I tried to use autoFocus, it behaved oddly, both in the case of the mui component and regular HTML's input. When the input is rendered, it looks blank (the first screenshot) even if it has a value, like in the code snippet, and the value appears only after the user's action, like click or key press (the second screenshot after arrow up press).

As far as I understood, this behavior is related to the philosophy of the library, but is it possible to fix it, or do I need to find a workaround?

The base component snippet

import TextField from '@mui/material/TextField';

const NumberField = (props: NumberFieldProps) => {
  const formattedInput = useFormattedInput(
    intlNumberFormatter({
      locales: props.locale,
      value: props.value,
      onChange: props.onChange,
      liveUpdate: true,
      ...props,
    }),
  );

  const { ref, ...rest } = formattedInput.props;
  return <TextField {...restProps} InputProps={{ ...rest, inputRef: ref }} />;
}

The usage of the base component

const Usage = () => {
  const [value, setValue] = useState<number | null>(12345.67);
  return (
      <NumberField
        autoFocus
        label="A number field"
        value={value}
        onChange={setValue}
      />
  )
}

empty when autoFocused
Знімок екрана 2023-11-10 о 09 11 22

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions