@pingux/astro 2.2.0 → 2.3.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/Calendar/Calendar.js +202 -0
- package/lib/cjs/components/Calendar/Calendar.stories.js +192 -0
- package/lib/cjs/components/Calendar/Calendar.styles.js +74 -0
- package/lib/cjs/components/Calendar/Calendar.test.js +324 -0
- package/lib/cjs/components/Calendar/CalendarCell.js +125 -0
- package/lib/cjs/components/Calendar/CalendarGrid.js +77 -0
- package/lib/cjs/components/Calendar/index.js +14 -0
- package/lib/cjs/components/DatePicker/DateField.js +316 -0
- package/lib/cjs/components/DatePicker/DatePicker.js +195 -0
- package/lib/cjs/components/DatePicker/DatePicker.stories.js +205 -0
- package/lib/cjs/components/DatePicker/DatePicker.styles.js +60 -0
- package/lib/cjs/components/DatePicker/DatePicker.test.js +570 -0
- package/lib/cjs/components/DatePicker/DateSegment.js +71 -0
- package/lib/cjs/components/DatePicker/index.js +14 -0
- package/lib/cjs/components/IconButton/IconButton.styles.js +4 -0
- package/lib/cjs/components/TextField/TextField.js +6 -2
- package/lib/cjs/components/TextField/TextField.stories.js +13 -1
- package/lib/cjs/components/TooltipTrigger/Tooltip.js +4 -6
- package/lib/cjs/components/TooltipTrigger/Tooltip.styles.js +14 -18
- package/lib/cjs/hooks/useOverlayPanelState/useOverlayPanelState.test.js +2 -2
- package/lib/cjs/index.js +132 -59
- package/lib/cjs/styles/forms/index.js +2 -0
- package/lib/cjs/styles/variants/variants.js +4 -2
- package/lib/components/Calendar/Calendar.js +190 -0
- package/lib/components/Calendar/Calendar.stories.js +171 -0
- package/lib/components/Calendar/Calendar.styles.js +66 -0
- package/lib/components/Calendar/Calendar.test.js +321 -0
- package/lib/components/Calendar/CalendarCell.js +111 -0
- package/lib/components/Calendar/CalendarGrid.js +68 -0
- package/lib/components/Calendar/index.js +1 -0
- package/lib/components/DatePicker/DateField.js +303 -0
- package/lib/components/DatePicker/DatePicker.js +183 -0
- package/lib/components/DatePicker/DatePicker.stories.js +180 -0
- package/lib/components/DatePicker/DatePicker.styles.js +51 -0
- package/lib/components/DatePicker/DatePicker.test.js +563 -0
- package/lib/components/DatePicker/DateSegment.js +58 -0
- package/lib/components/DatePicker/index.js +1 -0
- package/lib/components/IconButton/IconButton.styles.js +4 -0
- package/lib/components/TextField/TextField.js +6 -2
- package/lib/components/TextField/TextField.stories.js +11 -0
- package/lib/components/TooltipTrigger/Tooltip.js +4 -6
- package/lib/components/TooltipTrigger/Tooltip.styles.js +14 -18
- package/lib/hooks/useOverlayPanelState/useOverlayPanelState.test.js +2 -2
- package/lib/index.js +8 -0
- package/lib/styles/forms/index.js +2 -0
- package/lib/styles/variants/variants.js +3 -1
- package/package.json +7 -1
- package/NOTICE.html +0 -12399
- /package/lib/cjs/{styles/variants/callout.js → components/Callout/Callout.styles.js} +0 -0
- /package/lib/{styles/variants/callout.js → components/Callout/Callout.styles.js} +0 -0
@@ -0,0 +1,303 @@
|
|
1
|
+
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
2
|
+
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
3
|
+
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
4
|
+
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
5
|
+
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
6
|
+
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
|
7
|
+
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
|
8
|
+
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
|
9
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
10
|
+
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
11
|
+
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
|
12
|
+
var _excluded = ["buttonProps", "fieldProps", "groupProps", "groupRef", "hasFormatHelpText", "helperText", "isDisabled", "isOpen", "isReadOnly", "isRequired", "status", "datePickerState", "minValue", "maxValue", "unavailableRanges"],
|
13
|
+
_excluded2 = ["defaultValue"];
|
14
|
+
import _setTimeout from "@babel/runtime-corejs3/core-js-stable/set-timeout";
|
15
|
+
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
|
16
|
+
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
|
17
|
+
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
|
18
|
+
import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
|
19
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
20
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context4, _context5; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context4 = ownKeys(Object(source), !0)).call(_context4, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context5 = ownKeys(Object(source))).call(_context5, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
21
|
+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
22
|
+
import { FocusScope } from 'react-aria';
|
23
|
+
import { Item } from 'react-stately';
|
24
|
+
import { createCalendar, parseDate } from '@internationalized/date';
|
25
|
+
import { useDateField } from '@react-aria/datepicker';
|
26
|
+
import { useLocale } from '@react-aria/i18n';
|
27
|
+
import { VisuallyHidden } from '@react-aria/visually-hidden';
|
28
|
+
import { useDateFieldState } from '@react-stately/datepicker';
|
29
|
+
import CalendarIcon from 'mdi-react/CalendarIcon';
|
30
|
+
import PropTypes from 'prop-types';
|
31
|
+
import { useField } from '../../hooks';
|
32
|
+
import { Box, FieldHelperText, Icon, IconButton, Input, Label, Messages } from '../../index';
|
33
|
+
import statuses from '../../utils/devUtils/constants/statuses';
|
34
|
+
import DateSegment from './DateSegment';
|
35
|
+
|
36
|
+
/**
|
37
|
+
* DateFields allow users to enter and edit date using a keyboard.
|
38
|
+
* Parts of the date is displayed in editable segments
|
39
|
+
*/
|
40
|
+
import { jsx as ___EmotionJSX } from "@emotion/react";
|
41
|
+
var ARIA_LABELS_CALENDAR_BUTTON_EXPANSION = {
|
42
|
+
OPEN: 'Open the Calendar Popover',
|
43
|
+
CLOSE: 'Close the Calendar Popover'
|
44
|
+
};
|
45
|
+
var DateField = /*#__PURE__*/forwardRef(function (props, ref) {
|
46
|
+
var _labelRef$current, _context3;
|
47
|
+
var buttonProps = props.buttonProps,
|
48
|
+
fieldProps = props.fieldProps,
|
49
|
+
groupProps = props.groupProps,
|
50
|
+
groupRef = props.groupRef,
|
51
|
+
hasFormatHelpText = props.hasFormatHelpText,
|
52
|
+
helperText = props.helperText,
|
53
|
+
isDisabled = props.isDisabled,
|
54
|
+
isOpen = props.isOpen,
|
55
|
+
isReadOnly = props.isReadOnly,
|
56
|
+
isRequired = props.isRequired,
|
57
|
+
status = props.status,
|
58
|
+
datePickerState = props.datePickerState,
|
59
|
+
minValue = props.minValue,
|
60
|
+
maxValue = props.maxValue,
|
61
|
+
unavailableRanges = props.unavailableRanges,
|
62
|
+
other = _objectWithoutProperties(props, _excluded);
|
63
|
+
var _useState = useState(''),
|
64
|
+
_useState2 = _slicedToArray(_useState, 2),
|
65
|
+
errorMessage = _useState2[0],
|
66
|
+
setErrorMessage = _useState2[1];
|
67
|
+
var fieldRef = useRef();
|
68
|
+
var inputRef = useRef();
|
69
|
+
var labelRef = useRef();
|
70
|
+
|
71
|
+
// istanbul ignore next
|
72
|
+
useImperativeHandle(ref, function () {
|
73
|
+
return inputRef.current;
|
74
|
+
});
|
75
|
+
var _useLocale = useLocale(),
|
76
|
+
locale = _useLocale.locale;
|
77
|
+
var state = useDateFieldState(_objectSpread(_objectSpread({}, fieldProps), {}, {
|
78
|
+
locale: locale,
|
79
|
+
createCalendar: createCalendar
|
80
|
+
}));
|
81
|
+
var segments = state.segments;
|
82
|
+
var setValue = datePickerState.setValue;
|
83
|
+
var _useDateField = useDateField(_objectSpread({}, fieldProps), state, fieldRef),
|
84
|
+
dateFieldProps = _useDateField.fieldProps;
|
85
|
+
|
86
|
+
// removing a duplicate id, otherwise the groups element and field element each apply it
|
87
|
+
delete dateFieldProps.id;
|
88
|
+
|
89
|
+
// useDateFieldState does not return defaultValue so we separate it out to prevent control clashes
|
90
|
+
var defaultValue = props.defaultValue,
|
91
|
+
others = _objectWithoutProperties(props, _excluded2);
|
92
|
+
var _useField = useField(_objectSpread(_objectSpread({}, others), {}, {
|
93
|
+
value: state.value || ''
|
94
|
+
})),
|
95
|
+
fieldContainerProps = _useField.fieldContainerProps,
|
96
|
+
fieldControlInputProps = _useField.fieldControlInputProps,
|
97
|
+
fieldControlWrapperProps = _useField.fieldControlWrapperProps,
|
98
|
+
fieldLabelProps = _useField.fieldLabelProps;
|
99
|
+
var toggleCalendarButtonAriaLabel = isOpen ? ARIA_LABELS_CALENDAR_BUTTON_EXPANSION.OPEN : ARIA_LABELS_CALENDAR_BUTTON_EXPANSION.CLOSE;
|
100
|
+
useEffect(function () {
|
101
|
+
if (errorMessage) {
|
102
|
+
var timer = _setTimeout(function () {
|
103
|
+
setErrorMessage('');
|
104
|
+
}, 5000);
|
105
|
+
return function () {
|
106
|
+
clearTimeout(timer);
|
107
|
+
};
|
108
|
+
}
|
109
|
+
return undefined;
|
110
|
+
}, [errorMessage]);
|
111
|
+
var getKey = function getKey(segment, index) {
|
112
|
+
var _context;
|
113
|
+
return _concatInstanceProperty(_context = "".concat(segment, "-")).call(_context, index);
|
114
|
+
};
|
115
|
+
|
116
|
+
/**
|
117
|
+
* Reordering segments object for YYYY-MM-DD format
|
118
|
+
*/
|
119
|
+
var enUSSegments = [];
|
120
|
+
if (locale === 'en-US') {
|
121
|
+
do {
|
122
|
+
_forEachInstanceProperty(segments).call(segments, function (item) {
|
123
|
+
if (item.type === 'year' && enUSSegments.length === 0) {
|
124
|
+
enUSSegments.push(item);
|
125
|
+
}
|
126
|
+
if (item.type === 'literal' && enUSSegments.length === 1) {
|
127
|
+
enUSSegments.push(item);
|
128
|
+
}
|
129
|
+
if (item.type === 'month' && enUSSegments.length === 2) {
|
130
|
+
enUSSegments.push(item);
|
131
|
+
}
|
132
|
+
if (item.type === 'literal' && enUSSegments.length === 3) {
|
133
|
+
enUSSegments.push(item);
|
134
|
+
}
|
135
|
+
if (item.type === 'day' && enUSSegments.length === 4) {
|
136
|
+
enUSSegments.push(item);
|
137
|
+
}
|
138
|
+
});
|
139
|
+
} while (enUSSegments.length < 5);
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* Creates an expected date format helper text and automatically changes based on locale.
|
144
|
+
*/
|
145
|
+
|
146
|
+
var formatHelpText = useMemo(function () {
|
147
|
+
var _context2;
|
148
|
+
return _mapInstanceProperty(_context2 = locale === 'en-US' ? enUSSegments : segments).call(_context2, function (s) {
|
149
|
+
return s.text;
|
150
|
+
}).join(' ');
|
151
|
+
}, [hasFormatHelpText]);
|
152
|
+
var handlePaste = useCallback(function (e) {
|
153
|
+
var copiedValue = e.clipboardData.getData('text');
|
154
|
+
var isNextAvailableDate;
|
155
|
+
var handleSetDateValue = function handleSetDateValue(isDate) {
|
156
|
+
setValue(isDate);
|
157
|
+
};
|
158
|
+
if (locale === 'en-US' && !isDisabled && !isReadOnly) {
|
159
|
+
try {
|
160
|
+
if (copiedValue.match(/^\d{4}-\d{2}-\d{2}$/) !== null) {
|
161
|
+
var DateProps = {
|
162
|
+
isMinValue: minValue && parseDate(minValue),
|
163
|
+
isMaxValue: maxValue && parseDate(maxValue),
|
164
|
+
isValidDate: parseDate(copiedValue)
|
165
|
+
};
|
166
|
+
|
167
|
+
// pastes the min/max/isNextAvailable date if the pasted value is
|
168
|
+
// min date, beyond max date or is an unavailable date.
|
169
|
+
|
170
|
+
if (DateProps.isValidDate <= DateProps.isMinValue) {
|
171
|
+
return handleSetDateValue(DateProps.isMinValue);
|
172
|
+
}
|
173
|
+
if (DateProps.isValidDate >= DateProps.isMaxValue) {
|
174
|
+
return handleSetDateValue(DateProps.isMaxValue);
|
175
|
+
}
|
176
|
+
if (unavailableRanges) {
|
177
|
+
_mapInstanceProperty(unavailableRanges).call(unavailableRanges, function (interval) {
|
178
|
+
if (DateProps.isValidDate >= parseDate(interval[0]) && DateProps.isValidDate <= parseDate(interval[1])) {
|
179
|
+
isNextAvailableDate = parseDate(interval[1]).add({
|
180
|
+
days: 1
|
181
|
+
});
|
182
|
+
return isNextAvailableDate && setErrorMessage("Pasted value ".concat(copiedValue, " is an unavailable date"));
|
183
|
+
}
|
184
|
+
return null;
|
185
|
+
});
|
186
|
+
return handleSetDateValue(isNextAvailableDate || DateProps.isValidDate);
|
187
|
+
}
|
188
|
+
return handleSetDateValue(DateProps.isValidDate);
|
189
|
+
}
|
190
|
+
throw new Error();
|
191
|
+
} catch (err) {
|
192
|
+
setErrorMessage("Invalid Date. Paste in YYYY-MM-DD format. Pasted value ".concat(copiedValue));
|
193
|
+
}
|
194
|
+
}
|
195
|
+
return null;
|
196
|
+
}, [minValue, maxValue, unavailableRanges]);
|
197
|
+
return ___EmotionJSX(Box, _extends({
|
198
|
+
variant: "forms.input.fieldContainer"
|
199
|
+
}, fieldContainerProps), ___EmotionJSX(Label, _extends({}, fieldLabelProps, {
|
200
|
+
ref: labelRef
|
201
|
+
})), ___EmotionJSX(Box, _extends({
|
202
|
+
isRow: true,
|
203
|
+
variant: "forms.datePicker.inSlotContainer"
|
204
|
+
}, groupProps, {
|
205
|
+
ref: groupRef
|
206
|
+
}), ___EmotionJSX(Box, fieldControlWrapperProps, ___EmotionJSX(VisuallyHidden, null, ___EmotionJSX(Input, _extends({
|
207
|
+
ref: inputRef,
|
208
|
+
tabIndex: -1,
|
209
|
+
type: "date/text",
|
210
|
+
"data-testid": "date-field"
|
211
|
+
}, fieldControlInputProps, {
|
212
|
+
"aria-labelledby": labelRef === null || labelRef === void 0 ? void 0 : (_labelRef$current = labelRef.current) === null || _labelRef$current === void 0 ? void 0 : _labelRef$current.id
|
213
|
+
})))), ___EmotionJSX(Box, _extends({
|
214
|
+
isRow: true,
|
215
|
+
variant: "forms.datePicker.inputField"
|
216
|
+
}, dateFieldProps, {
|
217
|
+
ref: fieldRef
|
218
|
+
}), ___EmotionJSX(FocusScope, null, _mapInstanceProperty(_context3 = locale === 'en-US' ? enUSSegments : segments).call(_context3, function (segment, index) {
|
219
|
+
return ___EmotionJSX(DateSegment, _extends({
|
220
|
+
key: getKey(segment, index),
|
221
|
+
segment: segment,
|
222
|
+
state: state,
|
223
|
+
isDisabled: isDisabled,
|
224
|
+
isReadOnly: isReadOnly,
|
225
|
+
isRequired: isRequired,
|
226
|
+
handlePaste: handlePaste
|
227
|
+
}, other));
|
228
|
+
})), ___EmotionJSX(IconButton, _extends({
|
229
|
+
variant: "datePicker.containedIcon",
|
230
|
+
isDisabled: isDisabled || isReadOnly
|
231
|
+
}, buttonProps, {
|
232
|
+
"aria-label": toggleCalendarButtonAriaLabel
|
233
|
+
}), ___EmotionJSX(Icon, {
|
234
|
+
icon: CalendarIcon,
|
235
|
+
size: 20
|
236
|
+
})))), hasFormatHelpText && ___EmotionJSX(FieldHelperText, null, formatHelpText), errorMessage && ___EmotionJSX(Messages, {
|
237
|
+
onClose: function onClose() {
|
238
|
+
return setErrorMessage('');
|
239
|
+
}
|
240
|
+
}, ___EmotionJSX(Item, {
|
241
|
+
key: "message1",
|
242
|
+
status: "error"
|
243
|
+
}, errorMessage)));
|
244
|
+
});
|
245
|
+
DateField.propTypes = {
|
246
|
+
/** Prop to provide a custom default date (uncontrolled) */
|
247
|
+
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
248
|
+
/** Prop to provide a default date (controlled) */
|
249
|
+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
250
|
+
/** Props object that is spread directly into the calendar button element. */
|
251
|
+
buttonProps: PropTypes.shape({}),
|
252
|
+
/** Props object that is spread directly into the root (top-level) element. */
|
253
|
+
containerProps: PropTypes.shape({}),
|
254
|
+
/** state management for a date picker component */
|
255
|
+
datePickerState: PropTypes.shape({
|
256
|
+
setValue: PropTypes.func
|
257
|
+
}),
|
258
|
+
/** @ignore Props object that is spread directly into the segmented date field. */
|
259
|
+
fieldProps: PropTypes.shape({}),
|
260
|
+
/** @ignore Props passed to the box surrounding the input, button, and helper text. */
|
261
|
+
groupProps: PropTypes.shape({}),
|
262
|
+
/** @ignore Ref which is passed to the role="group" element. */
|
263
|
+
groupRef: PropTypes.shape({}),
|
264
|
+
/** Whether the input element is automatically focused when loaded onto the page. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautofocus). */
|
265
|
+
hasAutoFocus: PropTypes.bool,
|
266
|
+
/** Text rendered below the input displaying the expected date format for the user's locale. */
|
267
|
+
hasFormatHelpText: PropTypes.bool,
|
268
|
+
/** Text rendered below the input. */
|
269
|
+
helperText: PropTypes.node,
|
270
|
+
/** Whether the field is disabled. */
|
271
|
+
isDisabled: PropTypes.bool,
|
272
|
+
/** Whether the overlay is currently open. */
|
273
|
+
isOpen: PropTypes.bool,
|
274
|
+
/** Whether the input can be selected, but not changed by the user. */
|
275
|
+
isReadOnly: PropTypes.bool,
|
276
|
+
/** Whether the field is required. */
|
277
|
+
isRequired: PropTypes.bool,
|
278
|
+
/** Whether the field is quiet. */
|
279
|
+
isQuiet: PropTypes.bool,
|
280
|
+
/** Props object that is spread directly into the label element. */
|
281
|
+
labelProps: PropTypes.shape({}),
|
282
|
+
/** The maximum allowed date that a user may select. */
|
283
|
+
maxValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
284
|
+
/** The minimum allowed date that a user may select. */
|
285
|
+
minValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
286
|
+
/** Handler that is called when the element's selection state changes. */
|
287
|
+
onChange: PropTypes.func,
|
288
|
+
/** Determines the input status indicator and helper text styling. */
|
289
|
+
status: PropTypes.oneOf(_Object$values(statuses)),
|
290
|
+
/** The ranges of unavailable dates passed */
|
291
|
+
unavailableRanges: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)),
|
292
|
+
/** Props object that is spread directly into the input wrapper element. */
|
293
|
+
wrapperProps: PropTypes.shape({})
|
294
|
+
};
|
295
|
+
DateField.defaultProps = {
|
296
|
+
hasAutoFocus: false,
|
297
|
+
isDisabled: false,
|
298
|
+
isQuiet: false,
|
299
|
+
isReadOnly: false,
|
300
|
+
isRequired: false,
|
301
|
+
status: statuses.DEFAULT
|
302
|
+
};
|
303
|
+
export default DateField;
|
@@ -0,0 +1,183 @@
|
|
1
|
+
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
2
|
+
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
3
|
+
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
4
|
+
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
5
|
+
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
|
6
|
+
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
7
|
+
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
|
8
|
+
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
|
9
|
+
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
|
10
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
11
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
12
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context, _context2; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context = ownKeys(Object(source), !0)).call(_context, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context2 = ownKeys(Object(source))).call(_context2, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
13
|
+
import _someInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/some";
|
14
|
+
import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
|
15
|
+
import React, { forwardRef, useRef } from 'react';
|
16
|
+
import { parseDate } from '@internationalized/date';
|
17
|
+
import { useDatePicker } from '@react-aria/datepicker';
|
18
|
+
import { FocusScope } from '@react-aria/focus';
|
19
|
+
import { useOverlayPosition } from '@react-aria/overlays';
|
20
|
+
import { mergeProps } from '@react-aria/utils';
|
21
|
+
import { useDatePickerState } from '@react-stately/datepicker';
|
22
|
+
import PropTypes from 'prop-types';
|
23
|
+
import { Calendar, FieldHelperText, PopoverContainer } from '../../index';
|
24
|
+
import statuses from '../../utils/devUtils/constants/statuses';
|
25
|
+
import DateField from './DateField';
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Console Warning: The state update warning is a known issue coming from the react-aria library.
|
29
|
+
* The `is-selected` class is added to the button after the component unmounts.
|
30
|
+
*/
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Combines an DateField with a calendar to enter or select a date.
|
34
|
+
*
|
35
|
+
* Utilizes [useDatePicker](https://react-spectrum.adobe.com/react-aria/useDatePicker.html) from React
|
36
|
+
* Aria and [useDatePickerState](https://react-spectrum.adobe.com/react-stately/useDatePickerState.html)
|
37
|
+
* from React Stately.
|
38
|
+
*
|
39
|
+
* Props not specified are passed along to the underlying `DateField` component.
|
40
|
+
*/
|
41
|
+
import { jsx as ___EmotionJSX } from "@emotion/react";
|
42
|
+
var DatePicker = /*#__PURE__*/forwardRef(function (props, ref) {
|
43
|
+
var defaultValue = props.defaultValue,
|
44
|
+
hasAutoFocus = props.hasAutoFocus,
|
45
|
+
helperText = props.helperText,
|
46
|
+
isDefaultOpen = props.isDefaultOpen,
|
47
|
+
isReadOnly = props.isReadOnly,
|
48
|
+
maxValue = props.maxValue,
|
49
|
+
minValue = props.minValue,
|
50
|
+
status = props.status,
|
51
|
+
value = props.value,
|
52
|
+
unavailableRanges = props.unavailableRanges;
|
53
|
+
var groupRef = useRef();
|
54
|
+
var popoverRef = useRef();
|
55
|
+
|
56
|
+
/**
|
57
|
+
* This function is run against each date in the calendar
|
58
|
+
*/
|
59
|
+
var isDateUnavailable = function isDateUnavailable(date) {
|
60
|
+
return _someInstanceProperty(unavailableRanges).call(unavailableRanges, function (interval) {
|
61
|
+
return date.compare(parseDate(interval[0])) >= 0 && date.compare(parseDate(interval[1])) <= 0;
|
62
|
+
});
|
63
|
+
};
|
64
|
+
|
65
|
+
/**
|
66
|
+
* To accept date value as an object or as a string in format YYYY-MM-DD
|
67
|
+
*/
|
68
|
+
var parsedDates = {
|
69
|
+
value: value || value && parseDate(value),
|
70
|
+
defaultValue: defaultValue && parseDate(defaultValue),
|
71
|
+
maxValue: maxValue && parseDate(maxValue),
|
72
|
+
minValue: minValue && parseDate(minValue),
|
73
|
+
isDateUnavailable: unavailableRanges && isDateUnavailable
|
74
|
+
};
|
75
|
+
var state = useDatePickerState(_objectSpread({
|
76
|
+
autoFocus: hasAutoFocus,
|
77
|
+
defaultOpen: isDefaultOpen
|
78
|
+
}, mergeProps(props, parsedDates)));
|
79
|
+
var _useDatePicker = useDatePicker(_objectSpread({
|
80
|
+
autoFocus: hasAutoFocus
|
81
|
+
}, mergeProps(props, parsedDates)), state, groupRef),
|
82
|
+
buttonProps = _useDatePicker.buttonProps,
|
83
|
+
calendarProps = _useDatePicker.calendarProps,
|
84
|
+
dialogProps = _useDatePicker.dialogProps,
|
85
|
+
fieldProps = _useDatePicker.fieldProps,
|
86
|
+
groupProps = _useDatePicker.groupProps,
|
87
|
+
labelProps = _useDatePicker.labelProps;
|
88
|
+
var _useOverlayPosition = useOverlayPosition({
|
89
|
+
targetRef: groupRef,
|
90
|
+
overlayRef: popoverRef,
|
91
|
+
offset: 15,
|
92
|
+
crossOffset: 40,
|
93
|
+
isOpen: state.isOpen,
|
94
|
+
onClose: state.setOpen,
|
95
|
+
shouldUpdatePosition: true
|
96
|
+
}),
|
97
|
+
positionProps = _useOverlayPosition.overlayProps;
|
98
|
+
var calendar = !isReadOnly && ___EmotionJSX(PopoverContainer, _extends({
|
99
|
+
hasNoArrow: true,
|
100
|
+
isDismissable: true,
|
101
|
+
isNonModal: true,
|
102
|
+
ref: popoverRef,
|
103
|
+
isOpen: state.isOpen,
|
104
|
+
onClose: state.setOpen
|
105
|
+
}, mergeProps(dialogProps, positionProps)), ___EmotionJSX(FocusScope, {
|
106
|
+
autoFocus: true,
|
107
|
+
contain: true,
|
108
|
+
restoreFocus: true
|
109
|
+
}, ___EmotionJSX(Calendar, calendarProps)));
|
110
|
+
return ___EmotionJSX(React.Fragment, null, ___EmotionJSX(DateField, _extends({}, props, {
|
111
|
+
ref: ref,
|
112
|
+
buttonProps: buttonProps,
|
113
|
+
fieldProps: fieldProps,
|
114
|
+
groupProps: groupProps,
|
115
|
+
groupRef: groupRef,
|
116
|
+
helperText: helperText,
|
117
|
+
labelProps: labelProps,
|
118
|
+
isOpen: state.isOpen,
|
119
|
+
status: status,
|
120
|
+
datePickerState: state
|
121
|
+
})), calendar, helperText && ___EmotionJSX(FieldHelperText, {
|
122
|
+
status: status
|
123
|
+
}, helperText));
|
124
|
+
});
|
125
|
+
DatePicker.propTypes = {
|
126
|
+
/** Prop to provide a custom default date (uncontrolled) */
|
127
|
+
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
128
|
+
/** Prop to provide a default date (controlled) */
|
129
|
+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
130
|
+
/** Whether the element should receive focus on render. */
|
131
|
+
hasAutoFocus: PropTypes.bool,
|
132
|
+
/** Text rendered below the input displaying the expected date format for the user's locale. */
|
133
|
+
hasFormatHelpText: PropTypes.bool,
|
134
|
+
/** Text rendered below the input. */
|
135
|
+
helperText: PropTypes.string,
|
136
|
+
/** The element's unique identifier. */
|
137
|
+
id: PropTypes.string,
|
138
|
+
/**
|
139
|
+
* Callback that is called for each date of the calendar.
|
140
|
+
* If it returns true, then the date is unavailable.
|
141
|
+
*
|
142
|
+
* (date: DateValue) => boolean
|
143
|
+
*/
|
144
|
+
isDateUnavailable: PropTypes.func,
|
145
|
+
/** Sets the default open state of the overlay (uncontrolled). */
|
146
|
+
isDefaultOpen: PropTypes.bool,
|
147
|
+
/** Whether the calendar is disabled. */
|
148
|
+
isDisabled: PropTypes.bool,
|
149
|
+
/** Whether the overlay is currently open (controlled). */
|
150
|
+
isOpen: PropTypes.bool,
|
151
|
+
/** Whether the calendar dates are only focusable. */
|
152
|
+
isReadOnly: PropTypes.bool,
|
153
|
+
/** Whether user input is required on the input before form submission. */
|
154
|
+
isRequired: PropTypes.bool,
|
155
|
+
/** The rendered label for the field. */
|
156
|
+
label: PropTypes.node,
|
157
|
+
/** The maximum allowed date that a user may select. */
|
158
|
+
maxValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
159
|
+
/** The minimum allowed date that a user may select. */
|
160
|
+
minValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
161
|
+
/** Handler that is called when the element loses focus. */
|
162
|
+
onBlur: PropTypes.func,
|
163
|
+
/** Handler that is called when the element's selection state changes. */
|
164
|
+
onChange: PropTypes.func,
|
165
|
+
/** Handler that is called when the element receives focus. */
|
166
|
+
onFocus: PropTypes.func,
|
167
|
+
/** Handler that is called when the element's focus status changes. */
|
168
|
+
onFocusChange: PropTypes.func,
|
169
|
+
/** Handler that is called when a key is pressed. */
|
170
|
+
onKeyDown: PropTypes.func,
|
171
|
+
/** Handler that is called when a key is released. */
|
172
|
+
onKeyUp: PropTypes.func,
|
173
|
+
/** Determines the textarea status indicator and helper text styling. */
|
174
|
+
status: PropTypes.oneOf(_Object$values(statuses)),
|
175
|
+
/** The ranges of unavailable dates passed */
|
176
|
+
unavailableRanges: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string))
|
177
|
+
};
|
178
|
+
DateField.defaultProps = {
|
179
|
+
isDisabled: false,
|
180
|
+
isReadOnly: false,
|
181
|
+
isRequired: false
|
182
|
+
};
|
183
|
+
export default DatePicker;
|
@@ -0,0 +1,180 @@
|
|
1
|
+
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
2
|
+
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
3
|
+
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
4
|
+
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
5
|
+
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
|
6
|
+
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
7
|
+
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
|
8
|
+
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
|
9
|
+
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
10
|
+
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
|
11
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
12
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
13
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context, _context2; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context = ownKeys(Object(source), !0)).call(_context, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context2 = ownKeys(Object(source))).call(_context2, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
14
|
+
import React, { useState } from 'react';
|
15
|
+
import { parseDate } from '@internationalized/date';
|
16
|
+
import { chain } from '@react-aria/utils';
|
17
|
+
import { actions } from '@storybook/addon-actions';
|
18
|
+
import { Box, Button } from '../../index';
|
19
|
+
import { ariaAttributeBaseArgTypes } from '../../utils/docUtils/ariaAttributes';
|
20
|
+
import DatePicker from './DatePicker';
|
21
|
+
import { jsx as ___EmotionJSX } from "@emotion/react";
|
22
|
+
export default {
|
23
|
+
title: 'Form/DatePicker',
|
24
|
+
component: DatePicker,
|
25
|
+
parameters: {
|
26
|
+
docs: {
|
27
|
+
source: {
|
28
|
+
type: 'code'
|
29
|
+
}
|
30
|
+
}
|
31
|
+
},
|
32
|
+
argTypes: _objectSpread({
|
33
|
+
defaultValue: {
|
34
|
+
control: {
|
35
|
+
type: 'text'
|
36
|
+
}
|
37
|
+
},
|
38
|
+
value: {
|
39
|
+
table: {
|
40
|
+
disable: true
|
41
|
+
}
|
42
|
+
},
|
43
|
+
hasAutoFocus: {
|
44
|
+
control: {
|
45
|
+
type: 'boolean'
|
46
|
+
},
|
47
|
+
defaultValue: false
|
48
|
+
},
|
49
|
+
hasFormatHelpText: {
|
50
|
+
control: {
|
51
|
+
type: 'boolean'
|
52
|
+
},
|
53
|
+
defaultValue: false
|
54
|
+
},
|
55
|
+
isDisabled: {
|
56
|
+
control: {
|
57
|
+
type: 'boolean'
|
58
|
+
},
|
59
|
+
defaultValue: false
|
60
|
+
},
|
61
|
+
isReadOnly: {
|
62
|
+
control: {
|
63
|
+
type: 'boolean'
|
64
|
+
},
|
65
|
+
defaultValue: false
|
66
|
+
},
|
67
|
+
isRequired: {
|
68
|
+
control: {
|
69
|
+
type: 'boolean'
|
70
|
+
},
|
71
|
+
defaultValue: false
|
72
|
+
},
|
73
|
+
label: {
|
74
|
+
control: {
|
75
|
+
type: 'text'
|
76
|
+
},
|
77
|
+
defaultValue: 'Example label'
|
78
|
+
},
|
79
|
+
maxValue: {
|
80
|
+
control: {
|
81
|
+
type: 'text'
|
82
|
+
}
|
83
|
+
},
|
84
|
+
minValue: {
|
85
|
+
control: {
|
86
|
+
type: 'text'
|
87
|
+
}
|
88
|
+
},
|
89
|
+
onChange: {
|
90
|
+
defaultValue: null,
|
91
|
+
action: 'handleChange'
|
92
|
+
}
|
93
|
+
}, ariaAttributeBaseArgTypes)
|
94
|
+
};
|
95
|
+
export var Default = function Default(args) {
|
96
|
+
return ___EmotionJSX(DatePicker, args);
|
97
|
+
};
|
98
|
+
export var DefaultValue = function DefaultValue(args) {
|
99
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
100
|
+
defaultValue: "2022-08-10"
|
101
|
+
}));
|
102
|
+
};
|
103
|
+
export var Controlled = function Controlled(args) {
|
104
|
+
var _useState = useState(null),
|
105
|
+
_useState2 = _slicedToArray(_useState, 2),
|
106
|
+
date = _useState2[0],
|
107
|
+
setDate = _useState2[1];
|
108
|
+
return ___EmotionJSX(React.Fragment, null, ___EmotionJSX(DatePicker, _extends({}, args, {
|
109
|
+
value: date,
|
110
|
+
onChange: chain(setDate, actions('onChange'))
|
111
|
+
})), ___EmotionJSX(Box, {
|
112
|
+
isRow: true,
|
113
|
+
mt: "sm"
|
114
|
+
}, ___EmotionJSX(Button, {
|
115
|
+
mr: "sm",
|
116
|
+
onPress: function onPress() {
|
117
|
+
return setDate(parseDate('2022-08-10'));
|
118
|
+
}
|
119
|
+
}, "Change value"), ___EmotionJSX(Button, {
|
120
|
+
onPress: function onPress() {
|
121
|
+
return setDate(null);
|
122
|
+
}
|
123
|
+
}, "Clear")));
|
124
|
+
};
|
125
|
+
export var Disabled = function Disabled(args) {
|
126
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
127
|
+
isDisabled: true
|
128
|
+
}));
|
129
|
+
};
|
130
|
+
export var ReadOnly = function ReadOnly(args) {
|
131
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
132
|
+
isReadOnly: true
|
133
|
+
}));
|
134
|
+
};
|
135
|
+
export var UnavailableDates = function UnavailableDates(args) {
|
136
|
+
var unavailableRanges = [['2022-07-29', '2022-08-05'], ['2022-08-15', '2022-08-20'], ['2022-08-25', '2022-08-26']];
|
137
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
138
|
+
unavailableRanges: unavailableRanges,
|
139
|
+
defaultValue: "2022-08-10"
|
140
|
+
}));
|
141
|
+
};
|
142
|
+
export var MinimumDate = function MinimumDate(args) {
|
143
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
144
|
+
defaultValue: "2022-08-17",
|
145
|
+
minValue: "2022-08-10"
|
146
|
+
}));
|
147
|
+
};
|
148
|
+
export var MaximumDate = function MaximumDate(args) {
|
149
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
150
|
+
defaultValue: "2022-08-03",
|
151
|
+
maxValue: "2022-08-10"
|
152
|
+
}));
|
153
|
+
};
|
154
|
+
export var Required = function Required(args) {
|
155
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
156
|
+
isRequired: true
|
157
|
+
}));
|
158
|
+
};
|
159
|
+
export var FormatHelperText = function FormatHelperText(args) {
|
160
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
161
|
+
hasFormatHelpText: true
|
162
|
+
}));
|
163
|
+
};
|
164
|
+
export var DefaultOpen = function DefaultOpen(args) {
|
165
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
166
|
+
isDefaultOpen: true
|
167
|
+
}));
|
168
|
+
};
|
169
|
+
export var Error = function Error(args) {
|
170
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
171
|
+
status: "error",
|
172
|
+
helperText: "Here is some helpful text..."
|
173
|
+
}));
|
174
|
+
};
|
175
|
+
export var Success = function Success(args) {
|
176
|
+
return ___EmotionJSX(DatePicker, _extends({}, args, {
|
177
|
+
status: "success",
|
178
|
+
helperText: "Here is some helpful text..."
|
179
|
+
}));
|
180
|
+
};
|