|
| 1 | +--- |
| 2 | +title: Use Calendar |
| 3 | +--- |
| 4 | + |
| 5 | +# React Use Calendar Component |
| 6 | + |
| 7 | +This package is aimed to provide an easy-to-use API interface for those people who want to build their own calendar component with custom style. |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +```sh |
| 12 | +npm install react-use-calendar-component |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```tsx |
| 18 | +import useCalendarComponent from 'react-use-calendar-component'; |
| 19 | + |
| 20 | +const Calendar = () => { |
| 21 | + const [value, setValue] = useState(new Date()); |
| 22 | + const { |
| 23 | + selectedDates, |
| 24 | + displayedYear, |
| 25 | + displayedMonth, |
| 26 | + changeDisplayedYear, |
| 27 | + changeDisplayedMonth, |
| 28 | + getDateCellInfos, |
| 29 | + } = useCalendarComponent({ |
| 30 | + initialDisplayedDate: new Date(2022, 10, 10), |
| 31 | + selectType: 'single', |
| 32 | + value, |
| 33 | + onChange: setValue, |
| 34 | + }); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div> |
| 38 | + {/* Display and control the year/month */} |
| 39 | + <CalendarHeader |
| 40 | + onYearClick={changeDisplayedYear} |
| 41 | + onMonthClick={changeDisplayedMonth} |
| 42 | + year={displayedYear} |
| 43 | + month={displayedMonth} |
| 44 | + /> |
| 45 | + <CalendarBody> |
| 46 | + {/* Use these function to display and control your calendar cells*/} |
| 47 | + {getDateCellInfos().map(({key, ...info}) => { |
| 48 | + return <CalendarCell key={key} {...info}> |
| 49 | + })} |
| 50 | + </CalendarBody> |
| 51 | + </div> |
| 52 | + ); |
| 53 | +}; |
| 54 | +``` |
| 55 | +
|
| 56 | +## API Reference |
| 57 | +
|
| 58 | +```ts |
| 59 | +const { |
| 60 | + selectedDates, |
| 61 | + displayedYear, |
| 62 | + displayedMonth, |
| 63 | + changeDisplayedYear, |
| 64 | + changeDisplayedMonth, |
| 65 | + getDateCellInfos, |
| 66 | +} = useCalendarComponent({ initialDisplayedDate, selectType, value, onChange }); |
| 67 | +``` |
| 68 | +
|
| 69 | +### Args |
| 70 | +
|
| 71 | +#### `initialDisplayedDate` |
| 72 | +
|
| 73 | +This value will be used to generate the date cells at beginning. Its default value is `new Date()`. |
| 74 | +
|
| 75 | +#### `selectType` |
| 76 | +
|
| 77 | +The selection type of the calendar, currently support `'single' | 'multiple'`. Its default value is `'single'`. |
| 78 | +
|
| 79 | +> This option must be set if you don't want to use `single` type. |
| 80 | +
|
| 81 | +#### `value` |
| 82 | +
|
| 83 | +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[]`. |
| 84 | +
|
| 85 | +#### `onChange` |
| 86 | +
|
| 87 | +```ts |
| 88 | +type OnChange: (value: Date|Date[]) => void |
| 89 | +``` |
| 90 | +
|
| 91 | +For `single` selection type, the `value` is a `Date` instance. For `multiple` selection type, the `value` is an array of `Date` instances. |
| 92 | +
|
| 93 | +### Returns |
| 94 | +
|
| 95 | +#### `selectedDates` |
| 96 | +
|
| 97 | +An array contains the `Date` instances of all selected date. |
| 98 | +
|
| 99 | +#### `displayedYear` |
| 100 | +
|
| 101 | +The year of the calendar which should be rendered. |
| 102 | +
|
| 103 | +#### `displayedMonth` |
| 104 | +
|
| 105 | +The month of the calendar which should be rendered. |
| 106 | +
|
| 107 | +#### `changeDisplayedYear` |
| 108 | +
|
| 109 | +Call this function to add a value to `displayedYear`. |
| 110 | +
|
| 111 | +#### `changeDisplayedMonth` |
| 112 | +
|
| 113 | +Call this function to add a value to `displayedMonth`. |
| 114 | +
|
| 115 | +#### `getDateCellInfos` |
| 116 | +
|
| 117 | +```ts |
| 118 | +interface DateCellInfo { |
| 119 | + key: string; |
| 120 | + year: number; |
| 121 | + month: number; |
| 122 | + dayOfMonth: number; |
| 123 | + dayOfWeek: number; |
| 124 | + isToday: boolean; |
| 125 | + isSelected: boolean; |
| 126 | + monthStatus: 'current' | 'next' | 'previous'; |
| 127 | + selectThisDate: (options?: SelectDateOptions) => void; |
| 128 | +} |
| 129 | +``` |
| 130 | +
|
| 131 | +This function returns an array of `DateCellInfo`. It can be used to render your calendar. |
0 commit comments