UNPKG

11.2 kBMarkdownView Raw
1# @availity/reactstrap-validation-date
2
3> Wrapper for react-date-range to work with availity-reactstrap-validation.
4
5[![Version](https://img.shields.io/npm/v/@availity/reactstrap-validation-date.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/reactstrap-validation-date)
6
7## Installation
8
9```bash
10npm install @availity/reactstrap-validation-date availity-reactstrap-validation reactstrap react --save
11```
12
13### Usage
14
15```javascript
16import React from 'react';
17import { Label } from 'reactstrap';
18import { AvForm, AvGroup, AvFeedback } from 'availity-reactstrap-validation';
19import AvDate, { AvDateField, AvDateRange, AvDateRangeField } from '@availity/reactstrap-validation-date';
20import '@availity/reactstrap-validation-date/styles.scss';
21// ...
22<AvForm>
23 <AvGroup>
24 <Label for="justTheDate">My Input Label</Label>
25 <AvDate name="justTheDate" required />
26 <AvFeedback>Some error message</AvFeedback>
27 </AvGroup>
28
29 <AvDateField
30 name="fieldWithLabel"
31 label="Label Made For Me"
32 required
33 />
34
35 <AvGroup>
36 <Label for="justTheDateRange">My Input Label</Label>
37 <AvDateRange name="justTheDateRange" required ranges/>
38 <AvFeedback>Some error message</AvFeedback>
39 </AvGroup>
40
41 <AvDateField
42 name="DateRangeWithLabel"
43 label="Label Made For Me"
44 required
45 />
46</AvForm>;
47```
48
49Note: the input callbacks (e.g. onChange) do not get called with an event like other reactstrap-validation component; just the value of the field. This is because the underlying date picker does not return the event in it's callbacks.
50
51### AvDate (Default export)
52
53This is the underlying date without the `AvGroup`, `Label` or `AvFeedback`
54
55#### AvDate Props
56
57See availity-reactstrap-validation for additional props, such as `name`, `validate`, `min`, `max`, and more.
58
59* **`datepicker`**: Boolean. Optional. Default: `true`. If `true`, the date picker button will be shown, clicking it activates the datepicker calendar. If `false`, only the date input field will be displayed (useful for date of birth fields).
60* **`calendarIcon`**: Node. Optional. Default: `<Icon name="calendar" />`. You can optional change the icon the calendar renders in the case you don't use the `availity-uikit` icons.
61* **`min`**: string. Optional. Minimum date to allow the datepicker and input to take. You can either pass the `min` here or in the `validate` object if you want a custom error message with it.
62* **`max`**: string. Optional. Max date to allow the datepicker and input to take. You can either pass the `max` here or in the `validate` object if you want a custom error message with it.
63
64#### AvDate Example usage
65
66```javascript
67import React from 'react';
68import { Label } from 'reactstrap';
69import { AvForm, AvGroup, AvFeedback } from 'availity-reactstrap-validation';
70import AvDate from '@availity/reactstrap-validation-date';
71// ...
72<AvForm>
73 <AvGroup>
74 <Label for="justFieldAndPicker">My Input Label</Label>
75 <AvDate
76 name="justFieldAndPicker"
77 required
78 />
79 <AvFeedback>Some error message</AvFeedback>
80 </AvGroup>
81</AvForm>;
82```
83
84### AvDateField
85
86Like `AvField`, but for dates with a date picker
87
88#### AvDateField Props
89
90See availity-reactstrap-validation for additional props, such as `name`, `validate`, `min`, `max`, and more.
91
92* **`datepicker`**: Boolean. Optional. Default: `true`. If `true`, the date picker button will be shown, clicking it activates the datepicker calendar. If `false`, only the date input field will be displayed (useful for date of birth fields).
93* **`min`**: string. Optional. Minimum date to allow the datepicker and input to take. You can either pass the `min` here or in the `validate` object if you want a custom error message with it.
94* **`max`**: string. Optional. Max date to allow the datepicker and input to take. You can either pass the `max` here or in the `validate` object if you want a custom error message with it.
95
96#### AvDateField Example usage
97
98```javascript
99import React from 'react';
100import { AvDateField } from '@availity/reactstrap-validation-date';
101// ...
102<AvForm>
103 <AvDateField
104 name="fieldWithLabel"
105 label="Label Made For Me"
106 required
107 />
108</AvForm>;
109```
110
111### AvDateRange
112
113A date range, consists of 2 fields, a start date and an end date.
114This is the underlying date-range without the `AvGroup`, `Label` or `AvFeedback`.
115
116#### AvDateRange Props
117
118See availity-reactstrap-validation for additional props, such as `name`, `validate`, `min`, `max`, and more.
119
120* **`start`**: Object. Required. and object which will be spread on the start date input. It must contain the `name` prop as required by availity-reactstrap-validation. It can contain additional validations as well.
121* **`end`**: Object. Required. and object which will be spread on the end date input. It must contain the `name` prop as required by availity-reactstrap-validation. It can contain additional validations as well.
122* **`distance`**: Object. Optional. Object containing the `min` and `max` distance the start and end dates are allowed to be apart from each other. See example below.
123* **`calendarIcon`**: Node. Optional. Default: `<Icon name="calendar" />`. You can optional change the icon the calendar renders in the case you don't use the `availity-uikit` icons.
124* **`min`**: string. Optional. Minimum date to allow the datepicker and input to take. You can either pass the `min` here or in the `validate` object if you want a custom error message with it.
125* **`max`**: string. Optional. Max date to allow the datepicker and input to take. You can either pass the `max` here or in the `validate` object if you want a custom error message with it.
126* **`ranges`**: object, boolean, array. Optional. Renders list of ranges preset to the left of the calendar
127 * boolean - `true` will render the [default ranges](./src/AvDateRange.js#L19-L45)
128 * array<string> - Will pick only the selected date ranges by name. See above default ranges for list of names. ex. ["Today"]
129 * object - list or object of ranges. structure is noted below
130* **`autoSync`**: boolean. Optional. Toggle whether the other date should be automatically synced to the selected date when focus changes. Dates are only auto synced the first time the input is touched and if the date field to auto sync is empty
131
132```js
133{
134 'Tomorrow': {
135 startDate: now => now.add(1,'day'),
136 endDate: now => now.add(1,'day')
137 }
138}
139```
140
141#### AvDateRange Example usage
142
143```javascript
144import React from 'react';
145import AvApi from '@availity/api-axios';
146import { AvResourceDate } from '@availity/reactstrap-validation-date';
147import '@availity/reactstrap-validation-date/styles.scss';
148// ...
149const avCustomResource = new AvApi({ name: 'my-custom-resource' });
150// ...
151<AvForm>
152 <AvDateRange
153 start={{name: 'date.start'}}
154 end={{name: 'date.end'}}
155 resource={avCustomResource}
156 distance={{
157 min: {
158 value: 3,
159 units: 'days'
160 },
161 max: {
162 value: 1,
163 units: 'month'
164 }
165 }}
166 ranges={{
167 'Last 10 Days': {
168 startDate: now => now.add(-9, 'd'),
169 endDate: now => now,
170 },
171 'Last 20 Days': {
172 startDate: now => now.add(-19, 'd'),
173 endDate: now => now,
174 },
175 'Tomorrow': {
176 startDate: now => now.add(1, 'd'),
177 endDate: now => now.add(1, 'd'),
178 },
179 }}
180 required
181 />
182</AvForm>;
183```
184
185### AvDateRangeField
186
187Like `AvField`, but for a date range with a date range picker
188
189#### AvDateRangeField Props
190
191See availity-reactstrap-validation for additional props, such as `name`, `label`, `validate`, `min`, `max`, and more.
192
193* **`start`**: Object. Required. and object which will be spread on the start date input. It must contain the `name` prop as required by availity-reactstrap-validation. It can contain additional validations as well.
194* **`end`**: Object. Required. and object which will be spread on the end date input. It must contain the `name` prop as required by availity-reactstrap-validation. It can contain additional validations as well.
195* **`distance`**: Object. Optional. Object containing the `min` and `max` distance the start and end dates are allowed to be apart from each other. See example below. Note that these values are relative to today's date, not the min/max props.
196* **`defaultValues`**: Object. Optional. Object containing the `start` and `end`, the prefilled start and end dates. See example below. Note that these values are relative to today's date, not the min/max props.
197* **`min`**: string. Optional. Minimum date to allow the datepicker and input to take. You can either pass the `min` here or in the `validate` object if you want a custom error message with it.
198* **`max`**: string. Optional. Max date to allow the datepicker and input to take. You can either pass the `max` here or in the `validate` object if you want a custom error message with it.
199* **`autoSync`**: boolean. Optional. Toggle whether the other date should be automatically synced to the selected date when focus changes. Dates are only auto synced the first time the input is touched and if the date field to auto sync is empty
200
201Make sure to import the SCSS as well!
202
203#### AvDateRangeField Example usage
204
205```javascript
206import React from 'react';
207import {AvDateRangeField} from '@availity/reactstrap-validation-date';
208import { AvForm } from 'availity-reactstrap-validation';
209import '@availity/reactstrap-validation-date/styles.scss';
210// ...
211<AvForm>
212 <AvDateRangeField
213 name="dateRange" //required
214 start={{name: 'date.start'}}
215 end={{name: 'date.end'}}
216 ranges
217 />
218</AvForm>
219```
220
221#### AvDateRangeField Comprehensive Example
222
223This example contains Min/Max dates, default values, and distance requirements for the selected dates.
224
225```javascript
226import React from 'react';
227import moment from 'moment';
228import {AvDateRangeField} from '@availity/reactstrap-validation-date';
229import { AvForm } from 'availity-reactstrap-validation';
230import '@availity/reactstrap-validation-date/styles.scss';
231// ...
232
233// set max date to 5 days before today
234const maxDate = moment();
235maxDate.dayOfYear(maxDate.dayOfYear() - 5);
236const maxDateString = maxDate.format('YYYY-MM-DD');
237// ...
238<AvForm>
239 <AvDateRangeField
240 name="dateRange" //required
241 label="Dates of Service"
242 min="2019-01-01"
243 max={maxDateString}
244 start={{name: 'date.start'}}
245 end={{name: 'date.end'}}
246 distance={{
247 min: {
248 value: 2,
249 units: 'days'
250 },
251 max: {
252 value: 1,
253 units: 'month'
254 }
255 }}
256 defaultValues={{
257 start: {
258 value: -14,
259 units: 'days'
260 },
261 end: {
262 value: -7, units: 'days'
263 }
264 }}
265 required
266 />
267</AvForm>
268```