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): updated logic for parsing and focus management #9794

Merged
merged 1 commit into from
Nov 13, 2023
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
4 changes: 2 additions & 2 deletions packages/react-core/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
className,
locale = undefined,
dateFormat = yyyyMMddFormat,
dateParse = (val: string) => val.split('-').length === 3 && new Date(`${val}T00:00:00`),
dateParse = (val: string) => (val.split('-').length === 3 ? new Date(`${val}T00:00:00`) : new Date(undefined)),
Copy link
Contributor Author

@thatblindgeye thatblindgeye Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resolves the issue mentioned here #9721 (comment) regarding the return type of this prop. This basically just ensures that a Date type is always returned without really changing the outcome (instead of false we just get an invalid date object which should have the same effect).

Alternatively we could update the return type to include boolean (probably not the best since we're using dateParse to set internal date state) or update the default function to just return something like new Date(val.replace("/", "-")...), which would also satisfy a comment in the original issue of locales that use forward slashes for dates (or return an invalid date object).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should work fine - i'm approving as is. but i do think the point about forward slashes is valid, and if we don't add it as part of this, it probably makes sense to capture it in another issue.

isDisabled = false,
placeholder = 'YYYY-MM-DD',
value: valueProp = '',
Expand Down 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,7 +142,7 @@
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(() => {
setPristine(!value);
Expand All @@ -153,7 +153,7 @@
if (value === '' && !pristine && !textInputFocused) {
dateIsRequired ? setErrorText(emptyDateText) : setErrorText('');
}
}, [value]);

Check warning on line 156 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: 'dateIsRequired', 'dateParse', 'emptyDateText', 'errorText', 'pristine', 'setError', 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) => {
setErrorText(validators.map((validator) => validator(date)).join('\n') || '');
Expand Down Expand Up @@ -219,7 +219,7 @@
},
isCalendarOpen: popoverOpen
}),
[setPopoverOpen, popoverOpen, selectOpen]

Check warning on line 222 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 All @@ -230,7 +230,7 @@
return (
<div className={css(styles.datePicker, className)} ref={datePickerWrapperRef} style={style} {...props}>
<Popover
elementToFocus={valueDate ? focusSelectorForSelectedDate : focusSelectorForUnselectedDate}
elementToFocus={isValidDate(valueDate) ? focusSelectorForSelectedDate : focusSelectorForUnselectedDate}
position="bottom"
bodyContent={
<CalendarMonth
Expand Down
Loading