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

fix(Datepicker): revalidate on validators prop change #10293

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -131,7 +131,7 @@
const [selectOpen, setSelectOpen] = React.useState(false);
const [pristine, setPristine] = React.useState(true);
const [textInputFocused, setTextInputFocused] = React.useState(false);
const widthChars = React.useMemo(() => Math.max(dateFormat(new Date()).length, placeholder.length), [dateFormat]);

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

View workflow job for this annotation

GitHub Actions / lint

React Hook React.useMemo has a missing dependency: 'placeholder.length'. Either include it or remove the dependency array
const style = { [cssFormControlWidthChars.name]: widthChars, ...styleProps };
const buttonRef = React.useRef<HTMLButtonElement>();
const datePickerWrapperRef = React.useRef<HTMLDivElement>();
Expand All @@ -142,20 +142,26 @@
React.useEffect(() => {
setValue(valueProp);
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 @@
onBlur(event, value, onBlurDateArg);

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

if (!dateIsValid && !pristine) {
Expand All @@ -195,15 +201,15 @@
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 All @@ -219,7 +225,7 @@
},
isCalendarOpen: popoverOpen
}),
[setPopoverOpen, popoverOpen, selectOpen]

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

View workflow job for this annotation

GitHub Actions / lint

React Hook useImperativeHandle has an unnecessary dependency: 'selectOpen'. Either exclude it or remove the dependency array
);

const createFocusSelectorString = (modifierClass: string) =>
Expand Down
Loading