@instructure/ui-time-select 8.11.2-snapshot.5 → 8.11.2-snapshot.54

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.
@@ -27,26 +27,18 @@ var _dec, _class, _class2, _temp;
27
27
  * SOFTWARE.
28
28
  */
29
29
  import React, { Component } from 'react';
30
- import { ApplyLocaleContext, Locale } from '@instructure/ui-i18n';
31
- import dayjs from 'dayjs';
32
- import utc from 'dayjs/plugin/utc';
33
- import timezone from 'dayjs/plugin/timezone';
34
- import localizedFormat from 'dayjs/plugin/localizedFormat';
35
- import './locales';
36
- dayjs.extend(utc);
37
- dayjs.extend(timezone);
38
- dayjs.extend(localizedFormat);
30
+ import { ApplyLocaleContext, Locale, DateTime } from '@instructure/ui-i18n';
39
31
  import { getInteraction, passthroughProps, callRenderProp } from '@instructure/ui-react-utils';
40
32
  import { testable } from '@instructure/ui-testable';
41
33
  import { Select } from '@instructure/ui-select';
42
34
  import { uid } from '@instructure/uid';
43
35
  import { allowedProps, propTypes } from './props';
36
+
44
37
  /**
45
38
  ---
46
39
  category: components
47
40
  ---
48
- **/
49
-
41
+ **/
50
42
  let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class TimeSelect extends Component {
51
43
  constructor(props) {
52
44
  super(props);
@@ -54,60 +46,64 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
54
46
  this._emptyOptionId = uid('Select-EmptyOption');
55
47
 
56
48
  this.handleRef = node => {
57
- this._select = node; // TODO remove this and keep this.ref
58
-
59
49
  this.ref = node;
60
50
  };
61
51
 
62
52
  this.handleBlur = event => {
53
+ var _this$props$onBlur, _this$props;
54
+
63
55
  this.setState({
64
- highlightedOptionId: null
56
+ highlightedOptionId: void 0
65
57
  });
66
- this.props.onBlur(event);
58
+ (_this$props$onBlur = (_this$props = this.props).onBlur) === null || _this$props$onBlur === void 0 ? void 0 : _this$props$onBlur.call(_this$props, event);
67
59
  };
68
60
 
69
61
  this.handleInputChange = event => {
70
62
  const value = event.target.value;
71
63
  const newOptions = this.filterOptions(value);
72
- this.setState(state => ({
64
+ this.setState(_state => ({
73
65
  inputValue: value,
74
66
  filteredOptions: newOptions,
75
- highlightedOptionId: newOptions.length > 0 ? newOptions[0].id : null,
67
+ highlightedOptionId: newOptions.length > 0 ? newOptions[0].id : void 0,
76
68
  isShowingOptions: true
77
69
  }));
78
70
 
79
71
  if (!this.state.isShowingOptions) {
80
- this.props.onShowOptions(event);
72
+ var _this$props$onShowOpt, _this$props2;
73
+
74
+ (_this$props$onShowOpt = (_this$props2 = this.props).onShowOptions) === null || _this$props$onShowOpt === void 0 ? void 0 : _this$props$onShowOpt.call(_this$props2, event);
81
75
  }
82
76
  };
83
77
 
84
78
  this.handleShowOptions = event => {
79
+ var _this$props$onShowOpt2, _this$props3;
80
+
85
81
  this.setState({
86
82
  isShowingOptions: true
87
83
  });
88
- this.props.onShowOptions(event);
84
+ (_this$props$onShowOpt2 = (_this$props3 = this.props).onShowOptions) === null || _this$props$onShowOpt2 === void 0 ? void 0 : _this$props$onShowOpt2.call(_this$props3, event);
89
85
  };
90
86
 
91
87
  this.handleHideOptions = event => {
88
+ var _this$props$onHideOpt, _this$props4;
89
+
92
90
  const selectedOptionId = this.state.selectedOptionId;
93
91
  const option = this.getOption('id', selectedOptionId);
94
92
  let prevValue = '';
95
93
 
96
94
  if (this.props.defaultValue) {
97
- const date = dayjs(this.props.defaultValue).tz(this.timezone(), true).locale(this.locale());
98
- prevValue = date.format(this.props.format);
95
+ const date = DateTime.parse(this.props.defaultValue, this.locale(), this.timezone());
96
+ prevValue = this.props.format ? date.format(this.props.format) : date.toISOString();
99
97
  }
100
98
 
101
- this.setState(({
102
- inputValue
103
- }) => ({
99
+ this.setState(_state => ({
104
100
  isShowingOptions: false,
105
- highlightedOptionId: null,
101
+ highlightedOptionId: void 0,
106
102
  inputValue: selectedOptionId ? option.label : prevValue,
107
103
  filteredOptions: this.filterOptions(''),
108
104
  ...this.matchValue()
109
105
  }));
110
- this.props.onHideOptions(event);
106
+ (_this$props$onHideOpt = (_this$props4 = this.props).onHideOptions) === null || _this$props$onHideOpt === void 0 ? void 0 : _this$props$onHideOpt.call(_this$props4, event);
111
107
  };
112
108
 
113
109
  this.handleHighlightOption = (event, {
@@ -125,6 +121,8 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
125
121
  this.handleSelectOption = (event, {
126
122
  id
127
123
  }) => {
124
+ var _this$props$onHideOpt2, _this$props6;
125
+
128
126
  if (id === this._emptyOptionId) {
129
127
  this.setState({
130
128
  isShowingOptions: false
@@ -133,37 +131,53 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
133
131
  }
134
132
 
135
133
  const option = this.getOption('id', id);
134
+ let newInputValue;
136
135
 
137
136
  if (this.isControlled) {
138
137
  const prev = this.getOption('id', this.state.selectedOptionId);
138
+ newInputValue = prev ? prev.label : '';
139
139
  this.setState({
140
140
  isShowingOptions: false,
141
- inputValue: prev ? prev.label : '',
141
+ inputValue: newInputValue,
142
142
  filteredOptions: this.filterOptions('')
143
143
  });
144
144
  } else {
145
+ newInputValue = option.label;
145
146
  this.setState({
146
147
  isShowingOptions: false,
147
148
  selectedOptionId: id,
148
- inputValue: option.label,
149
+ inputValue: newInputValue,
149
150
  filteredOptions: this.filterOptions('')
150
151
  });
151
152
  }
152
153
 
153
154
  if (id !== this.state.selectedOptionId) {
154
- this.props.onChange(event, {
155
- value: option.value
155
+ var _this$props$onChange, _this$props5;
156
+
157
+ (_this$props$onChange = (_this$props5 = this.props).onChange) === null || _this$props$onChange === void 0 ? void 0 : _this$props$onChange.call(_this$props5, event, {
158
+ value: option.value,
159
+ inputText: newInputValue
156
160
  });
157
161
  }
158
162
 
159
- this.props.onHideOptions(event);
163
+ (_this$props$onHideOpt2 = (_this$props6 = this.props).onHideOptions) === null || _this$props$onHideOpt2 === void 0 ? void 0 : _this$props$onHideOpt2.call(_this$props6, event);
160
164
  };
161
165
 
162
166
  this.state = this.getInitialState();
163
167
  }
164
168
 
169
+ componentDidMount() {
170
+ // we'll need to recalculate the state because the context value is
171
+ // set at this point (and it might change locale & timezone)
172
+ this.setState(this.getInitialState());
173
+ }
174
+
165
175
  focus() {
166
- this._select && this._select.focus();
176
+ this.ref && this.ref.focus();
177
+ }
178
+
179
+ get _select() {
180
+ return this.ref;
167
181
  }
168
182
 
169
183
  get isControlled() {
@@ -177,11 +191,11 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
177
191
  }
178
192
 
179
193
  get focused() {
180
- return this._select && this._select.focused;
194
+ return this.ref && this.ref.focused;
181
195
  }
182
196
 
183
197
  get id() {
184
- return this._select && this._select.id;
198
+ return this.ref && this.ref.id;
185
199
  }
186
200
 
187
201
  locale() {
@@ -201,7 +215,7 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
201
215
  return this.context.timezone;
202
216
  }
203
217
 
204
- return dayjs.tz.guess();
218
+ return DateTime.browserTimeZone();
205
219
  }
206
220
 
207
221
  componentDidUpdate(prevProps) {
@@ -223,7 +237,7 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
223
237
 
224
238
  this.setState({
225
239
  inputValue: option ? option.label : '',
226
- selectedOptionId: option ? option.id : null
240
+ selectedOptionId: option ? option.id : void 0
227
241
  });
228
242
  }
229
243
  }
@@ -236,17 +250,17 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
236
250
  options: initialOptions,
237
251
  filteredOptions: initialOptions,
238
252
  isShowingOptions: false,
239
- highlightedOptionId: initialSelection ? initialSelection.id : null,
240
- selectedOptionId: initialSelection ? initialSelection.id : null
253
+ highlightedOptionId: initialSelection ? initialSelection.id : void 0,
254
+ selectedOptionId: initialSelection ? initialSelection.id : void 0
241
255
  };
242
256
  }
243
257
 
244
258
  getInitialOption(options) {
245
- const _this$props = this.props,
246
- value = _this$props.value,
247
- defaultValue = _this$props.defaultValue,
248
- defaultToFirstOption = _this$props.defaultToFirstOption,
249
- format = _this$props.format;
259
+ const _this$props7 = this.props,
260
+ value = _this$props7.value,
261
+ defaultValue = _this$props7.defaultValue,
262
+ defaultToFirstOption = _this$props7.defaultToFirstOption,
263
+ format = _this$props7.format;
250
264
  const initialValue = value || defaultValue;
251
265
 
252
266
  if (typeof initialValue === 'string') {
@@ -259,9 +273,9 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
259
273
  } // value does not match an existing option
260
274
 
261
275
 
262
- const date = dayjs(initialValue).tz(this.timezone(), true).locale(this.locale());
276
+ const date = DateTime.parse(initialValue, this.locale(), this.timezone());
263
277
  return {
264
- label: date.format(format)
278
+ label: format ? date.format(format) : date.toISOString()
265
279
  };
266
280
  } // otherwise return first option, if desired
267
281
 
@@ -270,7 +284,7 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
270
284
  return options[0];
271
285
  }
272
286
 
273
- return null;
287
+ return void 0;
274
288
  }
275
289
 
276
290
  getOption(field, value, options = this.state.options) {
@@ -281,7 +295,7 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
281
295
  // ISO8601 strings may contain a space. Remove any spaces before using the
282
296
  // date as the id.
283
297
  const dateStr = date.toISOString();
284
- return dateStr ? dateStr.replace(/\s/g, '') : null;
298
+ return dateStr.replace(/\s/g, '');
285
299
  }
286
300
 
287
301
  getBaseDate() {
@@ -289,12 +303,15 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
289
303
  const baseValue = this.props.value || this.props.defaultValue;
290
304
 
291
305
  if (baseValue) {
292
- baseDate = dayjs(baseValue).tz(this.timezone(), true).locale(this.locale());
306
+ baseDate = DateTime.parse(baseValue, this.locale(), this.timezone());
293
307
  } else {
294
- baseDate = dayjs().tz(this.timezone(), true).locale(this.locale());
308
+ baseDate = DateTime.now(this.locale(), this.timezone());
295
309
  }
296
310
 
297
- return baseDate.second(0).millisecond(0);
311
+ return baseDate.set({
312
+ second: 0,
313
+ millisecond: 0
314
+ }).clone();
298
315
  }
299
316
 
300
317
  generateOptions() {
@@ -304,15 +321,16 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
304
321
  for (let hour = 0; hour < 24; hour++) {
305
322
  for (let minute = 0; minute < 60 / this.props.step; minute++) {
306
323
  const minutes = minute * this.props.step;
307
- const newDate = date.hour(hour).minute(minutes); // store time options
324
+ const newDate = date.set({
325
+ hour: hour,
326
+ minute: minutes
327
+ }); // store time options
308
328
 
309
329
  options.push({
310
330
  id: this.getFormattedId(newDate),
311
331
  // iso no spaces
312
332
  value: newDate.toISOString(),
313
- // iso
314
- label: newDate.format(this.props.format) // formatted string
315
-
333
+ label: this.props.format ? newDate.format(this.props.format) : newDate.toISOString()
316
334
  });
317
335
  }
318
336
  }
@@ -321,15 +339,17 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
321
339
  }
322
340
 
323
341
  filterOptions(inputValue) {
324
- return this.state.options.filter(option => option.label.toLowerCase().startsWith(inputValue.toLowerCase()));
342
+ var _this$state;
343
+
344
+ return (_this$state = this.state) === null || _this$state === void 0 ? void 0 : _this$state.options.filter(option => option.label.toLowerCase().startsWith(inputValue.toLowerCase()));
325
345
  }
326
346
 
327
347
  matchValue() {
328
- const _this$state = this.state,
329
- inputValue = _this$state.inputValue,
330
- filteredOptions = _this$state.filteredOptions,
331
- highlightedOptionId = _this$state.highlightedOptionId,
332
- selectedOptionId = _this$state.selectedOptionId; // an option matching user input exists
348
+ const _this$state2 = this.state,
349
+ inputValue = _this$state2.inputValue,
350
+ filteredOptions = _this$state2.filteredOptions,
351
+ highlightedOptionId = _this$state2.highlightedOptionId,
352
+ selectedOptionId = _this$state2.selectedOptionId; // an option matching user input exists
333
353
 
334
354
  if (filteredOptions.length === 1) {
335
355
  const onlyOption = filteredOptions[0]; // automatically select the matching option
@@ -369,13 +389,15 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
369
389
  inputValue: ''
370
390
  };
371
391
  }
392
+
393
+ return void 0;
372
394
  }
373
395
 
374
396
  renderOptions() {
375
- const _this$state2 = this.state,
376
- filteredOptions = _this$state2.filteredOptions,
377
- highlightedOptionId = _this$state2.highlightedOptionId,
378
- selectedOptionId = _this$state2.selectedOptionId;
397
+ const _this$state3 = this.state,
398
+ filteredOptions = _this$state3.filteredOptions,
399
+ highlightedOptionId = _this$state3.highlightedOptionId,
400
+ selectedOptionId = _this$state3.selectedOptionId;
379
401
 
380
402
  if (filteredOptions.length < 1) {
381
403
  return this.renderEmptyOption();
@@ -402,34 +424,34 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
402
424
  }
403
425
 
404
426
  render() {
405
- const _this$props2 = this.props,
406
- value = _this$props2.value,
407
- defaultValue = _this$props2.defaultValue,
408
- placeholder = _this$props2.placeholder,
409
- renderLabel = _this$props2.renderLabel,
410
- inputRef = _this$props2.inputRef,
411
- id = _this$props2.id,
412
- listRef = _this$props2.listRef,
413
- renderBeforeInput = _this$props2.renderBeforeInput,
414
- renderAfterInput = _this$props2.renderAfterInput,
415
- isRequired = _this$props2.isRequired,
416
- isInline = _this$props2.isInline,
417
- width = _this$props2.width,
418
- format = _this$props2.format,
419
- step = _this$props2.step,
420
- optionsMaxWidth = _this$props2.optionsMaxWidth,
421
- visibleOptionsCount = _this$props2.visibleOptionsCount,
422
- messages = _this$props2.messages,
423
- placement = _this$props2.placement,
424
- constrain = _this$props2.constrain,
425
- onFocus = _this$props2.onFocus,
426
- onShowOptions = _this$props2.onShowOptions,
427
- onHideOptions = _this$props2.onHideOptions,
428
- rest = _objectWithoutProperties(_this$props2, _excluded);
429
-
430
- const _this$state3 = this.state,
431
- inputValue = _this$state3.inputValue,
432
- isShowingOptions = _this$state3.isShowingOptions;
427
+ const _this$props8 = this.props,
428
+ value = _this$props8.value,
429
+ defaultValue = _this$props8.defaultValue,
430
+ placeholder = _this$props8.placeholder,
431
+ renderLabel = _this$props8.renderLabel,
432
+ inputRef = _this$props8.inputRef,
433
+ id = _this$props8.id,
434
+ listRef = _this$props8.listRef,
435
+ renderBeforeInput = _this$props8.renderBeforeInput,
436
+ renderAfterInput = _this$props8.renderAfterInput,
437
+ isRequired = _this$props8.isRequired,
438
+ isInline = _this$props8.isInline,
439
+ width = _this$props8.width,
440
+ format = _this$props8.format,
441
+ step = _this$props8.step,
442
+ optionsMaxWidth = _this$props8.optionsMaxWidth,
443
+ visibleOptionsCount = _this$props8.visibleOptionsCount,
444
+ messages = _this$props8.messages,
445
+ placement = _this$props8.placement,
446
+ constrain = _this$props8.constrain,
447
+ onFocus = _this$props8.onFocus,
448
+ onShowOptions = _this$props8.onShowOptions,
449
+ onHideOptions = _this$props8.onHideOptions,
450
+ rest = _objectWithoutProperties(_this$props8, _excluded);
451
+
452
+ const _this$state4 = this.state,
453
+ inputValue = _this$state4.inputValue,
454
+ isShowingOptions = _this$state4.isShowingOptions;
433
455
  return /*#__PURE__*/React.createElement(Select, Object.assign({
434
456
  renderLabel: renderLabel,
435
457
  inputValue: inputValue,
@@ -462,7 +484,9 @@ let TimeSelect = (_dec = testable(), _dec(_class = (_temp = _class2 = class Time
462
484
 
463
485
  }, _class2.displayName = "TimeSelect", _class2.componentId = 'TimeSelect', _class2.allowedProps = allowedProps, _class2.propTypes = propTypes, _class2.defaultProps = {
464
486
  defaultToFirstOption: false,
487
+ id: void 0,
465
488
  format: 'LT',
489
+ // see https://momentjs.com/docs/#/displaying/
466
490
  step: 30,
467
491
  isRequired: false,
468
492
  isInline: false,
@@ -56,7 +56,8 @@ const propTypes = {
56
56
  /**
57
57
  * The format to use when displaying the possible and currently selected options.
58
58
  *
59
- * See [moment.js formats](https://momentjs.com/docs/#/displaying/format/) for the list of available formats.
59
+ * See [moment](https://momentjs.com/docs/#/displaying/format/) for the list
60
+ * of available formats.
60
61
  */
61
62
  format: PropTypes.string,
62
63
 
@@ -0,0 +1 @@
1
+ {"type":"module"}