Skip to content

Commit 6a03df0

Browse files
committed
feat: support multiple selection
1 parent a6e0460 commit 6a03df0

4 files changed

Lines changed: 80 additions & 28 deletions

File tree

packages/react-use-calendar-component/src/app/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useState } from 'react';
22

3+
import Multiple from './components/Examples/Multiple';
34
import Single from './components/Examples/Single';
45

5-
type ExampleType = 'single';
6+
type ExampleType = 'single' | 'multiple';
67

78
const exampleMap = {
89
single: <Single />,
10+
multiple: <Multiple />,
911
};
1012

1113
export default function App() {
@@ -17,6 +19,7 @@ export default function App() {
1719
value={value}
1820
onChange={e => setValue(e.currentTarget.value as ExampleType)}>
1921
<option value='single'>single</option>
22+
<option value='multiple'>multiple</option>
2023
</select>
2124
{exampleMap[value]}
2225
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useEffect } from 'react';
2+
3+
import useCalendarComponent from '../../../lib';
4+
import Calendar from '../Calendar';
5+
6+
export default function Multiple() {
7+
const calendarControl = useCalendarComponent({ selectType: 'multiple' });
8+
const { selectedDates, value } = calendarControl;
9+
// const { year, month, dayOfMonth } = selectedDate;
10+
11+
useEffect(() => {
12+
console.log('value:', value);
13+
}, [value]);
14+
15+
return (
16+
<div>
17+
<Calendar calendarControl={calendarControl} />
18+
<p className='text-center mt-10 text-white flex flex-col'>
19+
{selectedDates.map(date => {
20+
const localeString = date.toLocaleString(navigator.language, {
21+
dateStyle: 'long',
22+
});
23+
return <span key={localeString}>{localeString}</span>;
24+
})}
25+
</p>
26+
</div>
27+
);
28+
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
import { useEffect } from 'react';
2+
13
import useCalendarComponent from '../../../lib';
24
import Calendar from '../Calendar';
35

46
export default function Single() {
57
const calendarControl = useCalendarComponent();
6-
// const { selectedDate } = calendarControl;
8+
const { selectedDates, value } = calendarControl;
79
// const { year, month, dayOfMonth } = selectedDate;
810

11+
useEffect(() => {
12+
console.log('value:', value);
13+
}, [value]);
14+
915
return (
1016
<div>
1117
<Calendar calendarControl={calendarControl} />
12-
{/* <p className='text-center mt-10 text-white'>
13-
{new Date(year, month, dayOfMonth).toLocaleString(navigator.language, {
14-
dateStyle: 'long',
18+
<p className='text-center mt-10 text-white flex flex-col'>
19+
{selectedDates.map(date => {
20+
const localeString = date.toLocaleString(navigator.language, {
21+
dateStyle: 'long',
22+
});
23+
return <span key={localeString}>{localeString}</span>;
1524
})}
16-
</p> */}
25+
</p>
1726
</div>
1827
);
1928
}

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useState } from 'react';
33
import type {
44
UseCalendarOptions,
55
DateCellInfo,
6-
SelectDate,
76
SelectDateOptions,
87
SelectType,
98
} from '../types';
@@ -12,11 +11,11 @@ import useDisplayedDate from './useDisplayedDate';
1211

1312
const CELLS_OF_PICKER = 42;
1413

15-
export default function useCalendarComponent<S extends SelectType>({
14+
export default function useCalendarComponent<S extends SelectType = 'single'>({
1615
displayedDate = new Date(),
1716
selectType,
17+
defaultValue,
1818
}: UseCalendarOptions<S> = {}) {
19-
console.log(selectType);
2019
const {
2120
displayedYear,
2221
displayedMonth,
@@ -25,7 +24,8 @@ export default function useCalendarComponent<S extends SelectType>({
2524
changeDisplayedYear,
2625
changeDisplayedMonth,
2726
} = useDisplayedDate(displayedDate);
28-
const [selectedDate, setSelectedDate] = useState<Date[]>([]);
27+
const [selectedDates, setSelectedDates] = useState<Date[]>([]);
28+
const [value, setValue] = useState(defaultValue);
2929

3030
const getDateCellInfo = (cellIndex: number): DateCellInfo => {
3131
const prevMonth = displayedMonth === 0 ? 11 : displayedMonth - 1;
@@ -58,8 +58,28 @@ export default function useCalendarComponent<S extends SelectType>({
5858
};
5959
})();
6060
const weekDay = cellIndex % 7;
61-
const selectThisDate = (options?: SelectDateOptions) => {
62-
selectDate({ year, month, dayOfMonth }, options);
61+
const selectThisDate = (options: SelectDateOptions = {}) => {
62+
const { changeDisplayedValues = true } = options;
63+
if (changeDisplayedValues) {
64+
setDisplayedYear(year);
65+
setDisplayedMonth(month);
66+
}
67+
const selectedDate = new Date(year, month, dayOfMonth);
68+
if (selectType === 'multiple') {
69+
const newDates = selectedDates.filter(
70+
date => date.toString() !== selectedDate.toString()
71+
);
72+
if (newDates.length === selectedDates.length)
73+
newDates.push(selectedDate);
74+
newDates.sort((a, b) => a.getTime() - b.getTime());
75+
setSelectedDates(newDates);
76+
setValue(newDates as any);
77+
78+
return;
79+
}
80+
81+
setSelectedDates([selectedDate]);
82+
setValue(selectedDate as any);
6383
};
6484

6585
return {
@@ -69,10 +89,12 @@ export default function useCalendarComponent<S extends SelectType>({
6989
dayOfMonth: dayOfMonth,
7090
dayOfWeek: weekDay,
7191
isToday: isToday(year, month, dayOfMonth),
72-
isSelected:
73-
year === selectedDate[0]?.getFullYear() &&
74-
month === selectedDate[0]?.getMonth() &&
75-
dayOfMonth === selectedDate[0]?.getDate(),
92+
isSelected: !!selectedDates.find(
93+
date =>
94+
date.getFullYear() === year &&
95+
date.getMonth() === month &&
96+
date.getDate() === dayOfMonth
97+
),
7698
monthStatus:
7799
monthOffset < 0 ? 'previous' : monthOffset > 0 ? 'next' : 'current',
78100
selectThisDate,
@@ -88,21 +110,11 @@ export default function useCalendarComponent<S extends SelectType>({
88110
return arr;
89111
};
90112

91-
const selectDate: SelectDate = (
92-
{ year, month, dayOfMonth },
93-
options = {}
94-
) => {
95-
const { changeDisplayedValues = false } = options;
96-
setSelectedDate([new Date(year, month, dayOfMonth)]);
97-
if (changeDisplayedValues) {
98-
setDisplayedYear(year);
99-
setDisplayedMonth(month);
100-
}
101-
};
102-
103113
return {
114+
selectedDates,
104115
displayedYear,
105116
displayedMonth,
117+
value,
106118
changeDisplayedYear,
107119
changeDisplayedMonth,
108120
getDateCellInfos,

0 commit comments

Comments
 (0)