UNPKG

wix-style-react

Version:
232 lines (193 loc) • 8.61 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["suffix", "defaultValue", "strict", "max", "min", "hideStepper"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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); }; } 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; } } import React from 'react'; import PropTypes from 'prop-types'; import Input from '../Input'; var NumberInput = /*#__PURE__*/function (_React$PureComponent) { _inherits(NumberInput, _React$PureComponent); var _super = _createSuper(NumberInput); function NumberInput(props) { var _this; _classCallCheck(this, NumberInput); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "_increment", function () { _this._applyChange(function (value, step) { return value + step; }); }); _defineProperty(_assertThisInitialized(_this), "_decrement", function () { _this._applyChange(function (value, step) { return value - step; }); }); _defineProperty(_assertThisInitialized(_this), "_inputValueChanged", function (e) { var strict = _this.props.strict; var value = _this._normalizeValue(e.target.value); if (strict) { return _this._triggerOnChange(_this._applyStrictValue(value)); } return _this._triggerOnChange(value); }); _defineProperty(_assertThisInitialized(_this), "_normalizeValue", function (value) { if (typeof value !== 'string' || _this._isInvalidNumber(value)) { return value; } // Preserve minus sign when typing '-00'. return value.startsWith('-') ? value.replace(/^-0+(?!\.|$)/, '-') : value.replace(/^0+(?!\.|$)/, ''); }); _defineProperty(_assertThisInitialized(_this), "_getInputRef", function (ref) { var inputRef = _this.props.inputRef; _this.inputDOM = ref; if (inputRef) { inputRef(ref); } }); var _value = props.value; _this.state = { value: _this._defaultValueNullIfEmpty(_value) }; return _this; } _createClass(NumberInput, [{ key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(_ref) { var value = _ref.value; this.setState({ value: this._defaultValueNullIfEmpty(value) }); } }, { key: "_isInvalidNumber", value: function _isInvalidNumber(value) { return ['.', '-', undefined, ''].includes(value); } }, { key: "_defaultValueNullIfEmpty", value: function _defaultValueNullIfEmpty() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.defaultValue; return value == null || value === '' ? null : Number(value); } }, { key: "_defaultValueToNullIfInvalidNumber", value: function _defaultValueToNullIfInvalidNumber(value) { return this._isInvalidNumber(value) ? null : Number(value); } }, { key: "_getInputValueFromState", value: function _getInputValueFromState() { var value = this.state.value; var defaultValue = this.props.defaultValue; if (value != null) { return value; } return defaultValue || ''; } }, { key: "_isInRange", value: function _isInRange(value) { var _this$props = this.props, min = _this$props.min, max = _this$props.max; return !(!isNaN(min) && value < min) && !(!isNaN(max) && value > max); } }, { key: "_applyChange", value: function _applyChange(operator) { var _this$props2 = this.props, value = _this$props2.value, step = _this$props2.step, numberValue = parseFloat(value || this.inputDOM.value) || 0, numberStep = step, updatedValue = operator(numberValue, numberStep); if (this._isInRange(updatedValue)) { this._triggerOnChange(updatedValue); } } }, { key: "_triggerOnChange", value: function _triggerOnChange(value) { var _this2 = this; var onChange = this.props.onChange; this.setState({ value: value }, function () { return onChange && onChange(_this2._defaultValueToNullIfInvalidNumber(value)); }); } }, { key: "_applyStrictValue", value: function _applyStrictValue(value) { var _this$props3 = this.props, min = _this$props3.min, max = _this$props3.max; if (this._isInRange(value) || this._isInvalidNumber(value)) { return value; } var dMax = max - value; var dMin = min - value; if (!dMax) { return min; } if (!dMin) { return max; } return Math.abs(dMax) < Math.abs(dMin) ? max : min; } }, { key: "render", value: function render() { // <Input/> should always be controlled. Therefore, not passing defaultValue to <Input/>. var _this$props4 = this.props, suffix = _this$props4.suffix, defaultValue = _this$props4.defaultValue, strict = _this$props4.strict, max = _this$props4.max, min = _this$props4.min, hideStepper = _this$props4.hideStepper, props = _objectWithoutProperties(_this$props4, _excluded); var value = this._getInputValueFromState(); return /*#__PURE__*/React.createElement(Input, _extends({}, props, { max: max, min: min, type: "number", value: value, onChange: this._inputValueChanged, inputRef: this._getInputRef, suffix: /*#__PURE__*/React.createElement(Input.Group, null, suffix, !hideStepper && /*#__PURE__*/React.createElement(Input.Ticker, { onUp: this._increment, onDown: this._decrement, dataHook: "number-input-ticker", upDisabled: strict && max === value, downDisabled: strict && min === value })) })); } }]); return NumberInput; }(React.PureComponent); _defineProperty(NumberInput, "displayName", 'NumberInput'); NumberInput.propTypes = _objectSpread(_objectSpread({}, Input.propTypes), {}, { /** Defines the initial value of an input */ defaultValue: PropTypes.number, /** Specifies whether typed values that are beyond min/max range should be rounded to the nearest acceptable value */ strict: PropTypes.bool, /** Specifies whether stepper should be hidden */ hideStepper: PropTypes.bool }); NumberInput.defaultProps = { step: 1, strict: false, hideStepper: false }; export default NumberInput;