@pingux/astro 1.16.1-alpha.0 → 1.17.0-alpha.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.
@@ -38,6 +38,8 @@ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime-c
38
38
 
39
39
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
40
40
 
41
+ var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
42
+
41
43
  var _react = _interopRequireWildcard(require("react"));
42
44
 
43
45
  var _propTypes = _interopRequireDefault(require("prop-types"));
@@ -78,7 +80,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
78
80
 
79
81
  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; }
80
82
 
81
- 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) { (0, _defineProperty2["default"])(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; }
83
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context2, _context3; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context2 = ownKeys(Object(source), !0)).call(_context2, function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context3 = ownKeys(Object(source))).call(_context3, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
82
84
 
83
85
  /**
84
86
  * Combines an input with a listbox for a filterable dropdown list.
@@ -103,13 +105,14 @@ var ComboBoxField = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
103
105
  onFocusChange = props.onFocusChange,
104
106
  onKeyDown = props.onKeyDown,
105
107
  onKeyUp = props.onKeyUp,
108
+ hasAddOption = props.hasAddOption,
106
109
  hasCustomValue = props.hasCustomValue,
107
110
  hasNoEmptySelection = props.hasNoEmptySelection,
108
111
  defaultSelectedKey = props.defaultSelectedKey,
109
112
  selectedKey = props.selectedKey,
110
113
  onSelectionChange = props.onSelectionChange,
111
- defaultItems = props.defaultItems,
112
- items = props.items,
114
+ initialDefaultItems = props.defaultItems,
115
+ initialItems = props.items,
113
116
  onOpenChange = props.onOpenChange,
114
117
  loadingState = props.loadingState,
115
118
  onLoadMore = props.onLoadMore,
@@ -137,13 +140,11 @@ var ComboBoxField = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
137
140
  onFocusChange: onFocusChange,
138
141
  onKeyDown: onKeyDown,
139
142
  onKeyUp: onKeyUp,
140
- allowsCustomValue: hasCustomValue,
143
+ allowsCustomValue: hasAddOption || hasCustomValue,
141
144
  disallowEmptySelection: hasNoEmptySelection,
142
145
  defaultSelectedKey: defaultSelectedKey,
143
146
  selectedKey: selectedKey,
144
147
  onSelectionChange: onSelectionChange,
145
- defaultItems: defaultItems,
146
- items: items,
147
148
  onOpenChange: onOpenChange,
148
149
  loadingState: loadingState,
149
150
  onLoadMore: onLoadMore,
@@ -169,18 +170,47 @@ var ComboBoxField = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
169
170
 
170
171
  var onSelectionChangeHandler = function onSelectionChangeHandler(key) {
171
172
  var newVal = key || selectedKey || '';
173
+
174
+ if (hasAddOption && selectedKey !== inputValue) {
175
+ newVal = inputValue;
176
+ }
177
+
172
178
  if (onSelectionChange) onSelectionChange(newVal);
173
179
  };
174
180
 
181
+ var shouldShowAddOption = hasAddOption && inputValue && selectedKey !== inputValue;
182
+ var addOption = "ADD: ".concat(inputValue);
183
+
184
+ var getItemsArr = function getItemsArr(initialArr) {
185
+ if (initialArr && shouldShowAddOption) {
186
+ var _context;
187
+
188
+ return (0, _concat["default"])(_context = []).call(_context, initialArr, [{
189
+ name: addOption,
190
+ key: addOption
191
+ }]);
192
+ }
193
+
194
+ return initialArr;
195
+ };
196
+
197
+ var defaultItems = getItemsArr(initialDefaultItems);
198
+ var items = getItemsArr(initialItems);
199
+
175
200
  var _useFilter = (0, _i18n.useFilter)({
176
201
  sensitivity: 'base'
177
202
  }),
178
203
  contains = _useFilter.contains;
179
204
 
180
205
  var state = (0, _combobox2.useComboBoxState)(_objectSpread(_objectSpread({}, comboBoxOptions), {}, {
181
- onSelectionChange: hasCustomValue ? onSelectionChangeHandler : onSelectionChange,
206
+ defaultItems: defaultItems,
207
+ items: items,
208
+ onSelectionChange: hasAddOption || hasCustomValue ? onSelectionChangeHandler : onSelectionChange,
182
209
  defaultFilter: typeof defaultFilter !== 'undefined' ? defaultFilter : contains
183
210
  }));
211
+ (0, _react.useEffect)(function () {
212
+ if (shouldShowAddOption) state.selectionManager.setFocusedKey(addOption);
213
+ }, [shouldShowAddOption, state, addOption]);
184
214
 
185
215
  var _useComboBox = (0, _combobox.useComboBox)(_objectSpread(_objectSpread({}, comboBoxOptions), {}, {
186
216
  buttonRef: buttonRef,
@@ -286,6 +316,9 @@ var ComboBoxField = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
286
316
  }, listbox));
287
317
  });
288
318
  ComboBoxField.propTypes = {
319
+ /* Whether or not adding new options to the list is enabled. */
320
+ hasAddOption: _propTypes["default"].bool,
321
+
289
322
  /** Whether the ComboBox allows a non-item matching input value to be set. */
290
323
  hasCustomValue: _propTypes["default"].bool,
291
324
 
@@ -14,7 +14,11 @@ _Object$defineProperty(exports, "__esModule", {
14
14
  value: true
15
15
  });
16
16
 
17
- exports["default"] = exports.WithoutStatusIndicator = exports.WithSections = exports.WithCustomInputValue = exports.WithCustomHeight = exports.Required = exports.HelperText = exports.FocusMenuTrigger = exports.DisabledKeys = exports.Disabled = exports.Default = exports.CustomDefaultFilter = exports.ControlledWithCustomValue = exports.ControlledSelection = exports.ControlledMenu = exports.ControlledInput = exports.ControlledFiltering = exports.AsyncLoading = exports.AllowCustomValue = void 0;
17
+ exports["default"] = exports.WithoutStatusIndicator = exports.WithSections = exports.WithCustomInputValue = exports.WithCustomHeight = exports.Required = exports.HelperText = exports.FocusMenuTrigger = exports.DisabledKeys = exports.Disabled = exports.Default = exports.CustomDefaultFilter = exports.ControlledWithCustomValue = exports.ControlledWithAddOption = exports.ControlledSelection = exports.ControlledMenu = exports.ControlledInput = exports.ControlledFiltering = exports.AsyncLoading = exports.AllowCustomValue = void 0;
18
+
19
+ var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
20
+
21
+ var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));
18
22
 
