Skip to content

Commit

Permalink
fix(DatePicker): fixed clearButton logic (#3594)
Browse files Browse the repository at this point in the history
  • Loading branch information
gizeasy authored Apr 3, 2024
1 parent 986cdac commit a7ea473
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DatePickerFieldTypeDate = React.forwardRef<
...otherProps
} = props;

const inputRef = usePicker({
const [inputRef, onClear] = usePicker({
format,
separator,
onChange,
Expand All @@ -46,6 +46,7 @@ export const DatePickerFieldTypeDate = React.forwardRef<
inputContainerRef={ref}
inputRef={useForkRef([inputRef, inputRefProp])}
placeholder={placeholder}
onClear={onClear}
/>
);
});
16 changes: 9 additions & 7 deletions src/components/DatePicker/DatePickerFieldTypeDate/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ export const usePicker = (props: UsePickerProps) => {
[minDate?.getTime(), maxDate?.getTime(), formatProp, separator],
);

const {
ref,
setValue: setStringValue,
value: stringValue,
} = useIMask<HTMLInputElement, ReactMaskOpts>(
const imaskProps = useIMask<HTMLInputElement, ReactMaskOpts>(
{
mask: Date as unknown as MaskedDate,
pattern: formatProp,
Expand Down Expand Up @@ -237,7 +233,13 @@ export const usePicker = (props: UsePickerProps) => {
{ onAccept },
);

useStringValue(value, stringValue, formatProp, separator, setStringValue);
const handleClear = useStringValue(
value,
formatProp,
separator,
onChangeRef,
imaskProps,
);

return ref;
return [imaskProps.ref, handleClear] as const;
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const DatePickerFieldTypeDateTime = React.forwardRef<
...otherProps
} = props;

const inputRef = usePicker({
const [inputRef, onClear] = usePicker({
onChange,
value,
onError,
Expand All @@ -52,6 +52,7 @@ export const DatePickerFieldTypeDateTime = React.forwardRef<
inputContainerRef={ref}
inputRef={useForkRef([inputRef, inputRefProp])}
placeholder={placeholder}
onClear={onClear}
/>
);
});
16 changes: 9 additions & 7 deletions src/components/DatePicker/DatePickerFieldTypeDateTime/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ export const usePicker = (props: UsePickerProps) => {
[minDate?.getTime(), maxDate?.getTime(), formatProp, separator],
);

const {
ref,
setValue: setStringValue,
value: stringValue,
} = useIMask<HTMLInputElement, ReactMaskOpts>(
const imaskProps = useIMask<HTMLInputElement, ReactMaskOpts>(
{
mask: Date as unknown as MaskedDate,
pattern: formatProp,
Expand Down Expand Up @@ -319,7 +315,13 @@ export const usePicker = (props: UsePickerProps) => {
{ onAccept },
);

useStringValue(value, stringValue, formatProp, separator, setStringValue);
const handleClear = useStringValue(
value,
formatProp,
separator,
onChangeRef,
imaskProps,
);

return ref;
return [imaskProps.ref, handleClear] as const;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DatePickerFieldTypeMonth = React.forwardRef<
...otherProps
} = props;

const inputRef = usePicker({
const [inputRef, onClear] = usePicker({
value,
format,
separator,
Expand All @@ -46,6 +46,7 @@ export const DatePickerFieldTypeMonth = React.forwardRef<
inputContainerRef={ref}
inputRef={useForkRef([inputRef, inputRefProp])}
placeholder={placeholder}
onClear={onClear}
/>
);
});
16 changes: 9 additions & 7 deletions src/components/DatePicker/DatePickerFieldTypeMonth/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ export const usePicker = (props: UsePickerProps) => {
[minDate?.getTime(), maxDate?.getTime(), formatProp, separator],
);

const {
ref,
setValue: setStringValue,
value: stringValue,
} = useIMask<HTMLInputElement, ReactMaskOpts>(
const imaskProps = useIMask<HTMLInputElement, ReactMaskOpts>(
{
mask: Date as unknown as MaskedDate,
pattern: formatProp,
Expand Down Expand Up @@ -224,7 +220,13 @@ export const usePicker = (props: UsePickerProps) => {
{ onAccept },
);

useStringValue(value, stringValue, formatProp, separator, setStringValue);
const handleClear = useStringValue(
value,
formatProp,
separator,
onChangeRef,
imaskProps,
);

return ref;
return [imaskProps.ref, handleClear] as const;
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const DatePickerFieldTypeTime = React.forwardRef<
...otherProps
} = props;

const inputRef = usePicker({
const [inputRef, onClear] = usePicker({
value,
onChange,
onError,
Expand All @@ -52,6 +52,7 @@ export const DatePickerFieldTypeTime = React.forwardRef<
inputContainerRef={ref}
inputRef={useForkRef([inputRef, inputRefProp])}
placeholder={placeholder}
onClear={onClear}
/>
);
});
16 changes: 9 additions & 7 deletions src/components/DatePicker/DatePickerFieldTypeTime/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ export const usePicker = (props: UsePickerProps) => {
[minDate?.getTime(), maxDate?.getTime(), formatProp, separator],
);

const {
ref,
setValue: setStringValue,
value: stringValue,
} = useIMask<HTMLInputElement, ReactMaskOpts>(
const imaskProps = useIMask<HTMLInputElement, ReactMaskOpts>(
{
mask: Date as unknown as MaskedDate,
pattern: formatProp,
Expand Down Expand Up @@ -289,7 +285,13 @@ export const usePicker = (props: UsePickerProps) => {
{ onAccept },
);

useStringValue(value, stringValue, formatProp, separator, setStringValue);
const handleClear = useStringValue(
value,
formatProp,
separator,
onChangeRef,
imaskProps,
);

return ref;
return [imaskProps.ref, handleClear] as const;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DatePickerFieldTypeYear = React.forwardRef<
...otherProps
} = props;

const inputRef = usePicker({
const [inputRef, onClear] = usePicker({
value,
minDate,
maxDate,
Expand All @@ -46,6 +46,7 @@ export const DatePickerFieldTypeYear = React.forwardRef<
inputContainerRef={ref}
inputRef={useForkRef([inputRef, inputRefProp])}
placeholder={placeholder}
onClear={onClear}
/>
);
});
16 changes: 9 additions & 7 deletions src/components/DatePicker/DatePickerFieldTypeYear/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ export const usePicker = (props: UsePickerProps) => {
[minDate?.getTime(), maxDate?.getTime(), formatProp, separator],
);

const {
ref,
setValue: setStringValue,
value: stringValue,
} = useIMask<HTMLInputElement, ReactMaskOpts>(
const imaskProps = useIMask<HTMLInputElement, ReactMaskOpts>(
{
mask: Date as unknown as MaskedDate,
pattern: formatProp,
Expand Down Expand Up @@ -215,7 +211,13 @@ export const usePicker = (props: UsePickerProps) => {
{ onAccept },
);

useStringValue(value, stringValue, formatProp, separator, setStringValue);
const handleClear = useStringValue(
value,
formatProp,
separator,
onChangeRef,
imaskProps,
);

return ref;
return [imaskProps.ref, handleClear] as const;
};
38 changes: 33 additions & 5 deletions src/components/DatePicker/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { format, isValid, parse, startOfToday } from 'date-fns';
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import { useIMask } from 'react-imask';

import { useMutableRef } from '##/hooks/useMutableRef';

import { range } from '../../utils/array';
import { DateRange } from '../../utils/types/Date';
Expand Down Expand Up @@ -147,16 +150,39 @@ export const getFieldName = (
export const getDropdownZIndex = (style?: React.CSSProperties) =>
typeof style?.zIndex === 'number' ? style.zIndex + 1 : undefined;

type DatePickerFieldTypeDatePropOnChange = (
value: Date | null,
props: {
e: Event;
},
) => void;

export const useStringValue = (
value: Date | undefined | null,
stringValue: string,
formatProp: string,
separator: string,
setStringValue: React.Dispatch<string>,
onChangeRef: React.MutableRefObject<
DatePickerFieldTypeDatePropOnChange | undefined
>,
imaskProps: ReturnType<typeof useIMask>,
) => {
const stringValue = imaskProps.value;

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

const handleClear: React.MouseEventHandler<HTMLButtonElement> = useCallback(
(e) => {
refs.current[0]('');
if (refs.current[1]) {
onChangeRef.current?.(null, { e: e as unknown as Event });
}
},
[],
);

useEffect(() => {
if (value && isValid(value)) {
setStringValue(format(value, formatProp));
refs.current[0](format(value, formatProp));
}

if (!value && stringValue) {
Expand All @@ -176,8 +202,10 @@ export const useStringValue = (
: undefined;

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

return handleClear;
};

0 comments on commit a7ea473

Please sign in to comment.