lookfirst / mui-rff

MUI 5 / Material UI + React Final Form
https://lookfirst.github.io/mui-rff/
MIT License
490 stars 93 forks source link

Allow setting error as prop. #693

Open aFarkas opened 2 years ago

aFarkas commented 2 years ago

Problem. Currently the error is tightly bound to the final form wrapper. While this is what you want in 90% of all cases. There are situation where developers need to set the error from outside. While it is possible to handle the when using the showError prop. It is not possible to set the fieldProp to the error state.

Allow setting the fieldProp to an error state

the textfield code could look like this:

  const {
    input: { name, value, type, onChange, onBlur, onFocus, ...restInput },
    meta: propMeta,
    required,
    fullWidth = true,
    helperText,
    error: propError,
    showError = showErrorOnChange,
    ...rest
  } = props;
  const { error, submitError } = meta;
  const isError = showError({ meta, propError });

  return (
    <MuiTextField
      fullWidth={fullWidth}
      helperText={isError ? propError || error || submitError : helperText}
      error={isError}
//....

While the show error methods could check wether propError == null then do the old behavior otherwise override submitError and meta.error.

lookfirst commented 2 years ago

@aFarkas Sure. Submit a PR to cover your case.