19
23
  var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
20
24
 
@@ -562,4 +566,56 @@ var CustomDefaultFilter = function CustomDefaultFilter() {
562
566
  }));
563
567
  };
564
568
 
565
- exports.CustomDefaultFilter = CustomDefaultFilter;
569
+ exports.CustomDefaultFilter = CustomDefaultFilter;
570
+
571
+ var ControlledWithAddOption = function ControlledWithAddOption() {
572
+ var _useState11 = (0, _react.useState)(items),
573
+ _useState12 = (0, _slicedToArray2["default"])(_useState11, 2),
574
+ options = _useState12[0],
575
+ setOptions = _useState12[1];
576
+
577
+ var _useState13 = (0, _react.useState)(''),
578
+ _useState14 = (0, _slicedToArray2["default"])(_useState13, 2),
579
+ inputValue = _useState14[0],
580
+ setInputValue = _useState14[1];
581
+
582
+ var _useState15 = (0, _react.useState)(''),
583
+ _useState16 = (0, _slicedToArray2["default"])(_useState15, 2),
584
+ selectedKey = _useState16[0],
585
+ setSelectedKey = _useState16[1];
586
+
587
+ var onSelectionChange = function onSelectionChange(key) {
588
+ // Add new option to options array
589
+ if (key && !(0, _find["default"])(options).call(options, function (_ref2) {
590
+ var name = _ref2.name;
591
+ return name === key;
592
+ })) {
593
+ var _context2;
594
+
595
+ setOptions((0, _concat["default"])(_context2 = []).call(_context2, options, [{
596
+ key: key,
597
+ name: key
598
+ }]));
599
+ }
600
+
601
+ setInputValue(key);
602
+ setSelectedKey(key);
603
+ };
604
+
605
+ return (0, _react2.jsx)(_index.OverlayProvider, null, (0, _react2.jsx)(_ComboBoxField["default"], (0, _extends2["default"])({
606
+ label: "Example label",
607
+ defaultItems: options
608
+ }, actions, {
609
+ inputValue: inputValue,
610
+ selectedKey: selectedKey,
611
+ onInputChange: setInputValue,
612
+ onSelectionChange: onSelectionChange,
613
+ hasAddOption: true
614
+ }), function (item) {
615
+ return (0, _react2.jsx)(_index.Item, {
616
+ key: item.name
617
+ }, item.name);
618
+ }));
619
+ };
620
+
621
+ exports.ControlledWithAddOption = ControlledWithAddOption;
@@ -32,6 +32,10 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/
32
32
 
33
33
  var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
34
34
 
35
+ var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
36
+
37
+ var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));
38
+
35
39
  var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
36
40
 
37
41
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
@@ -62,7 +66,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
62
66
 
