@commercetools-uikit/creatable-select-field 0.0.0-canary-2021830134526

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.
@@ -0,0 +1,449 @@
1
+ import _Reflect$construct from '@babel/runtime-corejs3/core-js-stable/reflect/construct';
2
+ import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys';
3
+ import _Object$getOwnPropertySymbols from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols';
4
+ import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
5
+ import _Object$getOwnPropertyDescriptor from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor';
6
+ import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each';
7
+ import _Object$getOwnPropertyDescriptors from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors';
8
+ import _Object$defineProperties from '@babel/runtime-corejs3/core-js-stable/object/define-properties';
9
+ import _Object$defineProperty from '@babel/runtime-corejs3/core-js-stable/object/define-property';
10
+ import _defineProperty from '@babel/runtime-corejs3/helpers/esm/defineProperty';
11
+ import _classCallCheck from '@babel/runtime-corejs3/helpers/esm/classCallCheck';
12
+ import _createClass from '@babel/runtime-corejs3/helpers/esm/createClass';
13
+ import _inherits from '@babel/runtime-corejs3/helpers/esm/inherits';
14
+ import _possibleConstructorReturn from '@babel/runtime-corejs3/helpers/esm/possibleConstructorReturn';
15
+ import _getPrototypeOf from '@babel/runtime-corejs3/helpers/esm/getPrototypeOf';
16
+ import _someInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/some';
17
+ import _Object$values from '@babel/runtime-corejs3/core-js-stable/object/values';
18
+ import _concatInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/concat';
19
+ import { Component } from 'react';
20
+ import PropTypes from 'prop-types';
21
+ import requiredIf from 'react-required-if';
22
+ import { createSequentialId, filterDataAttributes, getFieldId, SafeHTMLElement } from '@commercetools-uikit/utils';
23
+ import Constraints from '@commercetools-uikit/constraints';
24
+ import Spacings from '@commercetools-uikit/spacings';
25
+ import FieldLabel from '@commercetools-uikit/field-label';
26
+ import CreatableSelectInput from '@commercetools-uikit/creatable-select-input';
27
+ import FieldErrors from '@commercetools-uikit/field-errors';
28
+ import { jsx, jsxs } from '@emotion/react/jsx-runtime';
29
+
30
+ function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
31
+
32
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context8; _forEachInstanceProperty(_context8 = ownKeys(Object(source), true)).call(_context8, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context9; _forEachInstanceProperty(_context9 = ownKeys(Object(source))).call(_context9, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
33
+
34
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
35
+
36
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
37
+ var sequentialId = createSequentialId('creatable-select-field-');
38
+
39
+ var hasErrors = function hasErrors(errors) {
40
+ var _context;
41
+
42
+ return errors && _someInstanceProperty(_context = _Object$values(errors)).call(_context, Boolean);
43
+ };
44
+
45
+ var CreatableSelectField = /*#__PURE__*/function (_Component) {
46
+ _inherits(CreatableSelectField, _Component);
47
+
48
+ var _super = _createSuper(CreatableSelectField);
49
+
50
+ function CreatableSelectField() {
51
+ var _context2;
52
+
53
+ var _this;
54
+
55
+ _classCallCheck(this, CreatableSelectField);
56
+
57
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
58
+ args[_key] = arguments[_key];
59
+ }
60
+
61
+ _this = _super.call.apply(_super, _concatInstanceProperty(_context2 = [this]).call(_context2, args));
62
+ _this.state = {
63
+ // We generate an id in case no id is provided by the parent to attach the
64
+ // label to the input component.
65
+ id: _this.props.id
66
+ };
67
+ return _this;
68
+ }
69
+
70
+ _createClass(CreatableSelectField, [{
71
+ key: "render",
72
+ value: function render() {
73
+ var hasError = CreatableSelectInput.isTouched(this.props.touched) && hasErrors(this.props.errors);
74
+ return jsx(Constraints.Horizontal, {
75
+ max: this.props.horizontalConstraint,
76
+ children: jsxs(Spacings.Stack, {
77
+ scale: "xs",
78
+ children: [jsx(FieldLabel, {
79
+ title: this.props.title,
80
+ hint: this.props.hint,
81
+ description: this.props.description,
82
+ onInfoButtonClick: this.props.onInfoButtonClick,
83
+ hintIcon: this.props.hintIcon,
84
+ badge: this.props.badge,
85
+ hasRequiredIndicator: this.props.isRequired,
86
+ htmlFor: this.state.id
87
+ }), jsx(CreatableSelectInput, _objectSpread({
88
+ horizontalConstraint: "scale",
89
+ hasError: hasError,
90
+ "aria-label": this.props['aria-label'],
91
+ "aria-labelledby": this.props['aria-labelledby'],
92
+ isAutofocussed: this.props.isAutofocussed,
93
+ backspaceRemovesValue: this.props.backspaceRemovesValue,
94
+ components: this.props.components,
95
+ filterOption: this.props.filterOption,
96
+ id: this.state.id,
97
+ containerId: this.props.containerId,
98
+ isClearable: this.props.isClearable,
99
+ isDisabled: this.props.isDisabled,
100
+ isOptionDisabled: this.props.isOptionDisabled,
101
+ isMulti: this.props.isMulti,
102
+ isSearchable: this.props.isSearchable,
103
+ hasWarning: this.props.hasWarning,
104
+ maxMenuHeight: this.props.maxMenuHeight,
105
+ menuPortalTarget: this.props.menuPortalTarget,
106
+ menuPortalZIndex: this.props.menuPortalZIndex,
107
+ menuShouldBlockScroll: this.props.menuShouldBlockScroll,
108
+ name: this.props.name,
109
+ noOptionsMessage: this.props.noOptionsMessage,
110
+ onBlur: this.props.onBlur,
111
+ onChange: this.props.onChange,
112
+ onFocus: this.props.onFocus,
113
+ onInputChange: this.props.onInputChange,
114
+ options: this.props.options,
115
+ placeholder: this.props.placeholder,
116
+ tabIndex: this.props.tabIndex,
117
+ tabSelectsValue: this.props.tabSelectsValue,
118
+ value: this.props.value // Creatable props
119
+ ,
120
+ allowCreateWhileLoading: this.props.allowCreateWhileLoading,
121
+ formatCreateLabel: this.props.formatCreateLabel,
122
+ isValidNewOption: this.props.isValidNewOption,
123
+ getNewOptionData: this.props.getNewOptionData,
124
+ onCreateOption: this.props.onCreateOption,
125
+ createOptionPosition: this.props.createOptionPosition,
126
+ showOptionGroupDivider: this.props.showOptionGroupDivider,
127
+ iconLeft: this.props.iconLeft
128
+ }, filterDataAttributes(this.props))), jsx(FieldErrors, {
129
+ errors: this.props.errors,
130
+ isVisible: hasError,
131
+ renderError: this.props.renderError
132
+ })]
133
+ })
134
+ });
135
+ }
136
+ }]);
137
+
138
+ return CreatableSelectField;
139
+ }(Component);
140
+
141
+ CreatableSelectField.displayName = 'CreatableSelectField';
142
+ CreatableSelectField.defaultProps = {
143
+ horizontalConstraint: 'scale'
144
+ };
145
+
146
+ CreatableSelectField.getDerivedStateFromProps = function (props, state) {
147
+ return {
148
+ id: getFieldId(props, state, sequentialId)
149
+ };
150
+ };
151
+ CreatableSelectField.propTypes = process.env.NODE_ENV !== "production" ? {
152
+ // CreatableSelectField
153
+
154
+ /**
155
+ * Used as HTML id property. An id is auto-generated when it is not specified.
156
+ */
157
+ id: PropTypes.string,
158
+
159
+ /**
160
+ * Horizontal size limit of the input fields.
161
+ */
162
+ horizontalConstraint: PropTypes.oneOf([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto']),
163
+
164
+ /**
165
+ * A map of errors. Error messages for known errors are rendered automatically.
166
+ * <br />
167
+ * Unknown errors will be forwarded to `renderError`
168
+ */
169
+ errors: PropTypes.shape({
170
+ missing: PropTypes.bool
171
+ }),
172
+
173
+ /**
174
+ * Called with custom errors. This function can return a message which will be wrapped in an ErrorMessage. It can also return null to show no error.
175
+ * <br />
176
+ * Signature: `(key, error) => React.node`
177
+ */
178
+ renderError: PropTypes.func,
179
+
180
+ /**
181
+ * Indicates if the value is required. Shows an the "required asterisk" if so.
182
+ */
183
+ isRequired: PropTypes.bool,
184
+
185
+ /**
186
+ * Indicates whether the field was touched. Errors will only be shown when the field was touched.
187
+ */
188
+ touched: function touched(props) {
189
+ var _context3, _context4, _context5;
190
+
191
+ for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
192
+ rest[_key2 - 1] = arguments[_key2];
193
+ }
194
+
195
+ return props.isMulti ? PropTypes.arrayOf.apply(PropTypes, _concatInstanceProperty(_context3 = [PropTypes.bool]).call(_context3, rest)).apply(void 0, _concatInstanceProperty(_context4 = [props]).call(_context4, rest)) : PropTypes.bool.apply(PropTypes, _concatInstanceProperty(_context5 = [props]).call(_context5, rest));
196
+ },
197
+ // CreatableSelectInput
198
+
199
+ /**
200
+ * Aria label (for assistive tech)
201
+ */
202
+ 'aria-label': PropTypes.string,
203
+
204
+ /**
205
+ * HTML ID of an element that should be used as the label (for assistive tech)
206
+ */
207
+ 'aria-labelledby': PropTypes.string,
208
+
209
+ /**
210
+ * Focus the control when it is mounted
211
+ */
212
+ isAutofocussed: PropTypes.bool,
213
+
214
+ /**
215
+ * Remove the currently focused option when the user presses backspace
216
+ */
217
+ backspaceRemovesValue: PropTypes.bool,
218
+
219
+ /**
220
+ * Map of components to overwrite the default ones, see [what components you can override](https://react-select.com/components)
221
+ */
222
+ components: PropTypes.objectOf(PropTypes.func),
223
+
224
+ /**
225
+ * Custom method to filter whether an option should be displayed in the menu
226
+ */
227
+ filterOption: PropTypes.func,
228
+
229
+ /**
230
+ * The id to set on the SelectContainer component
231
+ */
232
+ containerId: PropTypes.string,
233
+
234
+ /**
235
+ * Is the select value clearable
236
+ */
237
+ isClearable: PropTypes.bool,
238
+
239
+ /**
240
+ * Is the select disabled
241
+ */
242
+ isDisabled: PropTypes.bool,
243
+
244
+ /**
245
+ * Is the select read-only
246
+ */
247
+ // eslint-disable-next-line react/no-unused-prop-types
248
+ isReadOnly: PropTypes.bool,
249
+
250
+ /**
251
+ * Override the built-in logic to detect whether an option is disabled
252
+ */
253
+ isOptionDisabled: PropTypes.func,
254
+
255
+ /**
256
+ * Support multiple selected options
257
+ */
258
+ isMulti: PropTypes.bool,
259
+
260
+ /**
261
+ * Whether to enable search functionality
262
+ */
263
+ isSearchable: PropTypes.bool,
264
+ hasWarning: PropTypes.bool,
265
+
266
+ /**
267
+ * Maximum height of the menu before scrolling
268
+ */
269
+ maxMenuHeight: PropTypes.number,
270
+
271
+ /**
272
+ * Dom element to portal the select menu to
273
+ */
274
+ menuPortalTarget: PropTypes.instanceOf(SafeHTMLElement),
275
+
276
+ /**
277
+ * z-index value for the menu portal
278
+ */
279
+ menuPortalZIndex: PropTypes.number,
280
+
281
+ /**
282
+ * whether the menu should block scroll while open
283
+ */
284
+ menuShouldBlockScroll: PropTypes.bool,
285
+
286
+ /**
287
+ * Name of the HTML Input (optional - without this, no input will be rendered)
288
+ */
289
+ name: PropTypes.string,
290
+
291
+ /**
292
+ * Can be used to render a custom value when there are no options (either because of no search results, or all options have been used, or there were none in the first place).
293
+ * <br/>
294
+ * Gets called with `{ inputValue: String }`. `inputValue` will be an empty string when no search text is present.
295
+ * <br />
296
+ * Signature: `({ inputValue }) => string`
297
+ */
298
+ noOptionsMessage: PropTypes.func,
299
+
300
+ /**
301
+ * Handle blur events on the control
302
+ */
303
+ onBlur: PropTypes.func,
304
+
305
+ /**
306
+ * Called with a fake event when value changes.
307
+ * <br />
308
+ * The event's `target.name` will be the name supplied in props. The event's `target.value` will hold the value. The value will be the selected option, or an array of options in case `isMulti` is `true`.
309
+ * <br />
310
+ * Signature: `(event) => void`
311
+ */
312
+ onChange: PropTypes.func,
313
+
314
+ /**
315
+ * Handle focus events on the control
316
+ */
317
+ onFocus: PropTypes.func,
318
+
319
+ /**
320
+ * Handle change events on the input
321
+ */
322
+ onInputChange: PropTypes.func,
323
+
324
+ /**
325
+ * Array of options that populate the select menu
326
+ */
327
+ options: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape({
328
+ value: PropTypes.string.isRequired
329
+ }), PropTypes.shape({
330
+ options: PropTypes.arrayOf(PropTypes.shape({
331
+ value: PropTypes.string.isRequired
332
+ }))
333
+ })])),
334
+
335
+ /**
336
+ * Placeholder text for the select value
337
+ */
338
+ placeholder: PropTypes.string,
339
+
340
+ /**
341
+ * Sets the tabIndex attribute on the input
342
+ */
343
+ tabIndex: PropTypes.string,
344
+
345
+ /**
346
+ * Select the currently focused option when the user presses tab
347
+ */
348
+ tabSelectsValue: PropTypes.bool,
349
+
350
+ /**
351
+ * The value of the select; reflected by the selected option
352
+ */
353
+ value: function value(props) {
354
+ var _context6, _context7;
355
+
356
+ for (var _len3 = arguments.length, rest = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
357
+ rest[_key3 - 1] = arguments[_key3];
358
+ }
359
+
360
+ return props.isMulti ? PropTypes.arrayOf(PropTypes.shape({
361
+ value: PropTypes.string.isRequired
362
+ })).apply(void 0, _concatInstanceProperty(_context6 = [props]).call(_context6, rest)) : PropTypes.shape({
363
+ value: PropTypes.string.isRequired
364
+ }).apply(void 0, _concatInstanceProperty(_context7 = [props]).call(_context7, rest));
365
+ },
366
+ showOptionGroupDivider: PropTypes.bool,
367
+ // Creatable props
368
+
369
+ /**
370
+ * Allow options to be created while the isLoading prop is true. Useful to prevent the "create new ..." option being displayed while async results are still being loaded.
371
+ */
372
+ allowCreateWhileLoading: PropTypes.bool,
373
+
374
+ /**
375
+ * Gets the label for the "create new ..." option in the menu. Is given the current input value.
376
+ */
377
+ formatCreateLabel: PropTypes.func,
378
+
379
+ /**
380
+ * Determines whether the "create new ..." option should be displayed based on the current input value, select value and options array.
381
+ */
382
+ isValidNewOption: PropTypes.func,
383
+
384
+ /**
385
+ * Returns the data for the new option when it is created. Used to display the value, and is passed to onChange.
386
+ */
387
+ getNewOptionData: PropTypes.func,
388
+
389
+ /**
390
+ * If provided, this will be called with the input value when a new option is created, and onChange will not be called. Use this when you need more control over what happens when new options are created.
391
+ */
392
+ onCreateOption: PropTypes.func,
393
+
394
+ /**
395
+ * Sets the position of the createOption element in your options list.
396
+ */
397
+ createOptionPosition: PropTypes.string,
398
+ // LabelField
399
+
400
+ /**
401
+ * Title of the label
402
+ */
403
+ title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
404
+
405
+ /**
406
+ * Hint for the label. Provides a supplementary but important information regarding the behaviour of the input (e.g warn about uniqueness of a field, when it can only be set once), whereas `description` can describe it in more depth. Can also receive a `hintIcon`.
407
+ */
408
+ hint: requiredIf(PropTypes.oneOfType([PropTypes.string, PropTypes.node]), function (props) {
409
+ return props.hintIcon;
410
+ }),
411
+
412
+ /**
413
+ * Provides a description for the title.
414
+ */
415
+ description: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
416
+
417
+ /**
418
+ * Function called when info button is pressed.
419
+ * <br />
420
+ * Info button will only be visible when this prop is passed.
421
+ * <br />
422
+ * Signature: `(event) => void`
423
+ */
424
+ onInfoButtonClick: PropTypes.func,
425
+
426
+ /**
427
+ * Icon to be displayed beside the hint text.
428
+ * <br />
429
+ * Will only get rendered when `hint` is passed as well.
430
+ */
431
+ hintIcon: PropTypes.node,
432
+
433
+ /**
434
+ * Badge to be displayed beside the label.
435
+ * <br />
436
+ * Might be used to display additional information about the content of the field (E.g verified email)
437
+ */
438
+ badge: PropTypes.node,
439
+
440
+ /**
441
+ * Icon to display on the left of the placeholder text and selected value. Has no effect when isMulti is enabled.
442
+ */
443
+ iconLeft: PropTypes.node
444
+ } : {};
445
+
446
+ // NOTE: This string will be replaced in the `prepare` script by the `scripts/version.js` file.
447
+ var version = '0.0.0-canary-2021830134526';
448
+
449
+ export { CreatableSelectField as default, version };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@commercetools-uikit/creatable-select-field",
3
+ "description": "A controlled input component with validation states and a label getting a selection from the user, and where options can also be created by the user.",
4
+ "version": "0.0.0-canary-2021830134526",
5
+ "bugs": "https://github.com/commercetools/ui-kit/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/commercetools/ui-kit.git",
9
+ "directory": "packages/components/fields/creatable-select-field"
10
+ },
11
+ "homepage": "https://uikit.commercetools.com",
12
+ "keywords": [
13
+ "javascript",
14
+ "design system",
15
+ "react",
16
+ "uikit"
17
+ ],
18
+ "license": "MIT",
19
+ "private": false,
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "sideEffects": false,
24
+ "main": "dist/commercetools-uikit-creatable-select-field.cjs.js",
25
+ "module": "dist/commercetools-uikit-creatable-select-field.esm.js",
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "scripts": {
30
+ "prepare": "../../../../scripts/version.js replace"
31
+ },
32
+ "dependencies": {
33
+ "@babel/runtime": "7.14.8",
34
+ "@babel/runtime-corejs3": "7.14.9",
35
+ "@commercetools-uikit/constraints": "0.0.0-canary-2021830134526",
36
+ "@commercetools-uikit/creatable-select-input": "0.0.0-canary-2021830134526",
37
+ "@commercetools-uikit/design-system": "0.0.0-canary-2021830134526",
38
+ "@commercetools-uikit/field-errors": "0.0.0-canary-2021830134526",
39
+ "@commercetools-uikit/field-label": "0.0.0-canary-2021830134526",
40
+ "@commercetools-uikit/spacings": "0.0.0-canary-2021830134526",
41
+ "@commercetools-uikit/utils": "12.2.0",
42
+ "@emotion/react": "^11.4.0",
43
+ "@emotion/styled": "^11.3.0",
44
+ "prop-types": "15.7.2",
45
+ "react-required-if": "1.0.3"
46
+ },
47
+ "devDependencies": {
48
+ "react": "17.0.2"
49
+ },
50
+ "peerDependencies": {
51
+ "react": "17.x"
52
+ }
53
+ }