@atlaskit/datetime-picker 15.13.0 → 16.0.0
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/CHANGELOG.md +24 -0
- package/dist/cjs/components/date-picker-class.js +685 -0
- package/dist/cjs/components/date-picker-fc.js +639 -0
- package/dist/cjs/components/date-picker.js +7 -639
- package/dist/cjs/components/date-time-picker-class.js +436 -0
- package/dist/cjs/components/date-time-picker-fc.js +394 -0
- package/dist/cjs/components/date-time-picker.js +9 -379
- package/dist/cjs/components/time-picker.js +2 -2
- package/dist/cjs/internal/ff-component.js +58 -0
- package/dist/es2019/components/date-picker-class.js +649 -0
- package/dist/es2019/components/date-picker-fc.js +563 -0
- package/dist/es2019/components/date-picker.js +5 -570
- package/dist/es2019/components/date-time-picker-class.js +400 -0
- package/dist/es2019/components/date-time-picker-fc.js +354 -0
- package/dist/es2019/components/date-time-picker.js +6 -347
- package/dist/es2019/components/time-picker.js +2 -2
- package/dist/es2019/internal/ff-component.js +47 -0
- package/dist/esm/components/date-picker-class.js +680 -0
- package/dist/esm/components/date-picker-fc.js +630 -0
- package/dist/esm/components/date-picker.js +7 -637
- package/dist/esm/components/date-time-picker-class.js +434 -0
- package/dist/esm/components/date-time-picker-fc.js +384 -0
- package/dist/esm/components/date-time-picker.js +8 -375
- package/dist/esm/components/time-picker.js +2 -2
- package/dist/esm/internal/ff-component.js +49 -0
- package/dist/types/components/date-picker-class.d.ts +110 -0
- package/dist/types/components/date-picker-fc.d.ts +20 -0
- package/dist/types/components/date-picker.d.ts +19 -18
- package/dist/types/components/date-time-picker-class.d.ts +85 -0
- package/dist/types/components/date-time-picker-fc.d.ts +15 -0
- package/dist/types/components/date-time-picker.d.ts +31 -14
- package/dist/types/internal/ff-component.d.ts +34 -0
- package/dist/types-ts4.5/components/date-picker-class.d.ts +110 -0
- package/dist/types-ts4.5/components/date-picker-fc.d.ts +20 -0
- package/dist/types-ts4.5/components/date-picker.d.ts +19 -18
- package/dist/types-ts4.5/components/date-time-picker-class.d.ts +85 -0
- package/dist/types-ts4.5/components/date-time-picker-fc.d.ts +15 -0
- package/dist/types-ts4.5/components/date-time-picker.d.ts +31 -14
- package/dist/types-ts4.5/internal/ff-component.d.ts +34 -0
- package/package.json +31 -28
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
3
|
+
/**
|
|
4
|
+
* @jsxRuntime classic
|
|
5
|
+
* @jsx jsx
|
|
6
|
+
*/
|
|
7
|
+
import { Component, createRef } from 'react';
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
|
|
10
|
+
import { css, jsx } from '@emotion/react';
|
|
11
|
+
import { isValid, parseISO } from 'date-fns';
|
|
12
|
+
import { createAndFireEvent, withAnalyticsContext, withAnalyticsEvents } from '@atlaskit/analytics-next';
|
|
13
|
+
import { IconButton } from '@atlaskit/button/new';
|
|
14
|
+
import { IdProvider } from '@atlaskit/ds-lib/use-id';
|
|
15
|
+
import CalendarIcon from '@atlaskit/icon/core/migration/calendar';
|
|
16
|
+
import { createLocalizationProvider } from '@atlaskit/locale';
|
|
17
|
+
import { fg } from '@atlaskit/platform-feature-flags';
|
|
18
|
+
import { Box, xcss } from '@atlaskit/primitives';
|
|
19
|
+
import Select, { mergeStyles } from '@atlaskit/select';
|
|
20
|
+
import { EmptyComponent } from '../internal';
|
|
21
|
+
import { formatDate, getParsedISO, getPlaceholder, isDateDisabled, parseDate } from '../internal/date-picker-migration';
|
|
22
|
+
import { IndicatorsContainer } from '../internal/indicators-container';
|
|
23
|
+
import { Menu } from '../internal/menu';
|
|
24
|
+
import { getSafeCalendarValue, getShortISOString } from '../internal/parse-date';
|
|
25
|
+
import { makeSingleValue } from '../internal/single-value';
|
|
26
|
+
const packageName = "@atlaskit/datetime-picker";
|
|
27
|
+
const packageVersion = "16.0.0";
|
|
28
|
+
const dropdownIndicatorStyles = xcss({
|
|
29
|
+
minWidth: "var(--ds-space-300, 24px)",
|
|
30
|
+
minHeight: "var(--ds-space-300, 24px)",
|
|
31
|
+
display: 'flex',
|
|
32
|
+
alignItems: 'center',
|
|
33
|
+
justifyContent: 'center'
|
|
34
|
+
});
|
|
35
|
+
const datePickerDefaultProps = {
|
|
36
|
+
defaultIsOpen: false,
|
|
37
|
+
defaultValue: '',
|
|
38
|
+
disabled: [],
|
|
39
|
+
disabledDateFilter: _ => false,
|
|
40
|
+
locale: 'en-US',
|
|
41
|
+
// These disables are here for proper typing when used as defaults. They
|
|
42
|
+
// should *not* use the `noop` function.
|
|
43
|
+
/* eslint-disable @repo/internal/react/use-noop */
|
|
44
|
+
onBlur: _event => {},
|
|
45
|
+
onChange: _value => {},
|
|
46
|
+
onFocus: _event => {}
|
|
47
|
+
/* eslint-enable @repo/internal/react/use-noop */
|
|
48
|
+
// Not including a default prop for value as it will
|
|
49
|
+
// Make the component a controlled component
|
|
50
|
+
};
|
|
51
|
+
const pickerContainerStyles = css({
|
|
52
|
+
position: 'relative'
|
|
53
|
+
});
|
|
54
|
+
const iconContainerStyles = xcss({
|
|
55
|
+
display: 'flex',
|
|
56
|
+
height: '100%',
|
|
57
|
+
position: 'absolute',
|
|
58
|
+
alignItems: 'center',
|
|
59
|
+
flexBasis: 'inherit',
|
|
60
|
+
color: 'color.text.subtlest',
|
|
61
|
+
insetBlockStart: 'space.0',
|
|
62
|
+
insetInlineEnd: 'space.0',
|
|
63
|
+
transition: `color 150ms`,
|
|
64
|
+
':hover': {
|
|
65
|
+
color: 'color.text.subtle'
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const iconSpacingWithClearButtonStyles = xcss({
|
|
69
|
+
marginInlineEnd: 'space.400'
|
|
70
|
+
});
|
|
71
|
+
const iconSpacingWithoutClearButtonStyles = xcss({
|
|
72
|
+
marginInlineEnd: 'space.050'
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// eslint-disable-next-line @repo/internal/react/no-class-components
|
|
76
|
+
class DatePickerComponent extends Component {
|
|
77
|
+
constructor(props) {
|
|
78
|
+
var _this$props$selectPro2;
|
|
79
|
+
super(props);
|
|
80
|
+
_defineProperty(this, "containerRef", null);
|
|
81
|
+
_defineProperty(this, "calendarRef", /*#__PURE__*/createRef());
|
|
82
|
+
_defineProperty(this, "calendarButtonRef", /*#__PURE__*/createRef());
|
|
83
|
+
// All state needs to be accessed via this function so that the state is mapped from props
|
|
84
|
+
// correctly to allow controlled/uncontrolled usage.
|
|
85
|
+
_defineProperty(this, "getValue", () => {
|
|
86
|
+
var _this$props$value;
|
|
87
|
+
return (_this$props$value = this.props.value) !== null && _this$props$value !== void 0 ? _this$props$value : this.state.value;
|
|
88
|
+
});
|
|
89
|
+
_defineProperty(this, "getIsOpen", () => {
|
|
90
|
+
var _this$props$isOpen;
|
|
91
|
+
return (_this$props$isOpen = this.props.isOpen) !== null && _this$props$isOpen !== void 0 ? _this$props$isOpen : this.state.isOpen;
|
|
92
|
+
});
|
|
93
|
+
_defineProperty(this, "onCalendarChange", ({
|
|
94
|
+
iso
|
|
95
|
+
}) => {
|
|
96
|
+
this.setState({
|
|
97
|
+
calendarValue: getParsedISO({
|
|
98
|
+
iso
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
_defineProperty(this, "onCalendarSelect", ({
|
|
103
|
+
iso
|
|
104
|
+
}) => {
|
|
105
|
+
this.setState({
|
|
106
|
+
selectInputValue: '',
|
|
107
|
+
isOpen: false,
|
|
108
|
+
calendarValue: iso,
|
|
109
|
+
value: iso,
|
|
110
|
+
wasOpenedFromCalendarButton: false
|
|
111
|
+
});
|
|
112
|
+
this.props.onChange(iso);
|
|
113
|
+
|
|
114
|
+
// Yes, this is not ideal. The alternative is to be able to place a ref
|
|
115
|
+
// on the inner input of Select itself, which would require a lot of
|
|
116
|
+
// extra stuff in the Select component for only this one thing. While
|
|
117
|
+
// this would be more "React-y", it doesn't seem to pose any other
|
|
118
|
+
// benefits. Performance-wise, we are only searching within the
|
|
119
|
+
// container, so it's quick.
|
|
120
|
+
if (this.state.wasOpenedFromCalendarButton) {
|
|
121
|
+
var _this$calendarButtonR;
|
|
122
|
+
(_this$calendarButtonR = this.calendarButtonRef.current) === null || _this$calendarButtonR === void 0 ? void 0 : _this$calendarButtonR.focus();
|
|
123
|
+
} else {
|
|
124
|
+
var _this$containerRef;
|
|
125
|
+
const innerCombobox = (_this$containerRef = this.containerRef) === null || _this$containerRef === void 0 ? void 0 : _this$containerRef.querySelector('[role="combobox"]');
|
|
126
|
+
innerCombobox === null || innerCombobox === void 0 ? void 0 : innerCombobox.focus();
|
|
127
|
+
}
|
|
128
|
+
this.setState({
|
|
129
|
+
isOpen: false
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
_defineProperty(this, "onInputClick", () => {
|
|
133
|
+
if (!this.props.isDisabled && !this.getIsOpen()) {
|
|
134
|
+
this.setState({
|
|
135
|
+
isOpen: true,
|
|
136
|
+
wasOpenedFromCalendarButton: false
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
_defineProperty(this, "onContainerBlur", event => {
|
|
141
|
+
var _this$containerRef2;
|
|
142
|
+
const newlyFocusedElement = event.relatedTarget;
|
|
143
|
+
if (!((_this$containerRef2 = this.containerRef) !== null && _this$containerRef2 !== void 0 && _this$containerRef2.contains(newlyFocusedElement))) {
|
|
144
|
+
this.setState({
|
|
145
|
+
isOpen: false,
|
|
146
|
+
shouldSetFocusOnCurrentDay: false,
|
|
147
|
+
wasOpenedFromCalendarButton: false
|
|
148
|
+
});
|
|
149
|
+
this.props.onBlur(event);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
_defineProperty(this, "onContainerFocus", () => {
|
|
153
|
+
this.setState({
|
|
154
|
+
shouldSetFocusOnCurrentDay: false
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
_defineProperty(this, "onSelectBlur", event => {
|
|
158
|
+
var _this$containerRef3;
|
|
159
|
+
const newlyFocusedElement = event.relatedTarget;
|
|
160
|
+
if (this.state.clearingFromIcon) {
|
|
161
|
+
// Don't close menu if blurring after the user has clicked clear
|
|
162
|
+
this.setState({
|
|
163
|
+
clearingFromIcon: false
|
|
164
|
+
});
|
|
165
|
+
} else if (!((_this$containerRef3 = this.containerRef) !== null && _this$containerRef3 !== void 0 && _this$containerRef3.contains(newlyFocusedElement))) {
|
|
166
|
+
// Don't close menu if focus is staying within the date picker's
|
|
167
|
+
// container. Makes keyboard accessibility of calendar possible
|
|
168
|
+
this.setState({
|
|
169
|
+
isOpen: false,
|
|
170
|
+
isFocused: false,
|
|
171
|
+
wasOpenedFromCalendarButton: false
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
_defineProperty(this, "onSelectFocus", event => {
|
|
176
|
+
const {
|
|
177
|
+
clearingFromIcon
|
|
178
|
+
} = this.state;
|
|
179
|
+
const value = this.getValue();
|
|
180
|
+
if (clearingFromIcon) {
|
|
181
|
+
// Don't open menu if focussing after the user has clicked clear
|
|
182
|
+
this.setState({
|
|
183
|
+
clearingFromIcon: false
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
this.setState({
|
|
187
|
+
// Don't open when focused into via keyboard if the calendar button is present
|
|
188
|
+
isOpen: !this.props.shouldShowCalendarButton,
|
|
189
|
+
calendarValue: value,
|
|
190
|
+
isFocused: true,
|
|
191
|
+
wasOpenedFromCalendarButton: false
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
this.props.onFocus(event);
|
|
195
|
+
});
|
|
196
|
+
_defineProperty(this, "onTextInput", event => {
|
|
197
|
+
const inputValue = event.target.value;
|
|
198
|
+
if (inputValue) {
|
|
199
|
+
const parsed = parseDate(inputValue, {
|
|
200
|
+
parseInputValue: this.props.parseInputValue,
|
|
201
|
+
dateFormat: this.props.dateFormat,
|
|
202
|
+
l10n: this.state.l10n
|
|
203
|
+
});
|
|
204
|
+
// Only try to set the date if we have month & day
|
|
205
|
+
if (parsed && isValid(parsed)) {
|
|
206
|
+
// We format the parsed date to YYYY-MM-DD here because
|
|
207
|
+
// this is the format expected by the @atlaskit/calendar component
|
|
208
|
+
this.setState({
|
|
209
|
+
calendarValue: getShortISOString(parsed)
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
this.setState({
|
|
214
|
+
isOpen: true,
|
|
215
|
+
wasOpenedFromCalendarButton: false
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
_defineProperty(this, "onInputKeyDown", event => {
|
|
219
|
+
const {
|
|
220
|
+
calendarValue
|
|
221
|
+
} = this.state;
|
|
222
|
+
const value = this.getValue();
|
|
223
|
+
const keyPressed = event.key.toLowerCase();
|
|
224
|
+
|
|
225
|
+
// If the input is focused and the calendar is not visible, handle space and enter clicks
|
|
226
|
+
if (!this.state.isOpen && (keyPressed === 'enter' || keyPressed === ' ')) {
|
|
227
|
+
this.setState({
|
|
228
|
+
isOpen: true,
|
|
229
|
+
wasOpenedFromCalendarButton: false
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
switch (keyPressed) {
|
|
233
|
+
case 'escape':
|
|
234
|
+
// Yes, this is not ideal. The alternative is to be able to place a ref
|
|
235
|
+
// on the inner input of Select itself, which would require a lot of
|
|
236
|
+
// extra stuff in the Select component for only this one thing. While
|
|
237
|
+
// this would be more "React-y", it doesn't seem to pose any other
|
|
238
|
+
// benefits. Performance-wise, we are only searching within the
|
|
239
|
+
// container, so it's quick.
|
|
240
|
+
if (this.state.wasOpenedFromCalendarButton) {
|
|
241
|
+
var _this$calendarButtonR2;
|
|
242
|
+
(_this$calendarButtonR2 = this.calendarButtonRef.current) === null || _this$calendarButtonR2 === void 0 ? void 0 : _this$calendarButtonR2.focus();
|
|
243
|
+
} else {
|
|
244
|
+
var _this$containerRef4;
|
|
245
|
+
const innerCombobox = (_this$containerRef4 = this.containerRef) === null || _this$containerRef4 === void 0 ? void 0 : _this$containerRef4.querySelector('[role="combobox"]');
|
|
246
|
+
innerCombobox === null || innerCombobox === void 0 ? void 0 : innerCombobox.focus();
|
|
247
|
+
}
|
|
248
|
+
this.setState({
|
|
249
|
+
isOpen: false,
|
|
250
|
+
shouldSetFocusOnCurrentDay: false,
|
|
251
|
+
wasOpenedFromCalendarButton: false
|
|
252
|
+
});
|
|
253
|
+
break;
|
|
254
|
+
case 'backspace':
|
|
255
|
+
case 'delete':
|
|
256
|
+
{
|
|
257
|
+
const inputCount = 0;
|
|
258
|
+
if (value && event.target instanceof HTMLInputElement && event.target.value.length <= inputCount) {
|
|
259
|
+
// If being cleared from keyboard, don't change behaviour
|
|
260
|
+
this.setState({
|
|
261
|
+
clearingFromIcon: false,
|
|
262
|
+
value: ''
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case 'enter':
|
|
268
|
+
if (!this.state.isOpen) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
// Prevent form submission when a date is selected
|
|
272
|
+
// using enter. See https://product-fabric.atlassian.net/browse/DSP-2501
|
|
273
|
+
// for more details.
|
|
274
|
+
event.preventDefault();
|
|
275
|
+
if (!isDateDisabled(calendarValue, {
|
|
276
|
+
disabled: this.props.disabled
|
|
277
|
+
})) {
|
|
278
|
+
// Get a safe `calendarValue` in case the value exceeds the maximum
|
|
279
|
+
// allowed by ISO 8601
|
|
280
|
+
const safeCalendarValue = getSafeCalendarValue(calendarValue);
|
|
281
|
+
const valueChanged = safeCalendarValue !== value;
|
|
282
|
+
this.setState({
|
|
283
|
+
selectInputValue: '',
|
|
284
|
+
isOpen: false,
|
|
285
|
+
value: safeCalendarValue,
|
|
286
|
+
calendarValue: safeCalendarValue,
|
|
287
|
+
wasOpenedFromCalendarButton: false
|
|
288
|
+
});
|
|
289
|
+
if (valueChanged) {
|
|
290
|
+
this.props.onChange(safeCalendarValue);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
case 'arrowdown':
|
|
295
|
+
case 'arrowup':
|
|
296
|
+
if (!this.state.shouldSetFocusOnCurrentDay) {
|
|
297
|
+
this.setState({
|
|
298
|
+
isOpen: true,
|
|
299
|
+
shouldSetFocusOnCurrentDay: true
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
default:
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
_defineProperty(this, "onCalendarButtonKeyDown", e => {
|
|
308
|
+
// Don't allow an arrow up or down to open the menu, since the input key
|
|
309
|
+
// down handler is actually on the parent.
|
|
310
|
+
if (e.type === 'keydown' && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
311
|
+
e.stopPropagation();
|
|
312
|
+
}
|
|
313
|
+
// We want to stop this from triggering other keydown events, particularly
|
|
314
|
+
// for space and enter presses. Otherwise, it opens and then closes
|
|
315
|
+
// immediately.
|
|
316
|
+
if (e.type === 'keydown' && (e.key === ' ' || e.key === 'Enter')) {
|
|
317
|
+
e.stopPropagation();
|
|
318
|
+
this.setState({
|
|
319
|
+
isKeyDown: true,
|
|
320
|
+
wasOpenedFromCalendarButton: true
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
// This event handler is triggered from both keydown and click. It's weird.
|
|
325
|
+
_defineProperty(this, "onCalendarButtonClick", e => {
|
|
326
|
+
this.setState({
|
|
327
|
+
isOpen: !this.state.isOpen,
|
|
328
|
+
wasOpenedFromCalendarButton: true
|
|
329
|
+
}, () => {
|
|
330
|
+
var _this$calendarRef, _this$calendarRef$cur, _this$calendarRef$cur2;
|
|
331
|
+
// We don't want the focus to move if this is a click event
|
|
332
|
+
if (!this.state.isKeyDown) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
this.setState({
|
|
336
|
+
isKeyDown: false
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// Focus on the first button within the calendar
|
|
340
|
+
(_this$calendarRef = this.calendarRef) === null || _this$calendarRef === void 0 ? void 0 : (_this$calendarRef$cur = _this$calendarRef.current) === null || _this$calendarRef$cur === void 0 ? void 0 : (_this$calendarRef$cur2 = _this$calendarRef$cur.querySelector('button')) === null || _this$calendarRef$cur2 === void 0 ? void 0 : _this$calendarRef$cur2.focus();
|
|
341
|
+
});
|
|
342
|
+
e.stopPropagation();
|
|
343
|
+
});
|
|
344
|
+
_defineProperty(this, "onClear", () => {
|
|
345
|
+
let changedState = {
|
|
346
|
+
value: '',
|
|
347
|
+
calendarValue: this.props.defaultValue || getShortISOString(new Date())
|
|
348
|
+
};
|
|
349
|
+
if (!this.props.hideIcon) {
|
|
350
|
+
changedState = {
|
|
351
|
+
...changedState,
|
|
352
|
+
clearingFromIcon: true
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
this.setState(changedState);
|
|
356
|
+
this.props.onChange('');
|
|
357
|
+
});
|
|
358
|
+
// `unknown` is used because `value` is unused so it does not matter.
|
|
359
|
+
_defineProperty(this, "onSelectChange", (_value, action) => {
|
|
360
|
+
// Used for native clear event in React Select
|
|
361
|
+
// Triggered when clicking ClearIndicator or backspace with no value
|
|
362
|
+
if (action.action === 'clear') {
|
|
363
|
+
this.onClear();
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
_defineProperty(this, "handleSelectInputChange", (selectInputValue, actionMeta) => {
|
|
367
|
+
var _this$props$selectPro;
|
|
368
|
+
const onInputChange = (_this$props$selectPro = this.props.selectProps) === null || _this$props$selectPro === void 0 ? void 0 : _this$props$selectPro.onInputChange;
|
|
369
|
+
if (onInputChange) {
|
|
370
|
+
onInputChange(selectInputValue, actionMeta);
|
|
371
|
+
}
|
|
372
|
+
this.setState({
|
|
373
|
+
selectInputValue
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
_defineProperty(this, "getContainerRef", ref => {
|
|
377
|
+
const oldRef = this.containerRef;
|
|
378
|
+
this.containerRef = ref;
|
|
379
|
+
// Cause a re-render if we're getting the container ref for the first time
|
|
380
|
+
// as the layered menu requires it for dimension calculation
|
|
381
|
+
if (oldRef == null && ref != null) {
|
|
382
|
+
this.forceUpdate();
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
this.state = {
|
|
386
|
+
isKeyDown: false,
|
|
387
|
+
isOpen: this.props.defaultIsOpen,
|
|
388
|
+
isFocused: false,
|
|
389
|
+
clearingFromIcon: false,
|
|
390
|
+
selectInputValue: ((_this$props$selectPro2 = this.props.selectProps) === null || _this$props$selectPro2 === void 0 ? void 0 : _this$props$selectPro2.inputValue) || '',
|
|
391
|
+
value: this.props.value || this.props.defaultValue,
|
|
392
|
+
calendarValue: this.props.value || this.props.defaultValue || getShortISOString(new Date()),
|
|
393
|
+
l10n: createLocalizationProvider(this.props.locale),
|
|
394
|
+
locale: this.props.locale,
|
|
395
|
+
shouldSetFocusOnCurrentDay: false,
|
|
396
|
+
wasOpenedFromCalendarButton: false
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
static getDerivedStateFromProps(nextProps, prevState) {
|
|
400
|
+
if (nextProps.locale !== prevState.locale) {
|
|
401
|
+
return {
|
|
402
|
+
l10n: createLocalizationProvider(nextProps.locale),
|
|
403
|
+
locale: nextProps.locale
|
|
404
|
+
};
|
|
405
|
+
} else {
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
render() {
|
|
410
|
+
const {
|
|
411
|
+
appearance = 'default',
|
|
412
|
+
'aria-describedby': ariaDescribedBy,
|
|
413
|
+
autoFocus = false,
|
|
414
|
+
clearControlLabel = 'Clear',
|
|
415
|
+
hideIcon = false,
|
|
416
|
+
openCalendarLabel = 'Open calendar',
|
|
417
|
+
disabled,
|
|
418
|
+
disabledDateFilter,
|
|
419
|
+
icon: Icon = CalendarIcon,
|
|
420
|
+
id = '',
|
|
421
|
+
innerProps = {},
|
|
422
|
+
inputLabel = 'Date picker',
|
|
423
|
+
inputLabelId,
|
|
424
|
+
isDisabled = false,
|
|
425
|
+
isInvalid = false,
|
|
426
|
+
isRequired = false,
|
|
427
|
+
label = '',
|
|
428
|
+
locale,
|
|
429
|
+
maxDate,
|
|
430
|
+
minDate,
|
|
431
|
+
name = '',
|
|
432
|
+
nextMonthLabel,
|
|
433
|
+
previousMonthLabel,
|
|
434
|
+
selectProps = {},
|
|
435
|
+
shouldShowCalendarButton,
|
|
436
|
+
spacing = 'default',
|
|
437
|
+
testId,
|
|
438
|
+
weekStartDay
|
|
439
|
+
} = this.props;
|
|
440
|
+
const {
|
|
441
|
+
calendarValue,
|
|
442
|
+
selectInputValue
|
|
443
|
+
} = this.state;
|
|
444
|
+
const value = this.getValue();
|
|
445
|
+
let actualSelectInputValue;
|
|
446
|
+
actualSelectInputValue = selectInputValue;
|
|
447
|
+
const menuIsOpen = this.getIsOpen() && !isDisabled;
|
|
448
|
+
const showClearIndicator = Boolean((value || selectInputValue) && !hideIcon);
|
|
449
|
+
let clearIndicator = Icon;
|
|
450
|
+
|
|
451
|
+
// eslint-disable-next-line @atlaskit/platform/no-preconditioning
|
|
452
|
+
if (fg('platform-visual-refresh-icons')) {
|
|
453
|
+
clearIndicator = props => jsx(Box, {
|
|
454
|
+
xcss: dropdownIndicatorStyles
|
|
455
|
+
}, jsx(Icon, props));
|
|
456
|
+
}
|
|
457
|
+
const dropDownIcon = appearance === 'subtle' || hideIcon || showClearIndicator ? null : clearIndicator;
|
|
458
|
+
const SingleValue = makeSingleValue({
|
|
459
|
+
lang: this.props.locale
|
|
460
|
+
});
|
|
461
|
+
const selectComponents = {
|
|
462
|
+
DropdownIndicator: shouldShowCalendarButton ? EmptyComponent : dropDownIcon,
|
|
463
|
+
// Only use this new container component if the calendar button is shown.
|
|
464
|
+
// Otherwise, it throws errors downstream for some reason
|
|
465
|
+
...(shouldShowCalendarButton ? {
|
|
466
|
+
IndicatorsContainer: props => jsx(IndicatorsContainer, _extends({}, props, {
|
|
467
|
+
showClearIndicator: showClearIndicator
|
|
468
|
+
}))
|
|
469
|
+
} : {}),
|
|
470
|
+
Menu,
|
|
471
|
+
SingleValue,
|
|
472
|
+
...(!showClearIndicator && {
|
|
473
|
+
ClearIndicator: EmptyComponent
|
|
474
|
+
})
|
|
475
|
+
};
|
|
476
|
+
const {
|
|
477
|
+
styles: selectStyles = {}
|
|
478
|
+
} = selectProps;
|
|
479
|
+
const disabledStyle = isDisabled ? {
|
|
480
|
+
pointerEvents: 'none',
|
|
481
|
+
color: "var(--ds-icon-disabled, inherit)"
|
|
482
|
+
} : {};
|
|
483
|
+
const calendarProps = {
|
|
484
|
+
calendarContainerRef: this.containerRef,
|
|
485
|
+
calendarDisabled: disabled,
|
|
486
|
+
calendarDisabledDateFilter: disabledDateFilter,
|
|
487
|
+
calendarMaxDate: maxDate,
|
|
488
|
+
calendarMinDate: minDate,
|
|
489
|
+
calendarRef: this.calendarRef,
|
|
490
|
+
calendarValue: value && getShortISOString(parseISO(value)),
|
|
491
|
+
calendarView: calendarValue,
|
|
492
|
+
onCalendarChange: this.onCalendarChange,
|
|
493
|
+
onCalendarSelect: this.onCalendarSelect,
|
|
494
|
+
calendarLocale: locale,
|
|
495
|
+
calendarWeekStartDay: weekStartDay,
|
|
496
|
+
shouldSetFocusOnCurrentDay: this.state.shouldSetFocusOnCurrentDay
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
// @ts-ignore -- Argument of type 'StylesConfig<OptionType, false, GroupBase<OptionType>>' is not assignable to parameter of type 'StylesConfig<OptionType, boolean, GroupBase<OptionType>>'.
|
|
500
|
+
const mergedStyles = mergeStyles(selectStyles, {
|
|
501
|
+
control: base => ({
|
|
502
|
+
...base,
|
|
503
|
+
...disabledStyle
|
|
504
|
+
}),
|
|
505
|
+
indicatorsContainer: base => ({
|
|
506
|
+
...base,
|
|
507
|
+
paddingLeft: "var(--ds-space-025, 2px)",
|
|
508
|
+
// ICON_PADDING = 2
|
|
509
|
+
paddingRight: "var(--ds-space-075, 6px)" // 8 - ICON_PADDING = 6
|
|
510
|
+
})
|
|
511
|
+
});
|
|
512
|
+
const initialValue = value ? {
|
|
513
|
+
label: formatDate(value, {
|
|
514
|
+
formatDisplayLabel: this.props.formatDisplayLabel,
|
|
515
|
+
dateFormat: this.props.dateFormat,
|
|
516
|
+
l10n: this.state.l10n
|
|
517
|
+
}),
|
|
518
|
+
value
|
|
519
|
+
} : null;
|
|
520
|
+
|
|
521
|
+
// `label` takes precedence of the `inputLabel`
|
|
522
|
+
const fullopenCalendarLabel = label || inputLabel ? `${label || inputLabel}, ${openCalendarLabel}` : openCalendarLabel;
|
|
523
|
+
return (
|
|
524
|
+
// These event handlers must be on this element because the events come
|
|
525
|
+
// from different child elements.
|
|
526
|
+
// Until innerProps is removed, it must remain a div rather than a primitive component.
|
|
527
|
+
jsx("div", _extends({}, innerProps, {
|
|
528
|
+
css: pickerContainerStyles
|
|
529
|
+
// Since the onclick, onfocus are passed down, adding role="presentation" prevents typecheck errors.
|
|
530
|
+
,
|
|
531
|
+
role: "presentation",
|
|
532
|
+
onBlur: this.onContainerBlur,
|
|
533
|
+
onFocus: this.onContainerFocus,
|
|
534
|
+
onClick: this.onInputClick,
|
|
535
|
+
onInput: this.onTextInput,
|
|
536
|
+
onKeyDown: this.onInputKeyDown,
|
|
537
|
+
ref: this.getContainerRef,
|
|
538
|
+
"data-testid": testId && `${testId}--container`
|
|
539
|
+
}), jsx("input", {
|
|
540
|
+
name: name,
|
|
541
|
+
type: "hidden",
|
|
542
|
+
value: value,
|
|
543
|
+
"data-testid": testId && `${testId}--input`
|
|
544
|
+
}), jsx(Select, _extends({
|
|
545
|
+
appearance: this.props.appearance,
|
|
546
|
+
"aria-describedby": ariaDescribedBy,
|
|
547
|
+
label: label || undefined,
|
|
548
|
+
autoFocus: autoFocus,
|
|
549
|
+
clearControlLabel: clearControlLabel,
|
|
550
|
+
closeMenuOnSelect: true
|
|
551
|
+
// FOr some reason, this and the below `styles` type error _only_ show
|
|
552
|
+
// up when you alter some of the properties in the `selectComponents`
|
|
553
|
+
// object. These errors are still present, and I suspect have always
|
|
554
|
+
// been present, without changing the unrelated code. Ignoring as the
|
|
555
|
+
// component still works as expected despite this error. And also
|
|
556
|
+
// because the select refresh team may solve it later.
|
|
557
|
+
,
|
|
558
|
+
components: selectComponents,
|
|
559
|
+
enableAnimation: false,
|
|
560
|
+
inputId: id,
|
|
561
|
+
inputValue: actualSelectInputValue,
|
|
562
|
+
isDisabled: isDisabled,
|
|
563
|
+
isRequired: isRequired,
|
|
564
|
+
menuIsOpen: menuIsOpen,
|
|
565
|
+
onBlur: this.onSelectBlur,
|
|
566
|
+
onChange: this.onSelectChange,
|
|
567
|
+
onFocus: this.onSelectFocus,
|
|
568
|
+
onInputChange: this.handleSelectInputChange,
|
|
569
|
+
placeholder: getPlaceholder({
|
|
570
|
+
placeholder: this.props.placeholder,
|
|
571
|
+
l10n: this.state.l10n
|
|
572
|
+
}),
|
|
573
|
+
styles: mergedStyles,
|
|
574
|
+
value: initialValue
|
|
575
|
+
}, selectProps, {
|
|
576
|
+
// These are below the spread because I don't know what is in
|
|
577
|
+
// selectProps or not and what wil be overwritten
|
|
578
|
+
isClearable: true,
|
|
579
|
+
isInvalid: isInvalid,
|
|
580
|
+
spacing: spacing,
|
|
581
|
+
testId: testId
|
|
582
|
+
// These aren't part of `Select`'s API, but we're using them here.
|
|
583
|
+
// @ts-ignore -- Property 'calendarContainerRef' does not exist on type 'IntrinsicAttributes & LibraryManagedAttributes<(<Option extends unknown = OptionType, IsMulti extends boolean = false>(props: AtlaskitSelectProps<Option, IsMulti> & { ...; }) => Element), AtlaskitSelectProps<...> & { ...; }>'.
|
|
584
|
+
,
|
|
585
|
+
calendarContainerRef: calendarProps.calendarContainerRef,
|
|
586
|
+
calendarDisabled: calendarProps.calendarDisabled,
|
|
587
|
+
calendarDisabledDateFilter: calendarProps.calendarDisabledDateFilter,
|
|
588
|
+
calendarLocale: calendarProps.calendarLocale,
|
|
589
|
+
calendarMaxDate: calendarProps.calendarMaxDate,
|
|
590
|
+
calendarMinDate: calendarProps.calendarMinDate,
|
|
591
|
+
calendarRef: calendarProps.calendarRef,
|
|
592
|
+
calendarValue: calendarProps.calendarValue,
|
|
593
|
+
calendarView: calendarProps.calendarView,
|
|
594
|
+
calendarWeekStartDay: calendarProps.calendarWeekStartDay,
|
|
595
|
+
nextMonthLabel: nextMonthLabel,
|
|
596
|
+
onCalendarChange: calendarProps.onCalendarChange,
|
|
597
|
+
onCalendarSelect: calendarProps.onCalendarSelect,
|
|
598
|
+
previousMonthLabel: previousMonthLabel,
|
|
599
|
+
shouldSetFocusOnCurrentDay: calendarProps.shouldSetFocusOnCurrentDay
|
|
600
|
+
})), shouldShowCalendarButton && !isDisabled ? jsx(IdProvider, {
|
|
601
|
+
prefix: "open-calendar-label--"
|
|
602
|
+
}, ({
|
|
603
|
+
id: openCalendarLabelId
|
|
604
|
+
}) => jsx(Box, {
|
|
605
|
+
xcss: [iconContainerStyles, value && !hideIcon ? iconSpacingWithClearButtonStyles : iconSpacingWithoutClearButtonStyles]
|
|
606
|
+
}, jsx(IconButton, {
|
|
607
|
+
appearance: "subtle",
|
|
608
|
+
label: !inputLabelId ? fullopenCalendarLabel : openCalendarLabel,
|
|
609
|
+
"aria-labelledby": inputLabelId ? `${inputLabelId} ${openCalendarLabelId}` : undefined,
|
|
610
|
+
id: openCalendarLabelId,
|
|
611
|
+
icon: iconProps => jsx(CalendarIcon, _extends({}, iconProps, {
|
|
612
|
+
color: "var(--ds-icon, #44546F)"
|
|
613
|
+
})),
|
|
614
|
+
onClick: this.onCalendarButtonClick,
|
|
615
|
+
onKeyDown: this.onCalendarButtonKeyDown,
|
|
616
|
+
ref: this.calendarButtonRef,
|
|
617
|
+
testId: testId && `${testId}--open-calendar-button`
|
|
618
|
+
}))) : null)
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
_defineProperty(DatePickerComponent, "defaultProps", datePickerDefaultProps);
|
|
623
|
+
export { DatePickerComponent as DatePickerWithoutAnalytics };
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* __Date picker__
|
|
627
|
+
*
|
|
628
|
+
* A date picker allows the user to select a particular date.
|
|
629
|
+
*
|
|
630
|
+
* - [Examples](https://atlassian.design/components/datetime-picker/date-picker/examples)
|
|
631
|
+
* - [Code](https://atlassian.design/components/datetime-picker/date-picker/code)
|
|
632
|
+
* - [Usage](https://atlassian.design/components/datetime-picker/date-picker/usage)
|
|
633
|
+
*/
|
|
634
|
+
const DatePicker = withAnalyticsContext({
|
|
635
|
+
componentName: 'datePicker',
|
|
636
|
+
packageName,
|
|
637
|
+
packageVersion
|
|
638
|
+
})(withAnalyticsEvents({
|
|
639
|
+
onChange: createAndFireEvent('atlaskit')({
|
|
640
|
+
action: 'selectedDate',
|
|
641
|
+
actionSubject: 'datePicker',
|
|
642
|
+
attributes: {
|
|
643
|
+
componentName: 'datePicker',
|
|
644
|
+
packageName,
|
|
645
|
+
packageVersion
|
|
646
|
+
}
|
|
647
|
+
})
|
|
648
|
+
})(DatePickerComponent));
|
|
649
|
+
export default DatePicker;
|