63
67
  function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty2(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
64
68
 
65
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context5, _context6; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty2(_context5 = ownKeys(Object(source), !0)).call(_context5, function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty2(_context6 = ownKeys(Object(source))).call(_context6, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
69
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context6, _context7; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty2(_context6 = ownKeys(Object(source), !0)).call(_context6, function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty2(_context7 = ownKeys(Object(source))).call(_context7, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
66
70
 
67
71
  var items = [{
68
72
  name: 'Aardvark',
@@ -121,6 +125,54 @@ var ComboBoxWithCustomFilter = function ComboBoxWithCustomFilter() {
121
125
  });
122
126
  };
123
127
 
128
+ var ComboBoxWithAddOption = function ComboBoxWithAddOption() {
129
+ var _useState3 = (0, _react.useState)(items),
130
+ _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
131
+ options = _useState4[0],
132
+ setOptions = _useState4[1];
133
+
134
+ var _useState5 = (0, _react.useState)(''),
135
+ _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
136
+ inputValue = _useState6[0],
137
+ setInputValue = _useState6[1];
138
+
139
+ var _useState7 = (0, _react.useState)(''),
140
+ _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
141
+ selectedKey = _useState8[0],
142
+ setSelectedKey = _useState8[1];
143
+
144
+ var onSelectionChange = function onSelectionChange(key) {
145
+ if (key && !(0, _find["default"])(options).call(options, function (_ref2) {
146
+ var name = _ref2.name;
147
+ return name === key;
148
+ })) {
149
+ var _context;
150
+
151
+ setOptions((0, _concat["default"])(_context = []).call(_context, options, [{
152
+ key: key,
153
+ name: key
154
+ }]));
155
+ }
156
+
157
+ setInputValue(key);
158
+ setSelectedKey(key);
159
+ };
160
+
161
+ return (0, _react2.jsx)(_index.ComboBoxField, {
162
+ label: "Example label",
163
+ defaultItems: options,
164
+ inputValue: inputValue,
165
+ selectedKey: selectedKey,
166
+ onInputChange: setInputValue,
167
+ onSelectionChange: onSelectionChange,
168
+ hasAddOption: true
169
+ }, function (item) {
170
+ return (0, _react2.jsx)(_index.Item, {
171
+ key: item.name
172
+ }, item.name);
173
+ });
174
+ };
175
+
124
176
  beforeAll(function () {
125
177
  jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(function () {
126
178
  return 1000;
@@ -474,43 +526,43 @@ test('should use default contains filtering', function () {
474
526
  });
475
527
  test('should be able to use controlled filtering', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
476
528
  var options, input;
477
- return _regenerator["default"].wrap(function _callee$(_context) {
529
+ return _regenerator["default"].wrap(function _callee$(_context2) {
478
530
  while (1) {
479
- switch (_context.prev = _context.next) {
531
+ switch (_context2.prev = _context2.next) {
480
532
  case 0:
481
533
  (0, _testWrapper.render)((0, _react2.jsx)(ComboBoxWithCustomFilter, null));
482
534
  input = _testWrapper.screen.queryByRole('combobox'); // Should list all without filterable input
483
535
 
484
536
  _userEvent["default"].type(input, '{arrowdown}');
485
537
 
486
- _context.next = 5;
538
+ _context2.next = 5;
487
539
  return _testWrapper.screen.findAllByRole('option');
488
540
 
489
541
  case 5:
490
- options = _context.sent;
542
+ options = _context2.sent;
491
543
  expect(options).toHaveLength(items.length); // Should only list the second option
492
544
 
493
545
  _userEvent["default"].type(input, 'k');
494
546
 
495
- _context.next = 10;
547
+ _context2.next = 10;
496
548
  return _testWrapper.screen.findAllByRole('option');
497
549
 
498
550
  case 10:
499
- options = _context.sent;
551
+ options = _context2.sent;
500
552
  expect(options[0]).toHaveTextContent(items[1].name);
501
553
 
502
554
  case 12:
503
555
  case "end":
504
- return _context.stop();
556
+ return _context2.stop();
505
557
  }
506
558
  }
507
559
  }, _callee);
508
560
  })));
509
561
  test('should be able to use custom default filtering', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
510
562
  var options, defaultFilter, input;
511
- return _regenerator["default"].wrap(function _callee2$(_context2) {
563
+ return _regenerator["default"].wrap(function _callee2$(_context3) {
512
564
  while (1) {
513
- switch (_context2.prev = _context2.next) {
565
+ switch (_context3.prev = _context3.next) {
514
566
  case 0:
515
567
  defaultFilter = function defaultFilter(option, inputValue) {
516
568
  return (0, _startsWith["default"])(option).call(option, inputValue);
@@ -523,34 +575,34 @@ test('should be able to use custom default filtering', /*#__PURE__*/(0, _asyncTo
523
575
 
524
576
  _userEvent["default"].type(input, '{arrowdown}');
525
577
 
526
- _context2.next = 6;
578
+ _context3.next = 6;
527
579
  return _testWrapper.screen.findAllByRole('option');
528
580
 
529
581
  case 6:
530
- options = _context2.sent;
582
+ options = _context3.sent;
531
583
  expect(options).toHaveLength(items.length); // Should only list the second option
532
584
 
533
585
  _userEvent["default"].type(input, 'K');
534
586
 
535
- _context2.next = 11;
587
+ _context3.next = 11;
536
588
  return _testWrapper.screen.findAllByRole('option');
537
589
 
538
590
  case 11:
539
- options = _context2.sent;
591
+ options = _context3.sent;
540
592
  expect(options[0]).toHaveTextContent(items[1].name);
541
593
 
542
594
  case 13:
543
595
  case "end":
544
- return _context2.stop();
596
+ return _context3.stop();
545
597
  }
546
598
  }
547
599
  }, _callee2);
548
600
  })));
549
601
  test('should show in input "textValue" if provided', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
550
602
  var newItems, input, options;
551
- return _regenerator["default"].wrap(function _callee3$(_context3) {
603
+ return _regenerator["default"].wrap(function _callee3$(_context4) {
552
604
  while (1) {
553
- switch (_context3.prev = _context3.next) {
605
+ switch (_context4.prev = _context4.next) {
554
606
  case 0:
555
607
  newItems = (0, _map["default"])(items).call(items, function (item) {
556
608
  return _objectSpread(_objectSpread({}, item), {}, {
@@ -564,11 +616,11 @@ test('should show in input "textValue" if provided', /*#__PURE__*/(0, _asyncToGe
564
616
 
565
617
  _userEvent["default"].click(input);
566
618
 
567
- _context3.next = 6;
619
+ _context4.next = 6;
568
620
  return _testWrapper.screen.findAllByRole('option');
569
621
 
570
622
  case 6:
571
- options = _context3.sent;
623
+ options = _context4.sent;
572
624
 
573
625
  _userEvent["default"].click(options[0]);
574
626
 
@@ -580,7 +632,7 @@ test('should show in input "textValue" if provided', /*#__PURE__*/(0, _asyncToGe
580
632
 
581
633
  case 11:
582
634
  case "end":
583
- return _context3.stop();
635
+ return _context4.stop();
584
636
  }
585
637
  }
586
638
  }, _callee3);
@@ -951,25 +1003,71 @@ test('onSelectionChange works properly with custom value', function () {
951
1003
 
952
1004
  expect(onSelectionChange).toHaveBeenCalledWith('');
953
1005
  });
1006
+ test('add option shows when "hasAddOption" is provided', function () {
1007
+ (0, _testWrapper.render)((0, _react2.jsx)(ComboBoxWithAddOption, null));
1008
+
1009
+ var input = _testWrapper.screen.queryByRole('combobox');
1010
+
1011
+ expect(input).toHaveValue('');
1012
+ expect(_testWrapper.screen.queryByRole('listbox')).not.toBeInTheDocument();
1013
+ expect(_testWrapper.screen.queryByRole('option')).not.toBeInTheDocument();
1014
+ var inputValue = 'New value';
1015
+
1016
+ _userEvent["default"].type(input, inputValue);
1017
+
1018
+ expect(input).toHaveValue(inputValue);
1019
+ expect(_testWrapper.screen.queryByRole('listbox')).toBeInTheDocument();
1020
+
1021
+ var option = _testWrapper.screen.queryByRole('option');
1022
+
1023
+ expect(option).toBeInTheDocument();
1024
+ expect(option).toHaveClass('is-focused');
1025
+ expect(option).toHaveTextContent("ADD: ".concat(inputValue));
1026
+ });
1027
+ test('if "hasAddOption" is provided, then custom value is added to listbox on blur', function () {
1028
+ (0, _testWrapper.render)((0, _react2.jsx)(ComboBoxWithAddOption, null));
1029
+
1030
+ var input = _testWrapper.screen.queryByRole('combobox');
1031
+
1032
+ var inputValue = 'New value';
1033
+
1034
+ _userEvent["default"].type(input, inputValue);
1035
+
1036
+ expect(input).toHaveValue(inputValue); // blur input
1037
+
1038
+ _userEvent["default"].tab();
1039
+
1040
+ expect(_testWrapper.screen.queryByRole('listbox')).not.toBeInTheDocument();
1041
+ expect(_testWrapper.screen.queryByRole('option')).not.toBeInTheDocument();
1042
+
1043
+ _userEvent["default"].click(input);
1044
+
1045
+ expect(input).toHaveValue(inputValue);
1046
+ expect(_testWrapper.screen.queryByRole('listbox')).toBeInTheDocument();
1047
+
1048
+ var options = _testWrapper.screen.queryAllByRole('option');
1049
+
1050
+ expect(options[options.length - 1]).toHaveTextContent(inputValue);
1051
+ });
954
1052
  test('should have no accessibility violations', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
955
1053
  var _getComponent6, container, results;
956
1054
 
957
- return _regenerator["default"].wrap(function _callee4$(_context4) {
1055
+ return _regenerator["default"].wrap(function _callee4$(_context5) {
958
1056
  while (1) {
959
- switch (_context4.prev = _context4.next) {
1057
+ switch (_context5.prev = _context5.next) {
960
1058
  case 0:
961
1059
  jest.useRealTimers();
962
1060
  _getComponent6 = getComponent(), container = _getComponent6.container;
963
- _context4.next = 4;
1061
+ _context5.next = 4;
964
1062
  return (0, _jestAxe.axe)(container);
965
1063
 
966
1064
  case 4:
967
- results = _context4.sent;
1065
+ results = _context5.sent;
968
1066
  expect(results).toHaveNoViolations();
969
1067
 
970
1068
  case 6:
971
1069
  case "end":
972
- return _context4.stop();
1070
+ return _context5.stop();
973
1071
  }
974
1072
  }
975
1073
  }, _callee4);
@@ -11,13 +11,14 @@ import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
11
11
  import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
12
12
  import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
13
13
  import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
14
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
14
15
  var _excluded = ["shouldFocusOnHover", "shouldSelectOnPressUp"];
15
16
 
16
17
  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; }
17
18
 
18
- 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; }
19
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context2, _context3; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context2 = ownKeys(Object(source), !0)).call(_context2, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context3 = ownKeys(Object(source))).call(_context3, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
19
20
 
20
- import React, { useCallback, useRef, useState, forwardRef, useImperativeHandle } from 'react';
21
+ import React, { useCallback, useRef, useState, forwardRef, useImperativeHandle, useEffect } from 'react';
21
22
  import PropTypes from 'prop-types';
22
23
  import { DismissButton, useOverlayPosition } from '@react-aria/overlays';
23
24
  import { useComboBox } from '@react-aria/combobox';
@@ -57,13 +58,14 @@ var ComboBoxField = /*#__PURE__*/forwardRef(function (props, ref) {
57
58
  onFocusChange = props.onFocusChange,
58
59
  onKeyDown = props.onKeyDown,
59
60
  onKeyUp = props.onKeyUp,
61
+ hasAddOption = props.hasAddOption,
60
62
  hasCustomValue = props.hasCustomValue,
61
63
  hasNoEmptySelection = props.hasNoEmptySelection,
62
64
  defaultSelectedKey = props.defaultSelectedKey,
63
65
  selectedKey = props.selectedKey,
64
66
  onSelectionChange = props.onSelectionChange,
65
- defaultItems = props.defaultItems,
66
- items = props.items,
67
+ initialDefaultItems = props.defaultItems,
68
+ initialItems = props.items,
67
69
  onOpenChange = props.onOpenChange,
68
70
  loadingState = props.loadingState,
69
71
  onLoadMore = props.onLoadMore,
@@ -91,13 +93,11 @@ var ComboBoxField = /*#__PURE__*/forwardRef(function (props, ref) {
91
93
  onFocusChange: onFocusChange,
92
94
  onKeyDown: onKeyDown,
93
95
  onKeyUp: onKeyUp,
94
- allowsCustomValue: hasCustomValue,
96
+ allowsCustomValue: hasAddOption || hasCustomValue,
95
97
  disallowEmptySelection: hasNoEmptySelection,
96
98
  defaultSelectedKey: defaultSelectedKey,
97
99
  selectedKey: selectedKey,
98
100
  onSelectionChange: onSelectionChange,
99
- defaultItems: defaultItems,
100
- items: items,
101
101
  onOpenChange: onOpenChange,
102
102
  loadingState: loadingState,
103
103
  onLoadMore: onLoadMore,
@@ -123,18 +123,47 @@ var ComboBoxField = /*#__PURE__*/forwardRef(function (props, ref) {
123
123
 
124
124
  var onSelectionChangeHandler = function onSelectionChangeHandler(key) {
125
125
  var newVal = key || selectedKey || '';
126
+
127
+ if (hasAddOption && selectedKey !== inputValue) {
128
+ newVal = inputValue;
129
+ }
130
+
126
131
  if (onSelectionChange) onSelectionChange(newVal);
127
132
  };
128
133
 
134
+ var shouldShowAddOption = hasAddOption && inputValue && selectedKey !== inputValue;
135
+ var addOption = "ADD: ".concat(inputValue);
136
+
137
+ var getItemsArr = function getItemsArr(initialArr) {
138
+ if (initialArr && shouldShowAddOption) {
139
+ var _context;
140
+
141
+ return _concatInstanceProperty(_context = []).call(_context, initialArr, [{
142
+ name: addOption,
143
+ key: addOption
144
+ }]);
145
+ }
146
+
147
+ return initialArr;
148
+ };
149
+
150
+ var defaultItems = getItemsArr(initialDefaultItems);
151
+ var items = getItemsArr(initialItems);
152
+
129
153
  var _useFilter = useFilter({
130
154
  sensitivity: 'base'
131
155
  }),
132
156
  contains = _useFilter.contains;
133
157
 
134
158
  var state = useComboBoxState(_objectSpread(_objectSpread({}, comboBoxOptions), {}, {
135
- onSelectionChange: hasCustomValue ? onSelectionChangeHandler : onSelectionChange,
159
+ defaultItems: defaultItems,
160
+ items: items,
161
+ onSelectionChange: hasAddOption || hasCustomValue ? onSelectionChangeHandler : onSelectionChange,
136
162
  defaultFilter: typeof defaultFilter !== 'undefined' ? defaultFilter : contains
137
163
  }));
164
+ useEffect(function () {
165
+ if (shouldShowAddOption) state.selectionManager.setFocusedKey(addOption);
166
+ }, [shouldShowAddOption, state, addOption]);
138
167
 
139
168
  var _useComboBox = useComboBox(_objectSpread(_objectSpread({}, comboBoxOptions), {}, {
140
169
  buttonRef: buttonRef,
@@ -241,6 +270,9 @@ var ComboBoxField = /*#__PURE__*/forwardRef(function (props, ref) {
241
270
  }, listbox));
242
271
  });
243
272
  ComboBoxField.propTypes = {
273
+ /* Whether or not adding new options to the list is enabled. */
274
+ hasAddOption: PropTypes.bool,
275
+
244
276
  /** Whether the ComboBox allows a non-item matching input value to be set. */
245
277
  hasCustomValue: PropTypes.bool,
246
278
 
@@ -1,3 +1,5 @@
1
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
2
+ import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
1
3
  import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
2
4
  import _startsWithInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/starts-with";
3
5
  import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
@@ -469,4 +471,53 @@ export var CustomDefaultFilter = function CustomDefaultFilter() {
469
471
  key: item.name
470
472
  }, item.name);
471
473
  }));
474
+ };
475
+ export var ControlledWithAddOption = function ControlledWithAddOption() {
476
+ var _useState11 = useState(items),
477
+ _useState12 = _slicedToArray(_useState11, 2),
478
+ options = _useState12[0],
479
+ setOptions = _useState12[1];
480
+
481
+ var _useState13 = useState(''),
482
+ _useState14 = _slicedToArray(_useState13, 2),
483
+ inputValue = _useState14[0],
484
+ setInputValue = _useState14[1];
485
+
486
+ var _useState15 = useState(''),
487
+ _useState16 = _slicedToArray(_useState15, 2),
488
+ selectedKey = _useState16[0],
489
+ setSelectedKey = _useState16[1];
490
+
491
+ var onSelectionChange = function onSelectionChange(key) {
492
+ // Add new option to options array
493
+ if (key && !_findInstanceProperty(options).call(options, function (_ref2) {
494
+ var name = _ref2.name;
495
+ return name === key;
496
+ })) {
497
+ var _context2;
498
+
499
+ setOptions(_concatInstanceProperty(_context2 = []).call(_context2, options, [{
500
+ key: key,
501
+ name: key
502
+ }]));
503
+ }
504
+
505
+ setInputValue(key);
506
+ setSelectedKey(key);
507
+ };
508
+
509
+ return ___EmotionJSX(OverlayProvider, null, ___EmotionJSX(ComboBoxField, _extends({
510
+ label: "Example label",
511
+ defaultItems: options
512
+ }, actions, {
513
+ inputValue: inputValue,
514
+ selectedKey: selectedKey,
515
+ onInputChange: setInputValue,
516
+ onSelectionChange: onSelectionChange,
517
+ hasAddOption: true
518
+ }), function (item) {
519
+ return ___EmotionJSX(Item, {
520
+ key: item.name
521
+ }, item.name);
522
+ }));
472
523
  };
@@ -9,6 +9,8 @@ import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance
9
9
  import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
10
10
  import _asyncToGenerator from "@babel/runtime-corejs3/helpers/esm/asyncToGenerator";
11
11
  import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
12
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
13
+ import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
12
14
  import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
13
15
  import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
14
16
  import _startsWithInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/starts-with";
@@ -16,7 +18,7 @@ import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
16
18
 
17
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; }
18
20
 
19
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context5, _context6; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context5 = ownKeys(Object(source), !0)).call(_context5, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context6 = ownKeys(Object(source))).call(_context6, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
21
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context6, _context7; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context6 = ownKeys(Object(source), !0)).call(_context6, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context7 = ownKeys(Object(source))).call(_context7, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
20
22
 
21
23
  import React, { useState, useMemo } from 'react';
22
24
  import { axe } from 'jest-axe';
@@ -83,6 +85,54 @@ var ComboBoxWithCustomFilter = function ComboBoxWithCustomFilter() {
83
85
  });
84
86
  };
85
87
 
88
+ var ComboBoxWithAddOption = function ComboBoxWithAddOption() {
89
+ var _useState3 = useState(items),
90
+ _useState4 = _slicedToArray(_useState3, 2),
91
+ options = _useState4[0],
92
+ setOptions = _useState4[1];
93
+
94
+ var _useState5 = useState(''),
95
+ _useState6 = _slicedToArray(_useState5, 2),
96
+ inputValue = _useState6[0],
97
+ setInputValue = _useState6[1];
98
+
99
+ var _useState7 = useState(''),
100
+ _useState8 = _slicedToArray(_useState7, 2),
101
+ selectedKey = _useState8[0],
102
+ setSelectedKey = _useState8[1];
103
+
104
+ var onSelectionChange = function onSelectionChange(key) {
105
+ if (key && !_findInstanceProperty(options).call(options, function (_ref2) {
106
+ var name = _ref2.name;
107
+ return name === key;
108
+ })) {
109
+ var _context;
110
+
111
+ setOptions(_concatInstanceProperty(_context = []).call(_context, options, [{
112
+ key: key,
113
+ name: key
114
+ }]));
115
+ }
116
+
117
+ setInputValue(key);
118
+ setSelectedKey(key);
119
+ };
120
+
121
+ return ___EmotionJSX(ComboBoxField, {
122
+ label: "Example label",
123
+ defaultItems: options,
124
+ inputValue: inputValue,
125
+ selectedKey: selectedKey,
126
+ onInputChange: setInputValue,
127
+ onSelectionChange: onSelectionChange,
128
+ hasAddOption: true
129
+ }, function (item) {
130
+ return ___EmotionJSX(Item, {
131
+ key: item.name
132
+ }, item.name);
133
+ });
134
+ };
135
+
86
136
  beforeAll(function () {
87
137
  jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(function () {
88
138
  return 1000;
@@ -357,41 +407,41 @@ test('should use default contains filtering', function () {
357
407
  });
358
408
  test('should be able to use controlled filtering', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
359
409
  var options, input;
360
- return _regeneratorRuntime.wrap(function _callee$(_context) {
410
+ return _regeneratorRuntime.wrap(function _callee$(_context2) {
361
411
  while (1) {
362
- switch (_context.prev = _context.next) {
412
+ switch (_context2.prev = _context2.next) {
363
413
  case 0:
364
414
  render(___EmotionJSX(ComboBoxWithCustomFilter, null));
365
415
  input = screen.queryByRole('combobox'); // Should list all without filterable input
366
416
 
367
417
  userEvent.type(input, '{arrowdown}');
368
- _context.next = 5;
418
+ _context2.next = 5;
369
419
  return screen.findAllByRole('option');
370
420
 
371
421
  case 5:
372
- options = _context.sent;
422
+ options = _context2.sent;
373
423
  expect(options).toHaveLength(items.length); // Should only list the second option
374
424
 
375
425
  userEvent.type(input, 'k');
376
- _context.next = 10;
426
+ _context2.next = 10;
377
427
  return screen.findAllByRole('option');
378
428
 
379
429
  case 10:
380
- options = _context.sent;
430
+ options = _context2.sent;
381
431
  expect(options[0]).toHaveTextContent(items[1].name);
382
432
 
383
433
  case 12:
384
434
  case "end":
385
- return _context.stop();
435
+ return _context2.stop();
386
436
  }
387
437
  }
388
438
  }, _callee);
389
439
  })));
390
440
  test('should be able to use custom default filtering', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
391
441
  var options, defaultFilter, input;
392
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
442
+ return _regeneratorRuntime.wrap(function _callee2$(_context3) {
393
443
  while (1) {
394
- switch (_context2.prev = _context2.next) {
444
+ switch (_context3.prev = _context3.next) {
395
445
  case 0:
396
446
  defaultFilter = function defaultFilter(option, inputValue) {
397
447
  return _startsWithInstanceProperty(option).call(option, inputValue);
@@ -403,33 +453,33 @@ test('should be able to use custom default filtering', /*#__PURE__*/_asyncToGene
403
453
  input = screen.queryByRole('combobox'); // Should list all without filterable input
404
454
 
405
455
  userEvent.type(input, '{arrowdown}');
406
- _context2.next = 6;
456
+ _context3.next = 6;
407
457
  return screen.findAllByRole('option');
408
458
 
409
459
  case 6:
410
- options = _context2.sent;
460
+ options = _context3.sent;
411
461
  expect(options).toHaveLength(items.length); // Should only list the second option
412
462
 
413
463
  userEvent.type(input, 'K');
414
- _context2.next = 11;
464
+ _context3.next = 11;
415
465
  return screen.findAllByRole('option');
416
466
 
417
467
  case 11:
418
- options = _context2.sent;
468
+ options = _context3.sent;
419
469
  expect(options[0]).toHaveTextContent(items[1].name);
420
470
 
421
471
  case 13:
422
472
  case "end":
423
- return _context2.stop();
473
+ return _context3.stop();
424
474
  }
425
475
  }
426
476
  }, _callee2);
427
477
  })));
428
478
  test('should show in input "textValue" if provided', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
429
479
  var newItems, input, options;
430
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
480
+ return _regeneratorRuntime.wrap(function _callee3$(_context4) {
431
481
  while (1) {
432
- switch (_context3.prev = _context3.next) {
482
+ switch (_context4.prev = _context4.next) {
433
483
  case 0:
434
484
  newItems = _mapInstanceProperty(items).call(items, function (item) {
435
485
  return _objectSpread(_objectSpread({}, item), {}, {
@@ -441,11 +491,11 @@ test('should show in input "textValue" if provided', /*#__PURE__*/_asyncToGenera
441
491
  });
442
492
  input = screen.queryByRole('combobox');
443
493
  userEvent.click(input);
444
- _context3.next = 6;
494
+ _context4.next = 6;
445
495
  return screen.findAllByRole('option');
446
496
 
447
497
  case 6:
448
- options = _context3.sent;
498
+ options = _context4.sent;
449
499
  userEvent.click(options[0]);
450
500
  expect(input).toHaveValue(newItems[0].textValue); // Check that on clicking again "textValue" still returning
451
501
 
@@ -454,7 +504,7 @@ test('should show in input "textValue" if provided', /*#__PURE__*/_asyncToGenera
454
504
 
455
505
  case 11:
456
506
  case "end":
457
- return _context3.stop();
507
+ return _context4.stop();
458
508
  }
459
509
  }
460
510
  }, _callee3);
@@ -777,25 +827,56 @@ test('onSelectionChange works properly with custom value', function () {
777
827
  userEvent.type(input, '{selectall}{backspace}{enter}');
778
828
  expect(onSelectionChange).toHaveBeenCalledWith('');
779
829
  });
830
+ test('add option shows when "hasAddOption" is provided', function () {
831
+ render(___EmotionJSX(ComboBoxWithAddOption, null));
832
+ var input = screen.queryByRole('combobox');
833
+ expect(input).toHaveValue('');
834
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
835
+ expect(screen.queryByRole('option')).not.toBeInTheDocument();
836
+ var inputValue = 'New value';
837
+ userEvent.type(input, inputValue);
838
+ expect(input).toHaveValue(inputValue);
839
+ expect(screen.queryByRole('listbox')).toBeInTheDocument();
840
+ var option = screen.queryByRole('option');
841
+ expect(option).toBeInTheDocument();
842
+ expect(option).toHaveClass('is-focused');
843
+ expect(option).toHaveTextContent("ADD: ".concat(inputValue));
844
+ });
845
+ test('if "hasAddOption" is provided, then custom value is added to listbox on blur', function () {
846
+ render(___EmotionJSX(ComboBoxWithAddOption, null));
847
+ var input = screen.queryByRole('combobox');
848
+ var inputValue = 'New value';
849
+ userEvent.type(input, inputValue);
850
+ expect(input).toHaveValue(inputValue); // blur input
851
+
852
+ userEvent.tab();
853
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
854
+ expect(screen.queryByRole('option')).not.toBeInTheDocument();
855
+ userEvent.click(input);
856
+ expect(input).toHaveValue(inputValue);
857
+ expect(screen.queryByRole('listbox')).toBeInTheDocument();
858
+ var options = screen.queryAllByRole('option');
859
+ expect(options[options.length - 1]).toHaveTextContent(inputValue);
860
+ });
780
861
  test('should have no accessibility violations', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
781
862
  var _getComponent6, container, results;
782
863
 
783
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
864
+ return _regeneratorRuntime.wrap(function _callee4$(_context5) {
784
865
  while (1) {
785
- switch (_context4.prev = _context4.next) {
866
+ switch (_context5.prev = _context5.next) {
786
867
  case 0:
787
868
  jest.useRealTimers();
788
869
  _getComponent6 = getComponent(), container = _getComponent6.container;
789
- _context4.next = 4;
870
+ _context5.next = 4;
790
871
  return axe(container);
791
872
 
792
873
  case 4:
793
- results = _context4.sent;
874
+ results = _context5.sent;
794
875
  expect(results).toHaveNoViolations();
795
876
 
796
877
  case 6:
797
878
  case "end":
798
- return _context4.stop();
879
+ return _context5.stop();
799
880
  }
800
881
  }
801
882
  }, _callee4);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pingux/astro",
3
- "version": "1.16.1-alpha.0",
3
+ "version": "1.17.0-alpha.0",
4
4
  "description": "PingUX themeable React component library",
5
5
  "author": "ux-development@pingidentity.com",
6
6
  "license": "Apache-2.0",