Skip to content

Commit 45f099c

Browse files
authored
Merge pull request #20 from jason89521/test
2 parents e139ded + 27ffef5 commit 45f099c

7 files changed

Lines changed: 144 additions & 11 deletions

File tree

packages/react-use-calendar-component/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,88 @@ This package is still under development, the API interface may change frequently
2020

2121
- [Single Mode](./src/app/components/Examples/Single.tsx)
2222
- [Multiple Mode](./src/app/components/Examples/Multiple.tsx)
23+
24+
## Install
25+
26+
```sh
27+
npm install react-use-calendar-component
28+
```
29+
30+
## API Reference
31+
32+
```ts
33+
import useCalendarComponent from 'react-use-calendar-component';
34+
35+
const {
36+
selectedDates,
37+
displayedYear,
38+
displayedMonth,
39+
changeDisplayedYear,
40+
changeDisplayedMonth,
41+
getDateCellInfos,
42+
} = useCalendarComponent({ initialDisplayedDate, selectType, value, onChange });
43+
```
44+
45+
### Args
46+
47+
#### `initialDisplayedDate`
48+
49+
This value will be used to generate the date cells at beginning. Its default value is `new Date()`.
50+
51+
#### `selectType`
52+
53+
The selection type of the calendar, currently support `'single' | 'multiple'`. Its default value is `'single'`.
54+
55+
> This option must be set if you don't want to use `single` type.
56+
57+
#### `value`
58+
59+
Set it up to get the full control of the calendar. For `'single'` selection type, the type of this value should be `Date`. For `'multiple'` selection type, the type of this value should be `Date[]`.
60+
61+
#### `onChange`
62+
63+
```ts
64+
type OnChange: (value: Date|Date[]) => void
65+
```
66+
67+
For `single` selection type, the `value` is a `Date` instance. For `multiple` selection type, the `value` is an array of `Date` instances.
68+
69+
### Returns
70+
71+
#### `selectedDates`
72+
73+
An array contains the `Date` instances of all selected date.
74+
75+
#### `displayedYear`
76+
77+
The year of the calendar which should be rendered.
78+
79+
#### `displayedMonth`
80+
81+
The month of the calendar which should be rendered.
82+
83+
#### `changeDisplayedYear`
84+
85+
Call this function to add a value to `displayedYear`.
86+
87+
#### `changeDisplayedMonth`
88+
89+
Call this function to add a value to `displayedMonth`.
90+
91+
#### `getDateCellInfos`
92+
93+
```ts
94+
interface DateCellInfo {
95+
key: string;
96+
year: number;
97+
month: number;
98+
dayOfMonth: number;
99+
dayOfWeek: number;
100+
isToday: boolean;
101+
isSelected: boolean;
102+
monthStatus: 'current' | 'next' | 'previous';
103+
selectThisDate: (options?: SelectDateOptions) => void;
104+
}
105+
```
106+
107+
This function returns an array of `DateCellInfo`. It can be used to render your calendar.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ export default function CalendarCell({
2020
onClick={() => selectThisDate()}
2121
type='button'
2222
title={title}>
23-
<div className='flex items-center justify-center'>
24-
<span>{dayOfMonth}</span>
25-
</div>
23+
{dayOfMonth}
2624
</button>
2725
);
2826
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { useState } from 'react';
22

33
import useCalendarComponent from '../../../lib';
44
import Calendar from '../Calendar';
5-
import SelectedDates from '../SelectedDates';
5+
import StateInfo from '../StateInfo';
66

77
export function Multiple() {
88
const [value, setValue] = useState([new Date()]);
9-
console.log(value);
109
const calendarControl = useCalendarComponent({
1110
selectType: 'multiple',
1211
value,
@@ -17,7 +16,7 @@ export function Multiple() {
1716
return (
1817
<div>
1918
<Calendar calendarControl={calendarControl} />
20-
<SelectedDates selectedDates={selectedDates} />
19+
<StateInfo value={value} selectedDates={selectedDates} />
2120
</div>
2221
);
2322
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState } from 'react';
22

33
import useCalendarComponent from '../../../lib';
44
import Calendar from '../Calendar';
5-
import SelectedDates from '../SelectedDates';
5+
import StateInfo from '../StateInfo';
66

77
export function Single() {
88
const [value, setValue] = useState(new Date());
@@ -12,7 +12,7 @@ export function Single() {
1212
return (
1313
<div>
1414
<Calendar calendarControl={calendarControl} />
15-
<SelectedDates selectedDates={selectedDates} />
15+
<StateInfo value={[value]} selectedDates={selectedDates} />
1616
</div>
1717
);
1818
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ interface Props {
44

55
export default function SelectedDates({ selectedDates }: Props) {
66
return (
7-
<p className='text-center mt-10 text-white flex flex-col'>
7+
<div className='flex flex-col'>
88
{selectedDates.map(date => {
99
return <span key={date.toString()}>{date.toLocaleDateString()}</span>;
1010
})}
11-
</p>
11+
</div>
1212
);
1313
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
interface Props {
2+
value: Date[];
3+
selectedDates: Date[];
4+
}
5+
6+
export default function StateInfo({ value, selectedDates }: Props) {
7+
return (
8+
<div className='text-white flex justify-between mt-5'>
9+
<div className='flex flex-col' data-testid='value'>
10+
<div className='text-lg text-rose-300'>value:</div>
11+
{value.map((v, index) => (
12+
<span key={index}>{v.toDateString()}</span>
13+
))}
14+
</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>
21+
</div>
22+
);
23+
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import userEvent from '@testing-library/user-event';
44
import { Single } from '../app/components/Examples';
55
import { getLocaleMonth } from './utils';
66

7+
const dateInstance = new Date(2022, 9, 1);
8+
const idValue = 'value';
9+
const idSelectedDates = 'selected-dates';
10+
711
describe('common functionality', () => {
812
it('should display correct year/month', async () => {
9-
const dateInstance = new Date(2022, 9, 1);
1013
jest.useFakeTimers({ now: dateInstance });
1114
const user = userEvent.setup({ delay: null });
1215
render(<Single />);
@@ -49,4 +52,29 @@ describe('common functionality', () => {
4952
expect(screen.getByText(currentMonth)).toBeInTheDocument();
5053
expect(screen.getByTitle('today')).toHaveTextContent('1');
5154
});
55+
56+
it('should change value and selected dates correctly', async () => {
57+
jest.useFakeTimers({ now: dateInstance });
58+
const user = userEvent.setup({ delay: null });
59+
render(<Single />);
60+
const valueBlock = screen.getByTestId(idValue);
61+
const datesBlock = screen.getByTestId(idSelectedDates);
62+
let testString = dateInstance.toDateString();
63+
expect(valueBlock).toHaveTextContent(testString);
64+
expect(datesBlock).toHaveTextContent(testString);
65+
66+
const date10 = screen.getByRole('button', { name: '10' });
67+
await user.click(date10);
68+
testString = new Date(2022, 9, 10).toDateString();
69+
expect(valueBlock).toHaveTextContent(testString);
70+
expect(datesBlock).toHaveTextContent(testString);
71+
expect(datesBlock.childElementCount).toBe(2);
72+
73+
const date20 = screen.getByRole('button', { name: '20' });
74+
await user.click(date20);
75+
testString = new Date(2022, 9, 20).toDateString();
76+
expect(valueBlock).toHaveTextContent(testString);
77+
expect(datesBlock).toHaveTextContent(testString);
78+
expect(datesBlock.childElementCount).toBe(2);
79+
});
5280
});

0 commit comments

Comments
 (0)