@carbon/react 1.8.0 → 1.9.0-rc.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.
@@ -5,21 +5,18 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import { defineProperty as _defineProperty, extends as _extends, inherits as _inherits, createSuper as _createSuper, classCallCheck as _classCallCheck, assertThisInitialized as _assertThisInitialized, possibleConstructorReturn as _possibleConstructorReturn, createClass as _createClass, objectWithoutProperties as _objectWithoutProperties } from '../../_virtual/_rollupPluginBabelHelpers.js';
9
- import PropTypes from 'prop-types';
10
- import React__default, { Component } from 'react';
11
- import cx from 'classnames';
8
+ import { defineProperty as _defineProperty, objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
12
9
  import { Subtract, Add } from '@carbon/icons-react';
13
- import * as FeatureFlags from '@carbon/feature-flags';
14
- import mergeRefs from '../../tools/mergeRefs.js';
15
- import requiredIfValueExists from '../../prop-types/requiredIfValueExists.js';
10
+ import cx from 'classnames';
11
+ import PropTypes from 'prop-types';
12
+ import React__default, { useState, useRef } from 'react';
13
+ import { useFeatureFlag } from '../FeatureFlags/index.js';
14
+ import { useMergedRefs } from '../../internal/useMergedRefs.js';
16
15
  import { useNormalizedInputProps } from '../../internal/useNormalizedInputProps.js';
17
- import { useControlledStateWithValue } from '../../internal/FeatureFlags.js';
18
- import { PrefixContext } from '../../internal/usePrefix.js';
16
+ import { usePrefix } from '../../internal/usePrefix.js';
19
17
  import deprecate from '../../prop-types/deprecate.js';
20
- import { FeatureFlagContext } from '../FeatureFlags/index.js';
21
18
 
22
- var _excluded = ["className", "disabled", "iconDescription", "id", "hideLabel", "hideSteppers", "label", "max", "min", "step", "value", "readOnly", "invalid", "invalidText", "warn", "warnText", "helperText", "ariaLabel", "light", "allowEmpty", "innerRef", "translateWithId", "isMobile", "size", "defaultValue"];
19
+ var _excluded = ["allowEmpty", "className", "disabled", "defaultValue", "helperText", "hideLabel", "hideSteppers", "iconDescription", "id", "label", "invalid", "invalidText", "light", "max", "min", "onChange", "onClick", "onKeyUp", "readOnly", "size", "step", "translateWithId", "warn", "warnText", "value"];
23
20
 
24
21
  var _defaultTranslations, _Subtract, _Add;
25
22
  var translationIds = {
@@ -27,331 +24,226 @@ var translationIds = {
27
24
  'decrement.number': 'decrement.number'
28
25
  };
29
26
  var defaultTranslations = (_defaultTranslations = {}, _defineProperty(_defaultTranslations, translationIds['increment.number'], 'Increment number'), _defineProperty(_defaultTranslations, translationIds['decrement.number'], 'Decrement number'), _defaultTranslations);
27
+ var NumberInput = /*#__PURE__*/React__default.forwardRef(function NumberInput(props, forwardRef) {
28
+ var _cx, _cx3;
29
+
30
+ var enabled = useFeatureFlag('enable-v11-release');
31
+
32
+ var _props$allowEmpty = props.allowEmpty,
33
+ allowEmpty = _props$allowEmpty === void 0 ? false : _props$allowEmpty,
34
+ customClassName = props.className,
35
+ _props$disabled = props.disabled,
36
+ disabled = _props$disabled === void 0 ? false : _props$disabled,
37
+ defaultValue = props.defaultValue,
38
+ _props$helperText = props.helperText,
39
+ helperText = _props$helperText === void 0 ? '' : _props$helperText,
40
+ _props$hideLabel = props.hideLabel,
41
+ hideLabel = _props$hideLabel === void 0 ? false : _props$hideLabel,
42
+ hideSteppers = props.hideSteppers,
43
+ _props$iconDescriptio = props.iconDescription,
44
+ iconDescription = _props$iconDescriptio === void 0 ? enabled ? undefined : 'choose a number' : _props$iconDescriptio,
45
+ id = props.id,
46
+ label = props.label,
47
+ _props$invalid = props.invalid,
48
+ invalid = _props$invalid === void 0 ? false : _props$invalid,
49
+ _props$invalidText = props.invalidText,
50
+ invalidText = _props$invalidText === void 0 ? enabled ? undefined : 'Provide invalidText' : _props$invalidText,
51
+ light = props.light,
52
+ max = props.max,
53
+ min = props.min,
54
+ onChange = props.onChange,
55
+ _onClick = props.onClick,
56
+ onKeyUp = props.onKeyUp,
57
+ readOnly = props.readOnly,
58
+ _props$size = props.size,
59
+ size = _props$size === void 0 ? 'md' : _props$size,
60
+ _props$step = props.step,
61
+ step = _props$step === void 0 ? 1 : _props$step,
62
+ _props$translateWithI = props.translateWithId,
63
+ t = _props$translateWithI === void 0 ? function (id) {
64
+ return defaultTranslations[id];
65
+ } : _props$translateWithI,
66
+ _props$warn = props.warn,
67
+ warn = _props$warn === void 0 ? false : _props$warn,
68
+ _props$warnText = props.warnText,
69
+ warnText = _props$warnText === void 0 ? '' : _props$warnText,
70
+ controlledValue = props.value,
71
+ rest = _objectWithoutProperties(props, _excluded);
72
+
73
+ var prefix = usePrefix();
74
+
75
+ var _useState = useState(function () {
76
+ if (controlledValue !== undefined) {
77
+ return controlledValue;
78
+ }
30
79
 
31
- var capMin = function capMin(min, value) {
32
- return isNaN(min) || !min && min !== 0 || isNaN(value) || !value && value !== 0 ? value : Math.max(min, value);
33
- };
80
+ if (defaultValue !== undefined) {
81
+ return defaultValue;
82
+ }
34
83
 
35
- var capMax = function capMax(max, value) {
36
- return isNaN(max) || !max && max !== 0 || isNaN(value) || !value && value !== 0 ? value : Math.min(max, value);
37
- };
84
+ return 0;
85
+ }),
86
+ _useState2 = _slicedToArray(_useState, 2),
87
+ value = _useState2[0],
88
+ setValue = _useState2[1];
89
+
90
+ var _useState3 = useState(controlledValue),
91
+ _useState4 = _slicedToArray(_useState3, 2),
92
+ prevControlledValue = _useState4[0],
93
+ setPrevControlledValue = _useState4[1];
94
+
95
+ var inputRef = useRef(null);
96
+ var ref = useMergedRefs([forwardRef, inputRef]);
97
+ var numberInputClasses = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "--number"), true), _defineProperty(_cx, "".concat(prefix, "--number--helpertext"), true), _defineProperty(_cx, "".concat(prefix, "--number--readonly"), readOnly), _defineProperty(_cx, "".concat(prefix, "--number--light"), light), _defineProperty(_cx, "".concat(prefix, "--number--nolabel"), hideLabel), _defineProperty(_cx, "".concat(prefix, "--number--nosteppers"), hideSteppers), _defineProperty(_cx, "".concat(prefix, "--number--").concat(size), size), _defineProperty(_cx, customClassName, !enabled), _cx));
98
+ var isInputValid = getInputValidity({
99
+ allowEmpty: allowEmpty,
100
+ invalid: invalid,
101
+ value: value,
102
+ max: max,
103
+ min: min
104
+ });
105
+ var normalizedProps = useNormalizedInputProps({
106
+ id: id,
107
+ readOnly: readOnly,
108
+ disabled: disabled,
109
+ invalid: !isInputValid,
110
+ invalidText: invalidText,
111
+ warn: warn,
112
+ warnText: warnText
113
+ });
114
+ var _ref = [t('increment.number'), t('decrement.number')],
115
+ incrementNumLabel = _ref[0],
116
+ decrementNumLabel = _ref[1];
117
+ var wrapperClasses = cx("".concat(prefix, "--number__input-wrapper"), _defineProperty({}, "".concat(prefix, "--number__input-wrapper--warning"), normalizedProps.warn));
118
+ var iconClasses = cx((_cx3 = {}, _defineProperty(_cx3, "".concat(prefix, "--number__invalid"), normalizedProps.invalid || normalizedProps.warn), _defineProperty(_cx3, "".concat(prefix, "--number__invalid--warning"), normalizedProps.warn), _defineProperty(_cx3, "".concat(prefix, "--number__readonly-icon"), readOnly), _cx3));
119
+
120
+ if (controlledValue !== prevControlledValue) {
121
+ setValue(controlledValue);
122
+ setPrevControlledValue(controlledValue);
123
+ }
38
124
 
