Skip to content

Commit 4867df2

Browse files
committed
feat(Calendar): customize Calendar/RangeCalendar view
1 parent e126fc9 commit 4867df2

File tree

18 files changed

+1091
-205
lines changed

18 files changed

+1091
-205
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
"types": "./dist/cjs/index.d.ts",
1919
"default": "./dist/cjs/index.js"
2020
}
21+
},
22+
"./uikit": {
23+
"import": {
24+
"types": "./dist/esm/uikit.d.ts",
25+
"default": "./dist/esm/uikit.js"
26+
},
27+
"require": {
28+
"types": "./dist/cjs/uikit.d.ts",
29+
"default": "./dist/cjs/uikit.js"
30+
}
2131
}
2232
},
2333
"sideEffects": [

src/components/Calendar/Calendar.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,51 @@ import React from 'react';
44

55
import type {DateTime} from '@gravity-ui/date-utils';
66

7-
import {CalendarView} from '../CalendarView/CalendarView';
8-
import type {CalendarInstance, CalendarSize} from '../CalendarView/CalendarView';
7+
import {Provider, useContextProps} from '../../utils/providers';
8+
import type {RenderProps, SlotProps} from '../../utils/providers';
9+
import {CalendarContext, CalendarStateContext, CalendarView} from '../CalendarView/CalendarView';
10+
import type {CalendarSize} from '../CalendarView/CalendarView';
911
import {useCalendarState} from '../CalendarView/hooks/useCalendarState';
10-
import type {CalendarStateOptions} from '../CalendarView/hooks/useCalendarState';
11-
import type {AccessibilityProps, DomProps, FocusEvents, StyleProps} from '../types';
12+
import type {CalendarState, CalendarStateOptions} from '../CalendarView/hooks/useCalendarState';
13+
import type {AccessibilityProps, DomProps, FocusEvents} from '../types';
1214

1315
import '../CalendarView/Calendar.scss';
1416

15-
export interface CalendarProps<T = DateTime>
17+
export interface CalendarRenderProps {
18+
/**
19+
* State of the calendar.
20+
*/
21+
state: CalendarState;
22+
}
23+
24+
export interface CalendarProps<T = DateTime, RP = CalendarRenderProps>
1625
extends CalendarStateOptions<T>,
26+
RenderProps<RP>,
1727
DomProps,
18-
StyleProps,
1928
FocusEvents,
20-
AccessibilityProps {
29+
AccessibilityProps,
30+
SlotProps {
2131
/**
2232
* The size of the element.
2333
* @default m
2434
*/
2535
size?: CalendarSize;
2636
}
27-
export const Calendar = React.forwardRef<CalendarInstance, CalendarProps>(function Calendar(
37+
export const Calendar = React.forwardRef<HTMLDivElement, CalendarProps>(function Calendar(
2838
props: CalendarProps,
29-
ref,
39+
forwardedRef,
3040
) {
31-
const state = useCalendarState(props);
41+
const [mergedProps, ref] = useContextProps(props, forwardedRef, CalendarContext);
42+
const state = useCalendarState(mergedProps as any);
3243

33-
return <CalendarView ref={ref} {...props} state={state} />;
44+
return (
45+
<Provider
46+
values={[
47+
[CalendarStateContext, state],
48+
[CalendarContext, mergedProps],
49+
]}
50+
>
51+
<CalendarView ref={ref} {...mergedProps} />
52+
</Provider>
53+
);
3454
});

src/components/Calendar/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Calendar} from '@gravity-ui/date-components';
1010

1111
`Calendar` is a flexible, user-friendly calendar component for React applications. It allows users to view, select, and manage dates with ease. Ideal for event scheduling, booking systems, and any application where date selection is essential. It can be controlled if you set `value` property. Or it can be uncontrolled if you don't set any value, but in this case you can manage the initial state with optional property `defaultValue`. Component is uncontrolled by default.
1212

13-
### Useful addition
13+
## Useful addition
1414

1515
To set dates in the right format you may need to include additional helpers from [Date Utils library](https://gravity-ui.com/libraries/date-utils)
1616

@@ -174,6 +174,49 @@ LANDING_BLOCK-->
174174

175175
`timeZone` is the property to set the time zone of the value in the input. [Learn more about time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List)
176176

177+
## Customization
178+
179+
A calendar consists of a grouping element containing date / month / quarter / year grid, a previous and next button for navigating between date ranges, heading for displaying current date range and layout switch button for switching layout mode e.g. days / months / quarters / years. Calendar grid consists of cells containing button elements that can be pressed and navigated to using the arrow keys to select a date.
180+
181+
```jsx
182+
import {Flex} from '@gravity-ui/uikit';
183+
import {Button, Text} from '@gravity-ui/date-components/uikit';
184+
import {
185+
Calendar,
186+
CalendarGrid,
187+
CalendarGridHeader,
188+
CalendarGridHeaderCells,
189+
CalendarGridHeaderCell,
190+
CalendarGridBody,
191+
CalendarGridRow,
192+
CalendarGridRowHeader,
193+
CalendarGridRowCells,
194+
CalendarGridRowCell,
195+
} from '@gravity-ui/date-components';
196+
197+
<Calendar>
198+
<Flex justifyContent="space-between">
199+
<Button slot="previous" />
200+
<Text slot="heading" />
201+
<Button slot="next" />
202+
</Flex>
203+
<CalendarGrid>
204+
<CalendarGridHeader>
205+
<div role="columnheader">#</div>
206+
<CalendarGridHeaderCells>
207+
{(date) => <CalendarGridHeaderCell date={date} />}
208+
</CalendarGridHeaderCells>
209+
</CalendarGridHeader>
210+
<CalendarGridBody>
211+
<CalendarGridRow>
212+
<CalendarGridRowHeader>{({dates}) => dates[0].format('W')}</CalendarGridRowHeader>
213+
<CalendarGridRowCells>{(date) => <CalendarGridRowCell day={day} />}</CalendarGridRowCells>
214+
</CalendarGridRow>
215+
</CalendarGridBody>
216+
</CalendarGrid>
217+
</Calendar>;
218+
```
219+
177220
## Properties
178221

179222
| Name | Description | Type | Default |

0 commit comments

Comments
 (0)