mirror of
https://github.com/sartography/spiff-arena.git
synced 2025-01-14 03:24:40 +00:00
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:
parent
0ab1ca446c
commit
6b0625ea5d
@ -8,6 +8,7 @@ import {
|
|||||||
} from '@rjsf/utils';
|
} from '@rjsf/utils';
|
||||||
|
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
import { useDebouncedCallback } from 'use-debounce';
|
||||||
import { DATE_FORMAT_CARBON, DATE_FORMAT_FOR_DISPLAY } from '../../../config';
|
import { DATE_FORMAT_CARBON, DATE_FORMAT_FOR_DISPLAY } from '../../../config';
|
||||||
import DateAndTimeService from '../../../services/DateAndTimeService';
|
import DateAndTimeService from '../../../services/DateAndTimeService';
|
||||||
import { getCommonAttributes } from '../../helpers';
|
import { getCommonAttributes } from '../../helpers';
|
||||||
@ -55,21 +56,30 @@ export default function BaseInputTemplate<
|
|||||||
};
|
};
|
||||||
|
|
||||||
const _onChange = useCallback(
|
const _onChange = useCallback(
|
||||||
({ target: { value } }: React.ChangeEvent<HTMLInputElement>) =>
|
({ target }: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
onChange(value === '' ? options.emptyValue : value),
|
onChange(target.value === '' ? options.emptyValue : target.value);
|
||||||
|
},
|
||||||
[onChange, options]
|
[onChange, options]
|
||||||
);
|
);
|
||||||
const _onBlur = useCallback(
|
const _onBlur = useCallback(
|
||||||
({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
|
({ target }: React.FocusEvent<HTMLInputElement>) =>
|
||||||
onBlur(id, value),
|
onBlur(id, target.value),
|
||||||
[onBlur, id]
|
[onBlur, id]
|
||||||
);
|
);
|
||||||
const _onFocus = useCallback(
|
const _onFocus = useCallback(
|
||||||
({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
|
({ target }: React.FocusEvent<HTMLInputElement>) =>
|
||||||
onFocus(id, value),
|
onFocus(id, target.value),
|
||||||
[onFocus, id]
|
[onFocus, id]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const addDebouncedOnChangeDate = useDebouncedCallback(
|
||||||
|
(target: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
_onChange(target);
|
||||||
|
},
|
||||||
|
// delay in ms
|
||||||
|
1000
|
||||||
|
);
|
||||||
|
|
||||||
const commonAttributes = getCommonAttributes(
|
const commonAttributes = getCommonAttributes(
|
||||||
label,
|
label,
|
||||||
schema,
|
schema,
|
||||||
@ -108,7 +118,7 @@ export default function BaseInputTemplate<
|
|||||||
value={dateValue}
|
value={dateValue}
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
allowInput={false}
|
allowInput={false}
|
||||||
onChange={_onChange}
|
onChange={addDebouncedOnChangeDate}
|
||||||
invalid={commonAttributes.invalid}
|
invalid={commonAttributes.invalid}
|
||||||
invalidText={commonAttributes.errorMessageForField}
|
invalidText={commonAttributes.errorMessageForField}
|
||||||
autoFocus={autofocus}
|
autoFocus={autofocus}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user