39
- var NumberInput = /*#__PURE__*/function (_Component) {
40
- _inherits(NumberInput, _Component);
41
-
42
- var _super = _createSuper(NumberInput);
43
-
44
- function NumberInput(props) {
45
- var _this;
46
-
47
- _classCallCheck(this, NumberInput);
48
-
49
- _this = _super.call(this, props);
50
-
51
- _defineProperty(_assertThisInitialized(_this), "_inputRef", null);
52
-
53
- _defineProperty(_assertThisInitialized(_this), "handleChange", function (evt) {
54
- var _this$props = _this.props,
55
- disabled = _this$props.disabled,
56
- onChange = _this$props.onChange;
57
-
58
- if (!disabled) {
59
- evt.persist();
60
- evt.imaginaryTarget = _this._inputRef;
61
- var prevValue = _this.state.value;
62
- var value = evt.target.value;
63
- var direction = prevValue < value ? 'up' : 'down';
64
-
65
- _this.setState({
66
- value: value
67
- }, function () {
68
- if (useControlledStateWithValue) {
69
- onChange(evt, {
70
- value: value,
71
- direction: direction
72
- });
73
- } else if (onChange) {
74
- onChange(evt, {
75
- value: value,
76
- direction: direction
77
- });
78
- }
79
- });
80
- }
81
- });
82
-
83
- _defineProperty(_assertThisInitialized(_this), "handleArrowClick", function (evt, direction) {
84
- var value = typeof _this.state.value === 'string' ? Number(_this.state.value) : _this.state.value;
85
- var _this$props2 = _this.props,
86
- disabled = _this$props2.disabled,
87
- min = _this$props2.min,
88
- max = _this$props2.max,
89
- step = _this$props2.step,
90
- onChange = _this$props2.onChange,
91
- onClick = _this$props2.onClick;
92
- var conditional = direction === 'down' ? min !== undefined && value > min || min === undefined : max !== undefined && value < max || max === undefined;
93
-
94
- if (!disabled && conditional) {
95
- value = direction === 'down' ? value - step : value + step;
96
- value = capMax(max, capMin(min, value));
97
- evt.persist();
98
- evt.imaginaryTarget = _this._inputRef;
99
-
100
- _this.setState({
101
- value: value
102
- }, function () {
103
- //TO-DO v11: update these events to return the same things --> evt, {value, direction}
104
- if (useControlledStateWithValue) {
105
- onClick && onClick(evt, {
106
- value: value,
107
- direction: direction
108
- });
109
- onChange && onChange(evt, {
110
- value: value,
111
- direction: direction
112
- });
113
- } else {
114
- // value added as a 3rd argument rather than in same obj so it doesn't break in v10
115
- onClick && onClick(evt, direction, value);
116
- onChange && onChange(evt, direction, value);
117
- }
118
- });
119
- }
120
- });
125
+ var ariaDescribedBy = null;
121
126
 
122
- _defineProperty(_assertThisInitialized(_this), "_handleInputRef", function (ref) {
123
- _this._inputRef = ref;
124
- });
127
+ if (normalizedProps.invalid) {
128
+ ariaDescribedBy = normalizedProps.invalidId;
129
+ }
125
130
 
126
- _this.isControlled = props.value !== undefined;
131
+ if (normalizedProps.warn) {
132
+ ariaDescribedBy = normalizedProps.warnId;
133
+ }
127
134
 
128
- if (useControlledStateWithValue && _this.isControlled) {
129
- // Skips the logic of setting initial state if this component is controlled
130
- _this.state = {};
131
- return _possibleConstructorReturn(_this);
135
+ function handleOnChange(event) {
136
+ if (disabled) {
137
+ return;
132
138
  }
133
139
 
134
- var _value = useControlledStateWithValue || typeof props.defaultValue !== 'undefined' ? props.defaultValue : props.value;
135
-
136
- _value = _value === undefined ? 0 : _value;
140
+ var state = {
141
+ value: event.target.value,
142
+ direction: value < event.target.value ? 'up' : 'down'
143
+ };
144
+ setValue(state.value);
137
145
 
138
- if (props.min || props.min === 0) {
139
- _value = Math.max(props.min, _value);
146
+ if (onChange) {
147
+ onChange(event, state);
140
148
  }
141
-
142
- _this.state = {
143
- value: _value
144
- };
145
- return _this;
146
149
  }
147
150
 
148
- _createClass(NumberInput, [{
149
- key: "render",
150
- value: function render() {
151
- var _this2 = this;
152
-
153
- var _this$props3 = this.props,
154
- className = _this$props3.className,
155
- disabled = _this$props3.disabled,
156
- iconDescription = _this$props3.iconDescription,
157
- id = _this$props3.id,
158
- hideLabel = _this$props3.hideLabel,
159
- hideSteppers = _this$props3.hideSteppers,
160
- label = _this$props3.label,
161
- max = _this$props3.max,
162
- min = _this$props3.min,
163
- step = _this$props3.step,
164
- value = _this$props3.value,
165
- readOnly = _this$props3.readOnly,
166
- invalid = _this$props3.invalid,
167
- invalidText = _this$props3.invalidText,
168
- warn = _this$props3.warn,
169
- warnText = _this$props3.warnText,
170
- helperText = _this$props3.helperText,
171
- ariaLabel = _this$props3.ariaLabel,
172
- light = _this$props3.light,
173
- allowEmpty = _this$props3.allowEmpty,
174
- ref = _this$props3.innerRef,
175
- t = _this$props3.translateWithId,
176
- isMobile = _this$props3.isMobile,
177
- size = _this$props3.size;
178
- _this$props3.defaultValue;
179
- var other = _objectWithoutProperties(_this$props3, _excluded);
180
-
181
- var scope = this.context;
182
- var enabled;
183
-
184
- if (scope.enabled) {
185
- enabled = scope.enabled('enable-v11-release');
151
+ return /*#__PURE__*/React__default.createElement("div", {
152
+ className: cx("".concat(prefix, "--form-item"), _defineProperty({}, customClassName, enabled))
153
+ }, /*#__PURE__*/React__default.createElement("div", {
154
+ className: numberInputClasses,
155
+ "data-invalid": normalizedProps.invalid ? true : undefined
156
+ }, /*#__PURE__*/React__default.createElement(Label, {
157
+ disabled: normalizedProps.disabled,
158
+ hideLabel: hideLabel,
159
+ id: id,
160
+ label: label
161
+ }), /*#__PURE__*/React__default.createElement("div", {
162
+ className: wrapperClasses
163
+ }, /*#__PURE__*/React__default.createElement("input", _extends({}, rest, {
164
+ "data-invalid": normalizedProps.invalid ? true : undefined,
165
+ "aria-invalid": normalizedProps.invalid,
166
+ "aria-describedby": ariaDescribedBy,
167
+ disabled: normalizedProps.disabled,
168
+ ref: ref,
169
+ id: id,
170
+ max: max,
171
+ min: min,
172
+ onClick: _onClick,
173
+ onChange: handleOnChange,
174
+ onKeyUp: onKeyUp,
175
+ pattern: "[0-9]*",
176
+ readOnly: readOnly,
177
+ step: step,
178
+ type: "number",
179
+ value: value
180
+ })), normalizedProps.icon ? /*#__PURE__*/React__default.createElement(normalizedProps.icon, {
181
+ className: iconClasses
182
+ }) : null, !hideSteppers && /*#__PURE__*/React__default.createElement("div", {
183
+ className: "".concat(prefix, "--number__controls")
184
+ }, /*#__PURE__*/React__default.createElement("button", {
185
+ "aria-label": decrementNumLabel || iconDescription,
186
+ className: "".concat(prefix, "--number__control-btn down-icon"),
187
+ disabled: disabled,
188
+ onClick: function onClick(event) {
189
+ var state = {
190
+ value: clamp(max, min, value - step),
191
+ direction: 'down'
192
+ };
193
+ setValue(state.value);
194
+
195
+ if (onChange) {
196
+ onChange(event, state);
186
197
  }
187
198
 
188
- return /*#__PURE__*/React__default.createElement(PrefixContext.Consumer, null, function (prefix) {
189
- var _classNames, _classNames3, _classNames5, _normalizedProps$icon;
190
-
191
- var numberInputClasses = cx("".concat(prefix, "--number ").concat(prefix, "--number--helpertext"), [enabled ? null : className], (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--number--readonly"), readOnly), _defineProperty(_classNames, "".concat(prefix, "--number--light"), light), _defineProperty(_classNames, "".concat(prefix, "--number--nolabel"), hideLabel), _defineProperty(_classNames, "".concat(prefix, "--number--nosteppers"), hideSteppers), _defineProperty(_classNames, "".concat(prefix, "--number--mobile"), isMobile), _defineProperty(_classNames, "".concat(prefix, "--number--").concat(size), size), _classNames));
192
- var isInputInvalid; // If the user supplied `invalid` through props, we'll defer to the passed in value
193
-
194
- if (invalid) {
195
- isInputInvalid = true;
196
- } else {
197
- // Otherwise, if we don't allow an empty value then we check to see
198
- // if the value is empty, or if it is out of range
199
- if (!allowEmpty && _this2.state.value === '') {
200
- isInputInvalid = true;
201
- } else {
202
- if (_this2.state.value !== '' && (_this2.state.value > max || _this2.state.value < min)) {
203
- isInputInvalid = true;
204
- }
205
- }
206
- }
207
-
208
- var normalizedProps = useNormalizedInputProps({
209
- id: id,
210
- readOnly: readOnly,
211
- disabled: disabled,
212
- invalid: isInputInvalid,
213
- invalidText: invalidText,
214
- warn: warn,
215
- warnText: warnText
216
- });
217
- var props = {
218
- disabled: normalizedProps.disabled,
219
- id: id,
220
- max: max,
221
- min: min,
222
- step: step,
223
- onChange: _this2.handleChange,
224
- value: useControlledStateWithValue && _this2.isControlled ? value : _this2.state.value,
225
- readOnly: readOnly,
226
- 'aria-label': label ? null : ariaLabel
227
- };
228
- var buttonProps = {
229
- disabled: disabled
230
- };
231
- var inputWrapperProps = {};
232
-
233
- if (normalizedProps.invalid) {
234
- inputWrapperProps['data-invalid'] = true;
235
- }
236
-
237
- var helperTextClasses = cx("".concat(prefix, "--form__helper-text"), _defineProperty({}, "".concat(prefix, "--form__helper-text--disabled"), normalizedProps.disabled));
238
- var helper = helperText ? /*#__PURE__*/React__default.createElement("div", {
239
- className: helperTextClasses
240
- }, helperText) : null;
241
- var labelClasses = cx("".concat(prefix, "--label"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefix, "--label--disabled"), normalizedProps.disabled), _defineProperty(_classNames3, "".concat(prefix, "--visually-hidden"), hideLabel), _classNames3));
242
- var labelText = label ? /*#__PURE__*/React__default.createElement("label", {
243
- htmlFor: id,
244
- className: labelClasses
245
- }, label) : null;
246
- var _ref = [t('increment.number'), t('decrement.number')],
247
- incrementNumLabel = _ref[0],
248
- decrementNumLabel = _ref[1];
249
- var wrapperClasses = cx("".concat(prefix, "--number__input-wrapper"), _defineProperty({}, "".concat(prefix, "--number__input-wrapper--warning"), normalizedProps.warn));
250
- var iconClasses = cx((_classNames5 = {}, _defineProperty(_classNames5, "".concat(prefix, "--number__invalid"), normalizedProps.invalid || normalizedProps.warn), _defineProperty(_classNames5, "".concat(prefix, "--number__invalid--warning"), normalizedProps.warn), _defineProperty(_classNames5, "".concat(prefix, "--number__readonly-icon"), readOnly), _classNames5));
251
- var ariaDescribedBy = null;
252
-
253
- if (normalizedProps.invalid) {
254
- ariaDescribedBy = normalizedProps.invalidId;
255
- }
256
-
257
- if (normalizedProps.warn) {
258
- ariaDescribedBy = normalizedProps.warnId;
259
- }
260
-
261
- return /*#__PURE__*/React__default.createElement("div", {
262
- className: enabled ? cx("".concat(prefix, "--form-item"), className) : "".concat(prefix, "--form-item")
263
- }, /*#__PURE__*/React__default.createElement("div", _extends({
264
- className: numberInputClasses
265
- }, inputWrapperProps), function () {
266
- return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, labelText, /*#__PURE__*/React__default.createElement("div", {
267
- className: wrapperClasses
268
- }, /*#__PURE__*/React__default.createElement("input", _extends({
269
- "data-invalid": normalizedProps.invalid,
270
- "aria-invalid": normalizedProps.invalid,
271
- "aria-describedby": ariaDescribedBy,
272
- type: "number",
273
- pattern: "[0-9]*"
274
- }, other, props, {
275
- ref: mergeRefs(ref, _this2._handleInputRef)
276
- })), normalizedProps.icon && (_normalizedProps$icon || (_normalizedProps$icon = /*#__PURE__*/React__default.createElement(normalizedProps.icon, {
277
- className: iconClasses
278
- }))), !hideSteppers && /*#__PURE__*/React__default.createElement("div", {
279
- className: "".concat(prefix, "--number__controls")
280
- }, /*#__PURE__*/React__default.createElement("button", _extends({
281
- type: "button",
282
- className: "".concat(prefix, "--number__control-btn down-icon")
283
- }, buttonProps, {
284
- onClick: function onClick(evt) {
285
- return _this2.handleArrowClick(evt, 'down');
286
- },
287
- title: decrementNumLabel || iconDescription,
288
- "aria-label": decrementNumLabel || iconDescription,
289
- tabIndex: "-1"
290
- }), _Subtract || (_Subtract = /*#__PURE__*/React__default.createElement(Subtract, {
291
- className: "down-icon"
292
- }))), /*#__PURE__*/React__default.createElement("div", {
293
- className: "".concat(prefix, "--number__rule-divider")
294
- }), /*#__PURE__*/React__default.createElement("button", _extends({
295
- type: "button",
296
- className: "".concat(prefix, "--number__control-btn up-icon")
297
- }, buttonProps, {
298
- onClick: function onClick(evt) {
299
- return _this2.handleArrowClick(evt, 'up');
300
- },
301
- title: incrementNumLabel || iconDescription,
302
- "aria-label": incrementNumLabel || iconDescription,
303
- tabIndex: "-1"
304
- }), _Add || (_Add = /*#__PURE__*/React__default.createElement(Add, {
305
- className: "up-icon"
306
- }))), /*#__PURE__*/React__default.createElement("div", {
307
- className: "".concat(prefix, "--number__rule-divider")
308
- }))), normalizedProps.validation ? null : helper);
309
- }(), normalizedProps.validation));
310
- });
311
- }
312
- }], [{
313
- key: "getDerivedStateFromProps",
314
- value: function getDerivedStateFromProps(_ref2, state) {
315
- var value = _ref2.value;
316
- var prevValue = state.prevValue;
317
-
318
- if (useControlledStateWithValue && value === '' && prevValue !== '') {
319
- return {
320
- value: '',
321
- prevValue: ''
322
- };
323
- } // If `useControlledStateWithValue` feature flag is on, do nothing here.
324
- // Otherwise, do prop -> state sync with "value capping".
325
- //// Value capping removed in #8965
326
- //// value: capMax(max, capMin(min, value)), (L223)
327
-
328
-
329
- return useControlledStateWithValue || prevValue === value ? null : {
330
- value: value,
331
- prevValue: value
199
+ if (_onClick) {
200
+ _onClick(event, state);
201
+ }
202
+ },
203
+ tabIndex: "-1",
204
+ title: decrementNumLabel || iconDescription,
205
+ type: "button"
206
+ }, _Subtract || (_Subtract = /*#__PURE__*/React__default.createElement(Subtract, {
207
+ className: "down-icon"
208
+ }))), /*#__PURE__*/React__default.createElement("div", {
209
+ className: "".concat(prefix, "--number__rule-divider")
210
+ }), /*#__PURE__*/React__default.createElement("button", {
211
+ "aria-label": incrementNumLabel || iconDescription,
212
+ className: "".concat(prefix, "--number__control-btn up-icon"),
213
+ disabled: disabled,
214
+ onClick: function onClick(event) {
215
+ var state = {
216
+ value: clamp(max, min, value + step),
217
+ direction: 'up'
332
218
  };
333
- }
334
- /**
335
- * The DOM node reference to the `<input>`.
336
- * @type {HTMLInputElement}
337
- */
338
-
339
- }]);
219
+ setValue(state.value);
340
220
 
341
- return NumberInput;
342
- }(Component);
221
+ if (onChange) {
222
+ onChange(event, state);
223
+ }
343
224
 
344
- _defineProperty(NumberInput, "propTypes", {
225
+ if (_onClick) {
226
+ _onClick(event, state);
227
+ }
228
+ },
229
+ tabIndex: "-1",
230
+ title: incrementNumLabel || iconDescription,
231
+ type: "button"
232
+ }, _Add || (_Add = /*#__PURE__*/React__default.createElement(Add, {
233
+ className: "up-icon"
234
+ }))), /*#__PURE__*/React__default.createElement("div", {
235
+ className: "".concat(prefix, "--number__rule-divider")
236
+ }))), normalizedProps.validation ? normalizedProps.validation : /*#__PURE__*/React__default.createElement(HelperText, {
237
+ disabled: disabled,
238
+ description: helperText
239
+ })));
240
+ });
241
+ NumberInput.propTypes = {
345
242
  /**
346
243
  * `true` to allow empty string.
347
244
  */
348
245
  allowEmpty: PropTypes.bool,
349
246
 
350
- /**
351
- * Provide a description that would be used to best describe the use case of the NumberInput component
352
- */
353
- ariaLabel: PropTypes.string,
354
-
355
247
  /**
356
248
  * Specify an optional className to be applied to the wrapper node
357
249
  */
@@ -385,7 +277,7 @@ _defineProperty(NumberInput, "propTypes", {
385
277
  /**
386
278
  * Provide a description for up/down icons that can be read by screen readers
387
279
  */
388
- iconDescription: PropTypes.string.isRequired,
280
+ iconDescription: PropTypes.string,
389
281
 
390
282
  /**
391
283
  * Specify a custom `id` for the input
@@ -402,11 +294,6 @@ _defineProperty(NumberInput, "propTypes", {
402
294
  */
403
295
  invalidText: PropTypes.node,
404
296
 
405
- /**
406
- * `true` to use the mobile variant.
407
- */
408
- isMobile: deprecate(PropTypes.bool, "The `isMobile` prop no longer needed as the default NumberInput styles are now identical to the mobile variant styles. This prop will be removed in the next major version of `carbon-components-react`"),
409
-
410
297
  /**
411
298
  * Generic `label` that will be used as the textual representation of what
412
299
  * this field is for
@@ -416,7 +303,7 @@ _defineProperty(NumberInput, "propTypes", {
416
303
  /**
417
304
  * `true` to use the light version.
418
305
  */
419
- light: PropTypes.bool,
306
+ light: deprecate(PropTypes.bool, 'The `light` prop for `NumberInput` is no longer needed and has ' + 'been deprecated in v11 in favor of the new `Layer` component. It will be moved in the next major release.'),
420
307
 
421
308
  /**
422
309
  * The maximum value.
@@ -432,29 +319,28 @@ _defineProperty(NumberInput, "propTypes", {
432
319
  * The new value is available in 'imaginaryTarget.value'
433
320
  * i.e. to get the value: evt.imaginaryTarget.value
434
321
  *
435
- * * _With_ `useControlledStateWithValue` feature flag, the signature of the event handler will be altered to provide additional context in the second parameter: `onChange(event, { value, direction })` where:
436
- * * `event` is the (React) raw event
437
- * * `value` is the new value
438
- * * `direction` tells you the button you hit is up button or down button
439
- * * _Without_ this feature flag the event handler has `onChange(event, direction)` signature.
440
322
  */
441
- onChange: !useControlledStateWithValue ? PropTypes.func : requiredIfValueExists(PropTypes.func),
323
+ onChange: PropTypes.func,
442
324
 
443
325
  /**
444
326
  * Provide an optional function to be called when the up/down button is clicked
445
327
  */
446
328
  onClick: PropTypes.func,
447
329
 
330
+ /**
331
+ * Provide an optional function to be called when a key is pressed in the number input
332
+ */
333
+ onKeyUp: PropTypes.func,
334
+
448
335
  /**
449
336
  * Specify if the component should be read-only
450
337
  */
451
338
  readOnly: PropTypes.bool,
452
339
 
453
340
  /**
454
- * Specify the size of the Number Input. Currently supports either `sm`, 'md' (default) or 'lg` as an option.
455
- * TODO V11: remove `xl` (replaced with lg)
341
+ * Specify the size of the Number Input.
456
342
  */
457
- size: PropTypes.oneOf(['sm', 'md', 'lg', 'xl']),
343
+ size: PropTypes.oneOf(['sm', 'md', 'lg']),
458
344
 
459
345
  /**
460
346
  * Specify how much the values should increase/decrease upon clicking on up/down button
@@ -464,7 +350,7 @@ _defineProperty(NumberInput, "propTypes", {
464
350
  /**
465
351
  * Provide custom text for the component for each translation id
466
352
  */
467
- translateWithId: PropTypes.func.isRequired,
353
+ translateWithId: PropTypes.func,
468
354
 
469
355
  /**
470
356
  * Specify the value of the input
@@ -480,36 +366,99 @@ _defineProperty(NumberInput, "propTypes", {
480
366
  * Provide the text that is displayed when the control is in warning state
481
367
  */
482
368
  warnText: PropTypes.node
483
- });
369
+ };
484
370
 
485
- _defineProperty(NumberInput, "defaultProps", {
486
- disabled: false,
487
- hideLabel: false,
488
- iconDescription: FeatureFlags.enabled('enable-v11-release') ? undefined : 'choose a number',
489
- step: 1,
490
- invalid: false,
491
- invalidText: FeatureFlags.enabled('enable-v11-release') ? undefined : 'Provide invalidText',
492
- warn: false,
493
- warnText: '',
494
- ariaLabel: 'Numeric input field with increment and decrement buttons',
495
- helperText: '',
496
- light: false,
497
- allowEmpty: false,
498
- translateWithId: function translateWithId(id) {
499
- return defaultTranslations[id];
371
+ function Label(_ref2) {
372
+ var _cx5;
373
+
374
+ var disabled = _ref2.disabled,
375
+ id = _ref2.id,
376
+ hideLabel = _ref2.hideLabel,
377
+ label = _ref2.label;
378
+ var prefix = usePrefix();
379
+ var className = cx((_cx5 = {}, _defineProperty(_cx5, "".concat(prefix, "--label"), true), _defineProperty(_cx5, "".concat(prefix, "--label--disabled"), disabled), _defineProperty(_cx5, "".concat(prefix, "--visually-hidden"), hideLabel), _cx5));
380
+
381
+ if (label) {
382
+ return /*#__PURE__*/React__default.createElement("label", {
383
+ htmlFor: id,
384
+ className: className
385
+ }, label);
500
386
  }
501
- });
502
387
 
503
- _defineProperty(NumberInput, "contextType", FeatureFlagContext);
504
- var NumberInput$1 = (function () {
505
- var forwardRef = function forwardRef(props, ref) {
506
- return /*#__PURE__*/React__default.createElement(NumberInput, _extends({}, props, {
507
- innerRef: ref
508
- }));
509
- };
388
+ return null;
389
+ }
390
+
391
+ Label.propTypes = {
392
+ disabled: PropTypes.bool,
393
+ hideLabel: PropTypes.bool,
394
+ id: PropTypes.string,
395
+ label: PropTypes.node
396
+ };
397
+
398
+ function HelperText(_ref3) {
399
+ var disabled = _ref3.disabled,
400
+ description = _ref3.description;
401
+ var prefix = usePrefix();
402
+ var className = cx("".concat(prefix, "--form__helper-text"), _defineProperty({}, "".concat(prefix, "--form__helper-text--disabled"), disabled));
403
+
404
+ if (description) {
405
+ return /*#__PURE__*/React__default.createElement("div", {
406
+ className: className
407
+ }, description);
408
+ }
409
+
410
+ return null;
411
+ }
412
+
413
+ HelperText.propTypes = {
414
+ description: PropTypes.node,
415
+ disabled: PropTypes.bool
416
+ };
417
+ /**
418
+ * Determine if the given value is invalid based on the given max, min and
419
+ * conditions like `allowEmpty`. If `invalid` is passed through, it will default
420
+ * to false.
421
+ *
422
+ * @param {object} config
423
+ * @param {boolean} config.allowEmpty
424
+ * @param {boolean} config.invalid
425
+ * @param {number} config.value
426
+ * @param {number} config.max
427
+ * @param {number} config.min
428
+ * @returns {boolean}
429
+ */
430
+
431
+ function getInputValidity(_ref4) {
432
+ var allowEmpty = _ref4.allowEmpty,
433
+ invalid = _ref4.invalid,
434
+ value = _ref4.value,
435
+ max = _ref4.max,
436
+ min = _ref4.min;
437
+
438
+ if (invalid) {
439
+ return false;
440
+ }
441
+
442
+ if (value === '') {
443
+ return allowEmpty;
444
+ }
445
+
446
+ if (value > max || value < min) {
447
+ return false;
448
+ }
449
+
450
+ return true;
451
+ }
452
+ /**
453
+ * Clamp the given value between the upper bound `max` and the lower bound `min`
454
+ * @param {number} max
455
+ * @param {number} min
456
+ * @param {number} value
457
+ */
458
+
510
459
 
511
- forwardRef.displayName = 'NumberInput';
512
- return /*#__PURE__*/React__default.forwardRef(forwardRef);
513
- })();
460
+ function clamp(max, min, value) {
461
+ return Math.min(max, Math.max(min, value));
462
+ }
514
463
 
515
- export { NumberInput, NumberInput$1 as default, translationIds };
464
+ export { NumberInput, translationIds };