Skip to content

Commit

Permalink
fix(Datepicker): revalidate on validators prop change (#10293)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora authored Apr 29, 2024
1 parent 58139cd commit fcf105e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/react-core/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,24 @@ const DatePickerBase = (
setValueDate(dateParse(valueProp));
}, [valueProp]);

Check warning on line 145 in packages/react-core/src/components/DatePicker/DatePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook React.useEffect has a missing dependency: 'dateParse'. Either include it or remove the dependency array. If 'dateParse' changes too often, find the parent component that defines it and wrap that definition in useCallback

React.useEffect(() => {
if (isValidDate(valueDate)) {
applyValidators(valueDate);
}
}, [validators]);

Check warning on line 151 in packages/react-core/src/components/DatePicker/DatePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook React.useEffect has missing dependencies: 'applyValidators' and 'valueDate'. Either include them or remove the dependency array

React.useEffect(() => {
setPristine(!value);
const newValueDate = dateParse(value);
if (errorText && isValidDate(newValueDate)) {
setError(newValueDate);
applyValidators(newValueDate);
}
if (value === '' && !pristine && !textInputFocused) {
dateIsRequired ? setErrorText(emptyDateText) : setErrorText('');
}
}, [value]);

Check warning on line 162 in packages/react-core/src/components/DatePicker/DatePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook React.useEffect has missing dependencies: 'applyValidators', 'dateIsRequired', 'dateParse', 'emptyDateText', 'errorText', 'pristine', and 'textInputFocused'. Either include them or remove the dependency array. If 'dateParse' changes too often, find the parent component that defines it and wrap that definition in useCallback

const setError = (date: Date) => {
const applyValidators = (date: Date) => {
setErrorText(validators.map((validator) => validator(date)).join('\n') || '');
};

Expand All @@ -179,7 +185,7 @@ const DatePickerBase = (
onBlur(event, value, onBlurDateArg);

if (dateIsValid) {
setError(newValueDate);
applyValidators(newValueDate);
}

if (!dateIsValid && !pristine) {
Expand All @@ -195,15 +201,15 @@ const DatePickerBase = (
const newValue = dateFormat(newValueDate);
setValue(newValue);
setValueDate(newValueDate);
setError(newValueDate);
applyValidators(newValueDate);
setPopoverOpen(false);
onChange(null, newValue, new Date(newValueDate));
};

const onKeyPress = (ev: React.KeyboardEvent<HTMLInputElement>) => {
if (ev.key === 'Enter' && value) {
if (isValidDate(valueDate)) {
setError(valueDate);
applyValidators(valueDate);
} else {
setErrorText(invalidFormatText);
}
Expand Down

0 comments on commit fcf105e

Please sign in to comment.