Skip to content

Commit

Permalink
feat: defaultDate empty and side-effects addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dias, Diego committed Sep 13, 2024
1 parent 7ca4295 commit 4548a0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ DateValueSet.args = {
minDate: undefined,
maxDate: undefined,
language: "en",
defaultValue: new Date(),
theme: {},
};

Expand Down
15 changes: 6 additions & 9 deletions packages/ui/src/components/Datepicker/Datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
labelClearButton = "Clear",
showTodayButton = true,
labelTodayButton = "Today",
defaultValue = new Date(),
defaultValue,
minDate,
maxDate,
language = "en",
Expand All @@ -126,10 +126,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
ref,
) => {
const theme = mergeDeep(getTheme().datepicker, customTheme);

const effectiveDefaultValue = useMemo(() => {
return getFirstDateInRange(defaultValue, minDate, maxDate);
}, []);
const initialDate = defaultValue ? getFirstDateInRange(defaultValue, minDate, maxDate) : null;

const effectiveDefaultView = useMemo(() => {
return defaultValue ? getFirstDateInRange(defaultValue, minDate, maxDate) : new Date();
Expand All @@ -138,7 +135,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
const [isOpen, setIsOpen] = useState(open);
const [view, setView] = useState<Views>(Views.Days);
// selectedDate is the date selected by the user
const [selectedDate, setSelectedDate] = useState<Date | null>(value ?? effectiveDefaultValue);
const [selectedDate, setSelectedDate] = useState<Date | null>(value ?? initialDate);
// viewDate is only for navigation
const [viewDate, setViewDate] = useState<Date>(value ?? effectiveDefaultView);

Expand All @@ -159,7 +156,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
};

const clearDate = () => {
changeSelectedDate(defaultValue, true);
changeSelectedDate(initialDate, true);
if (defaultValue) {
setViewDate(defaultValue);
}
Expand Down Expand Up @@ -257,7 +254,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
setSelectedDate(effectiveValue);
}
if (selectedDate == null) {
setSelectedDate(effectiveDefaultValue);
setSelectedDate(initialDate);
}
}, [value, setSelectedDate, setViewDate, selectedDate]);

Expand Down Expand Up @@ -296,7 +293,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
}}
value={displayValue}
readOnly
defaultValue={effectiveDefaultValue ? getFormattedDate(language, effectiveDefaultValue) : label}
defaultValue={initialDate ? getFormattedDate(language, initialDate) : label}
{...props}
/>
)}
Expand Down

0 comments on commit 4548a0c

Please sign in to comment.