Skip to content

Commit 9737fbc

Browse files
committed
chore: replace useMemo usage with React.memo for DayButton
1 parent 4b2d193 commit 9737fbc

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/components/experimental/Calendar/Calendar.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export type RangeProps = BaseProps & {
4141

4242
export type CalendarProps = SingleProps | MultipleProps | RangeProps;
4343

44+
const SelectionTypeContext = React.createContext<SelectionType>('single');
45+
4446
export function Calendar(props: SingleProps): JSX.Element;
4547
export function Calendar(props: MultipleProps): JSX.Element;
4648
export function Calendar(props: RangeProps): JSX.Element;
@@ -61,11 +63,13 @@ export function Calendar(props: CalendarProps): JSX.Element {
6163
const selectionType = props.selectionType ?? 'single';
6264
const defaults = getDefaultClassNames();
6365

64-
const DayBtn = useMemo(
65-
() => (p: React.ComponentProps<typeof RdpDayButton>) =>
66-
<CalendarDayButton selectionType={selectionType} {...p} />,
67-
[selectionType]
68-
);
66+
const MemoCalendarDayButton = React.memo(CalendarDayButton);
67+
MemoCalendarDayButton.displayName = 'MemoCalendarDayButton';
68+
69+
const DayButtonComp = (p: React.ComponentProps<typeof RdpDayButton>) => {
70+
const ctxSelectionType = React.useContext(SelectionTypeContext);
71+
return <MemoCalendarDayButton selectionType={ctxSelectionType} {...p} />;
72+
};
6973

7074
const common = {
7175
showOutsideDays: false,
@@ -82,7 +86,7 @@ export function Calendar(props: CalendarProps): JSX.Element {
8286
if (orientation === 'right') return <ChevronRightIcon size={24} {...p} />;
8387
return null as unknown as React.ReactElement;
8488
},
85-
DayButton: DayBtn,
89+
DayButton: DayButtonComp,
8690
...(components ?? {})
8791
},
8892
...rest

0 commit comments

Comments
 (0)