Skip to content

Commit bdd74bd

Browse files
committed
refactor: remove selectedDates from return value
1 parent 9aee31e commit bdd74bd

8 files changed

Lines changed: 25 additions & 49 deletions

File tree

packages/react-use-calendar-component/src/app/components/CalendarCell.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default function CalendarCell({
44
monthStatus,
55
isSelected,
66
isToday,
7-
monthDay: dayOfMonth,
7+
monthDay,
88
selectThisDate,
99
}: DateCellInfo) {
1010
const title = isToday ? 'today' : undefined;
@@ -20,7 +20,7 @@ export default function CalendarCell({
2020
onClick={() => selectThisDate()}
2121
type='button'
2222
title={title}>
23-
{dayOfMonth}
23+
{monthDay}
2424
</button>
2525
);
2626
}

packages/react-use-calendar-component/src/app/components/Examples/Multiple.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ export function Multiple() {
1111
value,
1212
onChange: setValue,
1313
});
14-
const { selectedDates } = calendarControl;
1514

1615
return (
1716
<div>
1817
<Calendar calendarControl={calendarControl} />
19-
<StateInfo value={value} selectedDates={selectedDates} />
18+
<StateInfo value={value} />
2019
</div>
2120
);
2221
}

packages/react-use-calendar-component/src/app/components/Examples/Single.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import StateInfo from '../StateInfo';
77
export function Single() {
88
const [value, setValue] = useState(new Date());
99
const calendarControl = useCalendarComponent({ value, onChange: setValue });
10-
const { selectedDates } = calendarControl;
1110

1211
return (
1312
<div>
1413
<Calendar calendarControl={calendarControl} />
15-
<StateInfo value={[value]} selectedDates={selectedDates} />
14+
<StateInfo value={[value]} />
1615
</div>
1716
);
1817
}

packages/react-use-calendar-component/src/app/components/SelectedDates.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
interface Props {
22
value: Date[];
3-
selectedDates: Date[];
43
}
54

6-
export default function StateInfo({ value, selectedDates }: Props) {
5+
export default function StateInfo({ value }: Props) {
76
return (
87
<div className='text-white flex justify-between mt-5'>
98
<div className='flex flex-col' data-testid='value'>
@@ -12,12 +11,6 @@ export default function StateInfo({ value, selectedDates }: Props) {
1211
<span key={index}>{v.toDateString()}</span>
1312
))}
1413
</div>
15-
<div className='flex flex-col' data-testid='selected-dates'>
16-
<div className='text-lg text-rose-300'>selected dates:</div>
17-
{selectedDates.map((date, index) => {
18-
return <span key={index}>{date.toDateString()}</span>;
19-
})}
20-
</div>
2114
</div>
2215
);
2316
}

packages/react-use-calendar-component/src/lib/hooks/useCalendarComponent.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
Value,
1010
} from '../types';
1111
import { getDateInfoByIndex, isSameDate, noop } from '../utils';
12+
import { normalizeValue } from '../utils/normalizeValue';
1213
import useDisplayedDate from './useDisplayedDate';
1314

1415
export default function useCalendarComponent<S extends SelectType = 'single'>({
@@ -26,17 +27,7 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
2627
changeDisplayedMonth,
2728
} = useDisplayedDate(initialDisplayedDate);
2829
const [internalValue, setInternalValue] = useState(userValue);
29-
const selectedDates = (() => {
30-
if (selectType === 'multiple') {
31-
if (userValue) return userValue as Date[];
32-
if (internalValue) return internalValue as Date[];
33-
}
34-
35-
if (userValue) return [userValue] as Date[];
36-
if (internalValue) return [internalValue] as Date[];
37-
38-
return [];
39-
})();
30+
const selectedDates = normalizeValue(selectType, userValue || internalValue);
4031

4132
const handleValueChange = (value: Value<S>) => {
4233
onChange(value);
@@ -54,6 +45,10 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
5445
monthDay,
5546
...info
5647
} = getDateInfoByIndex(year, month, cellIndex);
48+
49+
const isSelected = !!selectedDates.find(date =>
50+
isSameDate(date, new Date(dateYear, dateMonth, monthDay))
51+
);
5752
const selectThisDate = (options: SelectDateOptions = {}) => {
5853
const { changeDisplayedValues = true } = options;
5954
if (changeDisplayedValues) {
@@ -81,9 +76,7 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
8176
year: dateYear,
8277
month: dateMonth,
8378
monthDay,
84-
isSelected: !!selectedDates.find(date =>
85-
isSameDate(date, new Date(dateYear, dateMonth, monthDay))
86-
),
79+
isSelected,
8780
selectThisDate,
8881
...info,
8982
};
@@ -102,7 +95,6 @@ export default function useCalendarComponent<S extends SelectType = 'single'>({
10295
};
10396

10497
return {
105-
selectedDates,
10698
displayedYear,
10799
displayedMonth,
108100
changeDisplayedYear,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Value, SelectType } from '../types';
2+
3+
export function normalizeValue<S extends SelectType>(
4+
selectType?: S,
5+
value?: Value<S>
6+
): Date[] {
7+
if (!value) return [];
8+
if (selectType === 'multiple') {
9+
return value as Value<'multiple'>;
10+
}
11+
12+
return [value as Value<'single'>];
13+
}

packages/react-use-calendar-component/src/test/single.test.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { getLocaleMonth } from './utils';
66

77
const dateInstance = new Date(2022, 9, 1);
88
const idValue = 'value';
9-
const idSelectedDates = 'selected-dates';
109

1110
describe('common functionality', () => {
1211
it('should display correct year/month', async () => {
@@ -58,23 +57,17 @@ describe('common functionality', () => {
5857
const user = userEvent.setup({ delay: null });
5958
render(<Single />);
6059
const valueBlock = screen.getByTestId(idValue);
61-
const datesBlock = screen.getByTestId(idSelectedDates);
6260
let testString = dateInstance.toDateString();
6361
expect(valueBlock).toHaveTextContent(testString);
64-
expect(datesBlock).toHaveTextContent(testString);
6562

6663
const date10 = screen.getByRole('button', { name: '10' });
6764
await user.click(date10);
6865
testString = new Date(2022, 9, 10).toDateString();
6966
expect(valueBlock).toHaveTextContent(testString);
70-
expect(datesBlock).toHaveTextContent(testString);
71-
expect(datesBlock.childElementCount).toBe(2);
7267

7368
const date20 = screen.getByRole('button', { name: '20' });
7469
await user.click(date20);
7570
testString = new Date(2022, 9, 20).toDateString();
7671
expect(valueBlock).toHaveTextContent(testString);
77-
expect(datesBlock).toHaveTextContent(testString);
78-
expect(datesBlock.childElementCount).toBe(2);
7972
});
8073
});

0 commit comments

Comments
 (0)