Skip to content

Commit

Permalink
fix: fix clearing text on recieving null as value in datepicker
Browse files Browse the repository at this point in the history
affects: @medly-components/core
  • Loading branch information
gmukul01 committed Jun 8, 2024
1 parent a712ffa commit 7c14373
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const Component: FC<DatePickerProps> = memo(
inputRef = useCombinedRefs<HTMLInputElement>(ref, useRef(null)),
[inputKey, setInputKey] = useState(0),
[textValue, setTextValue] = useState(''),
[isFocused, setFocusedState] = useState(false),
[builtInErrorMessage, setErrorMessage] = useState(''),
[showCalendar, toggleCalendar] = useState(false),
[active, setActive] = useState(false),
Expand All @@ -52,8 +53,10 @@ const Component: FC<DatePickerProps> = memo(
useEffect(() => {
if (date) {
setTextValue(format(date, displayFormat!).replace(new RegExp('\\/|\\-', 'g'), ' $& '));
} else if (!isErrorPresent && !isFocused) {
setTextValue('');
}
}, [date, displayFormat]);
}, [date, isFocused, isErrorPresent, displayFormat]);
const onTextChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value,
Expand Down Expand Up @@ -95,7 +98,10 @@ const Component: FC<DatePickerProps> = memo(
[props.required, displayFormat, validator, minSelectableDate, maxSelectableDate]
),
onBlur = useCallback(
(event: React.FocusEvent<HTMLInputElement>) => inputRef.current?.value && validate(event, props.onBlur),
(event: React.FocusEvent<HTMLInputElement>) => {
setFocusedState(false);
inputRef.current?.value && validate(event, props.onBlur);
},
[props.onBlur, displayFormat]
),
onInvalid = useCallback(
Expand All @@ -105,6 +111,7 @@ const Component: FC<DatePickerProps> = memo(
onFocus = useCallback(
(event: React.FocusEvent<HTMLInputElement>) => {
setActive(true);
setFocusedState(true);
props.onFocus && props.onFocus(event);
},
[props.onFocus]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ export const useDateRangeTextFieldsHandlers = (props: Props) => {
useEffect(() => {
const formattedStartDate = selectedDates.startDate ? getFormattedDate(selectedDates.startDate, displayFormat) : '',
formattedEndDate = selectedDates.endDate ? getFormattedDate(selectedDates.endDate, displayFormat) : '';
formattedStartDate && setStartDateText(formattedStartDate);
formattedEndDate && setEndDateText(formattedEndDate);
formattedStartDate && setStartDateMaskLabel(formattedStartDate || mask);
formattedEndDate && setEndDateMaskLabel(formattedEndDate || mask);

if (formattedStartDate) {
setStartDateText(formattedStartDate);
setStartDateMaskLabel(formattedStartDate);
} else if (!isActive && !isErrorPresent) {
setStartDateText('');
setStartDateMaskLabel(mask);
}

if (formattedEndDate) {
setEndDateText(formattedEndDate);
setEndDateMaskLabel(formattedEndDate);
} else if (!isActive && !isErrorPresent) {
setEndDateText('');
setEndDateMaskLabel(mask);
}
isValidDate(selectedDates.startDate) && isValidDate(selectedDates.endDate) && setErrorMessage('');
}, [isActive, selectedDates, displayFormat]);

Expand Down

0 comments on commit 7c14373

Please sign in to comment.