added debounce to rjsf date picker to avoid converting dates too early when typing (#909)

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
jasquat 2024-01-19 11:18:52 -05:00 committed by GitHub
parent 0ab1ca446c
commit 6b0625ea5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import {
} from '@rjsf/utils';
import { useCallback } from 'react';
import { useDebouncedCallback } from 'use-debounce';
import { DATE_FORMAT_CARBON, DATE_FORMAT_FOR_DISPLAY } from '../../../config';
import DateAndTimeService from '../../../services/DateAndTimeService';
import { getCommonAttributes } from '../../helpers';
@ -55,21 +56,30 @@ export default function BaseInputTemplate<
};
const _onChange = useCallback(
({ target: { value } }: React.ChangeEvent<HTMLInputElement>) =>
onChange(value === '' ? options.emptyValue : value),
({ target }: React.ChangeEvent<HTMLInputElement>) => {
onChange(target.value === '' ? options.emptyValue : target.value);
},
[onChange, options]
);
const _onBlur = useCallback(
({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
onBlur(id, value),
({ target }: React.FocusEvent<HTMLInputElement>) =>
onBlur(id, target.value),
[onBlur, id]
);
const _onFocus = useCallback(
({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
onFocus(id, value),
({ target }: React.FocusEvent<HTMLInputElement>) =>
onFocus(id, target.value),
[onFocus, id]
);
const addDebouncedOnChangeDate = useDebouncedCallback(
(target: React.ChangeEvent<HTMLInputElement>) => {
_onChange(target);
},
// delay in ms
1000
);
const commonAttributes = getCommonAttributes(
label,
schema,
@ -108,7 +118,7 @@ export default function BaseInputTemplate<
value={dateValue}
autocomplete="off"
allowInput={false}
onChange={_onChange}
onChange={addDebouncedOnChangeDate}
invalid={commonAttributes.invalid}
invalidText={commonAttributes.errorMessageForField}
autoFocus={autofocus}