Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastField not updating Checkbox value on toggle #2327

Closed
simkessy opened this issue Feb 28, 2020 · 1 comment
Closed

FastField not updating Checkbox value on toggle #2327

simkessy opened this issue Feb 28, 2020 · 1 comment
Labels

Comments

@simkessy
Copy link

simkessy commented Feb 28, 2020

I'm trying to control if my points input is enabled or disabled. When I click on checked, it disabled as expected. When I uncheck it remains disabled. I first tried setting disabled={isChecked} then I created it's own state I'd toggle using useEffect neither work. Once it's disabled, it won't toggle back to enabled.

  const [isChecked, setIsChecked] = useState(false);

  const handleChecked = event => {
    handleChange(event);
    setIsChecked(checked => !checked);
    setFieldValue(`questionData.${idx}.answers.${answer}.point`, 0);
    setFieldTouched(event.target.name);
  };



    <FormControlLabel
        control={
          <Checkbox
            name={`field1`}
            onChange={handleChecked}
            value={isChecked}
          />
        }
        label="Disable points"
        style={{ marginTop: 15, marginLeft: 8 }}
    />
    <FastField
      as={InputField}
      name={`field2`}
      label="Points"
      type="number"
      inputProps={{ min: '0', max: '100', step: '1' }}
      disabled={isChecked}
      handleError={handleError}
    />
export const InputField = memo(props => {
  const { handleError = () => {}, name, ...attr } = props;

  const field = name.split('.').pop();
  return (
    <TextField
      size="small"
      fullWidth
      color="secondary"
      error={!!handleError(field)}
      helperText={handleError(field)}
      name={name}
      {...attr}
    />
  );
});

I was able to solve this by changing from FastField to Field. I'm not entirely sure why it works now.

@stale stale bot added the stale label Apr 29, 2020
@maddhruv
Copy link
Collaborator

maddhruv commented Nov 9, 2020

Try the latest formik (>= 2.2.3), fixed with #2874

@maddhruv maddhruv closed this as completed Nov 9, 2020
@jaredpalmer jaredpalmer mentioned this issue Nov 10, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants