'How to update helper text of two interdependent inputs?

Let's say I have a date range and I want to validate that the start date is smaller than the end date and vice versa.

const schema = yup.object().shape({
  startDate: yup
    .date()
    .when('endDate', (endDate: Date, schema) => schema.max(endDate)),
  endDate: yup
    .date()
    .when('startDate', (startDate: Date, schema) => schema.min(startDate)),
}, 
[['startDate', 'endDate']]);

Now I'm using react-hook-form with an inputs of type date

const { control } = useForm({
  mode: 'onBlur',
  resolver: yupResolver(schema),
});
<MaterialTextField
  inputRef={ref}
  error={isTouched && invalid}
  onBlur={onBlur}
  helperText={error?.message}
  type="date"
/>

With this example, it's working fine, but when a helper text is displayed, if one of the fields is changed and the values become valid, I would like both fields helper text to be removed, except the only one disappearing is the touched one.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source