1 | ### JavaScript
|
2 |
|
3 | #### Getting component class reference
|
4 |
|
5 | ##### ES2015
|
6 |
|
7 | ```javascript
|
8 | import { DatePicker } from 'carbon-components';
|
9 | ```
|
10 |
|
11 | ##### With pre-build bundle (`carbon-components.min.js`)
|
12 |
|
13 | ```javascript
|
14 | var DatePicker = CarbonComponents.DatePicker;
|
15 | ```
|
16 |
|
17 | #### Instantiating
|
18 |
|
19 | ```javascript
|
20 | // `#my-date-picker` is an element with `[data-date-picker]` attribute
|
21 | DatePicker.create(document.getElementById('my-date-picker'));
|
22 | ```
|
23 |
|
24 | #### Public Methods
|
25 |
|
26 | | Name | Params | Description |
|
27 | | --------- | ------ | -------------------- |
|
28 | | `release` | | Deletes the instance |
|
29 |
|
30 | #### Options
|
31 |
|
32 | | Option | Default Selector | Description |
|
33 | | ----------------------------- | ------------------------------- | -------------------------------------------------------- |
|
34 | | `selectorInit` | `[data-date-picker]` | Element for initializing instance |
|
35 | | `selectorDatePickerInput` | `[data-date-picker-input]` | Input element |
|
36 | | `selectorDatePickerInputFrom` | `[data-date-picker-input-from]` | For ranged calendars only: The "From date" input element |
|
37 | | `selectorDatePickerInputTo` | `[data-date-picker-input-to]` | For ranged calendars only: The "To date" input element |
|
38 | | `selectorDatePickerIcon` | `[data-date-picker-icon]` | Calendar icon |
|
39 | | `classCalendarContainer` | `.bx--date-picker__calendar` | Class selector for the calendar container |
|
40 | | `classMonth` | `.bx--date-picker__month` | Class selector for the calendar month |
|
41 | | `classWeekdays` | `.bx--date-picker__weekdays` | Class selector for the calendar weekdays |
|
42 | | `classDays` | `.bx--date-picker__days` | Class selector for the calendar days |
|
43 | | `classWeekday` | `.bx--date-picker__weekday` | Class selector for a calendar weekday |
|
44 | | `classDay` | `.bx--date-picker__day` | Class selector for a calendar day |
|
45 | | `attribType` | `data-date-picker-type` | Specifies the calendar mode (single or range) |
|
46 | | `dateFormat` | `'m/d/Y'` | The date format given to the calendar instance |
|
47 |
|
48 | The date picker is built on top of [Flatpickr](https://chmln.github.io/flatpickr/), so many of the [events](https://chmln.github.io/flatpickr/events/) and [config options](https://chmln.github.io/flatpickr/options/) that come with Flatpickr is therefore available to the date picker options. This includes methods for setting a min date, max date, disabling date(s), specifiying a range of dates, and more.
|
49 |
|
50 | ##### Example - Getting notified of date picker dropdown being closed
|
51 |
|
52 | ```javascript
|
53 | // `#my-date-picker` is an element with `[data-date-picker]` attribute
|
54 | DatePicker.create(document.getElementById('my-date-picker'), {
|
55 | onClose() {
|
56 | console.log('Date picker dropdown closed!');
|
57 | },
|
58 | });
|
59 | ```
|
60 |
|
61 | ### FAQ
|
62 |
|
63 | #### Using and understanding Date Picker
|
64 |
|
65 | There are 3 different date picker types:
|
66 |
|
67 | | Type | `data-date-picker-type` |
|
68 | | --------------------------------------- | ---------------------------------- |
|
69 | | A simple date picker without a calendar | No data attributes needed |
|
70 | | A single date picker | `[data-date-picker-type="single"]` |
|
71 | | A ranged date picker | `[data-date-picker-type="range"]` |
|
72 |
|
73 | **The simple date picker** is a text input without a calendar. You can specify the pattern for the text input to make sure the user enters the correct date format. The default regex pattern that ships with the
|
74 | simple date picker is `\d{1,2}/\d{4}` ('dd/yyyy' for short date pickers) and `\d{1,2}/\d{1,2}/\d{4}` ('dd/mm/yyyy' or mm/dd/yyyy). The simple date picker does not require any JavaScript.
|
75 |
|
76 | **The single date picker** is a text input with a calendar instance attached to it. It also ships with a calendar icon inside the input field. The calendar will open when the input is focused, and the user can both type in a date or select a day from the calendar. The single date picker requires JavaScript, so the data attributes `data-date-picker` and `data-date-picker-type="single"` are required on the parent div, and the data attribute `data-date-picker-input` is required on the input field.
|
77 |
|
78 | **The ranged date picker** has two text inputs with a ranged calendar instance attached to them. The ranged date picker requires JavaScript, so the data attributes `data-date-picker` and `data-date-picker-type="range"` are required on the parent div, and the data attributes `data-date-picker-input-from` and `data-date-picker-input-to` are required on the two input fields.
|
79 |
|
80 | #### Localization
|
81 |
|
82 | Date Picker supports localization, and you can specify the date format by overriding the component option `dateFormat`. Supported date formats are listed [here](https://chmln.github.io/flatpickr/formatting/).
|
83 |
|
84 | To localize the date picker globally, please follow [these instructions](https://chmln.github.io/flatpickr/localization/).
|