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

TextField: honor error from props #64

Closed

Conversation

LoicMahieu
Copy link

No description provided.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 91.011% when pulling 59494e2 on LoicMahieu:feature/textfield-error into d26e900 on stackworx:master.

@cliedeman
Copy link
Collaborator

Hi Thanks for the PR. can you explain to me a little bit more about your use case? I have documented it properly yet I intentionally exclude some props Omit<MuiTextFieldProps, 'error' | 'name' | 'onChange' | 'value'> {} because I want formik to be the only way to configure some fields so there is no confusion.

Is there any reason you can use setFieldError(name, error)

@LoicMahieu
Copy link
Author

Hi @cliedeman
Sorry indeed, it requires a little bit more explanation ;)

The idea is to allow to force the error UI when an error occured outside of the form validation. My example is a graphql query that could be in error:

export const PetBreedSelector = (props: any) => {
  const { data, error, loading } = useQuery<GetPetBreedsType>(GetPetBreeds, {
    variables: {
      type: "DOG",
    },
  });
  const animalBreeds = (data && data.animalBreeds) || []

  const errorProps = {
    error: !!error,
    helperText: error && error.message,
  };

  return (
    <FormikTextField
      {...errorProps}
      {...props}
      select
      InputProps={{ ...(loading && muiTextFieldLoading) }}
    >
      {animalBreeds.map(breed => (
        <MenuItem key={breed.id} value={breed.id}>
          {breed.name}
        </MenuItem>
      ))}
    </FormikTextField>
  );
};

Use setFielderror could be an option but more complicated since it will requires to useEffect or something like that.

@cliedeman cliedeman force-pushed the master branch 2 times, most recently from ce76884 to b49624c Compare January 20, 2020 14:39
@cliedeman
Copy link
Collaborator

cliedeman commented Jan 20, 2020

After some thought I have decided to make this standard.

Please see here for my reasoning

As a workaround you can always use form.setError(fieldName, error);

@cliedeman cliedeman closed this Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants