UNPKG

3.97 kBMarkdownView Raw
1@# Date picker
2
3A `DatePicker` shows a monthly calendar and allows the user to choose a single date.
4
5`DatePicker` is built on top of the [**react-day-picker**](https://github.com/gpbl/react-day-picker) library.
6
7@reactExample DatePickerExample
8
9@## Props
10
11`DatePicker` supports both controlled and uncontrolled usage. You can control
12the selected day by setting the `value` prop, or use the component in
13uncontrolled mode and specify an initial day by setting `defaultValue`. Use the
14`onChange` prop to listen for changes to the selected day.
15
16Some props are managed by the `DatePicker` component, while others are passed
17to the **react-day-picker** library. These passed props are documented in full
18in the [**react-day-picker** documentation](http://www.gpbl.org/react-day-picker/index.html).
19
20@interface IDatePickerProps
21
22@## Shortcuts
23
24The menu on the left of the calendars provides "shortcuts" that allow users to
25quickly select common dates. The items in this menu are controlled through
26the `shortcuts` prop: `true` to show presets, `false` to hide (default), or an
27array of `IDatePickerShortcut` objects to define custom shortcuts.
28
29The **preset shortcuts** can be seen in the example above. They are as follows:
30
31- Today
32- Yesterday
33- 1 week ago
34- 1 month ago
35- 3 months ago
36- 1 year ago
37
38**Custom shortcuts** use the following interface:
39
40@interface IDatePickerShortcut
41
42@## Modifiers
43
44You can use the `modifiers` prop to conditionally apply styles to days.
45Modifiers are a react-day-picker concept and are documented in full in the
46[**react-day-picker** documentation](http://react-day-picker.js.org/docs/matching-days).
47
48The example below creates a `DatePicker` that prevents the user from selecting any Sundays,
49by using the component in controlled mode and with the `modifiers` prop:
50
51```css.scss
52// in CSS
53.#{$ns}-datepicker .DayPicker-Day--isSunday {
54 // CSS rules to make the day appear disabled
55}
56```
57
58```tsx
59// in TypeScript
60export class DatePickerExample extends React.Component<{}, { selectedDate: Date }> {
61 public state = { selectedDate: new Date() };
62
63 public render() {
64 // name of modifier function becomes the suffix for the CSS class above
65 const modifiers = { isSunday };
66 return (
67 <DatePicker
68 modifiers={modifiers}
69 onChange={newDate => this.handleChange(newDate)}
70 value={this.state.selectedDate}
71 />
72 );
73 }
74
75 private handleChange(date: Date) {
76 if (!isSunday(date)) {
77 this.setState({ selectedDate: date });
78 }
79 }
80}
81
82function isSunday(date: Date) {
83 return date.getDay() === 0;
84}
85```
86
87@## Localization
88
89`DatePicker`, `DateRangePicker`, `DateInput`, and `DateRangeInput` all support localization using an interface defined in the
90`react-day-picker` module:
91
92```tsx
93import { LocaleUtils } from "react-day-picker";
94```
95
96By supplying a `locale: string` and `localeUtils: LocaleUtils` prop to these Blueprint components, you can
97customize how dates are rendered, which day of the week is the first column, etc.
98You will need to overwrite the functions of `LocaleUtil` by your own.
99[See the interface definition for more details](https://github.com/gpbl/react-day-picker/blob/v7.3.0/types/utils.d.ts#L5).
100
101Although `@blueprintjs/datetime` and `react-day-picker` do not explicitly require `moment.js` as a dependency,
102you may wish to use Moment's implementation of localization so that you do not have to write these functions yourself.
103
104To use moment for your localization, make sure to include `moment` in your dependencies and use `MomentLocaleUtils`
105from `react-day-picker/moment` as follow:
106
107```tsx
108import MomentLocaleUtils from "react-day-picker/moment";
109import "moment/locale/fr";
110
111<DatePicker locale="fr" localeUtils={MomentLocaleUtils} />;
112```
113
114More detailed examples can be found in the [**react-day-picker** documentation](https://react-day-picker.js.org/docs/localization/).