Skip to content

Commit

Permalink
fix(DatePicker): fixed nulling of value (#3736)
Browse files Browse the repository at this point in the history
closes #3733, #3545
  • Loading branch information
gizeasy authored Aug 19, 2024
1 parent d02f78e commit 2e8e851
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/components/DatePicker/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { format, isValid, parse, startOfToday } from 'date-fns';
import { useCallback, useEffect } from 'react';
import { useIMask } from 'react-imask';

import { getForm } from '##/components/FieldGroup';
import { TextFieldPropForm } from '##/components/TextField';
import { useMutableRef } from '##/hooks/useMutableRef';
import { range } from '##/utils/array';
import { DateRange } from '##/utils/types/Date';

import { range } from '../../utils/array';
import { DateRange } from '../../utils/types/Date';
import { getForm } from '../FieldGroup/FieldGroup';
import { TextFieldPropForm } from '../TextField/TextField';
import { DatePickerPropType } from './types';

export const datePickerPropSeparatorDefault = '.';
Expand Down Expand Up @@ -164,15 +164,24 @@ export const useStringValue = (
onChangeRef: React.MutableRefObject<
DatePickerFieldTypeDatePropOnChange | undefined
>,
imaskProps: ReturnType<typeof useIMask>,
imaskProps: ReturnType<typeof useIMask<HTMLInputElement>>,
) => {
const stringValue = imaskProps.value;

const refs = useMutableRef([imaskProps.setValue, value] as const);

const setStringValueToNull = useCallback(() => {
if (imaskProps.ref?.current && imaskProps.maskRef.current) {
refs.current[0]('');
imaskProps.ref.current.value = '';
imaskProps.maskRef.current.updateValue();
}
}, []);

const handleClear: React.MouseEventHandler<HTMLButtonElement> = useCallback(
(e) => {
refs.current[0]('');
setStringValueToNull();

if (refs.current[1]) {
onChangeRef.current?.(null, { e: e as unknown as Event });
}
Expand Down Expand Up @@ -202,7 +211,7 @@ export const useStringValue = (
: undefined;

if (isValid(date)) {
refs.current[0]('');
setStringValueToNull();
}
}
}, [value?.getTime()]);
Expand Down

0 comments on commit 2e8e851

Please sign in to comment.