semantic-ui-react-numberinput
Version:
Numeric input control with step buttons for Semantic UI React
130 lines (111 loc) • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Validators = _interopRequireDefault(require("./Validators"));
var _NumberUtils = _interopRequireDefault(require("../utils/NumberUtils"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var PropValidators =
/*#__PURE__*/
function () {
function PropValidators() {
_classCallCheck(this, PropValidators);
}
_createClass(PropValidators, null, [{
key: "validateValue",
value: function validateValue(props) {
PropValidators.validateEmptyValue(props.allowEmptyValue, props.value);
if (props.allowEmptyValue && !props.value) {
return;
}
var value = _NumberUtils.default.getParsedValue(props.value, props.valueType);
if (!_Validators.default.isValidValue(value, props.valueType)) {
throw new Error('value must be a string that can be parsed to integer/decimal number depending on valueType');
}
}
}, {
key: "validateEmptyValue",
value: function validateEmptyValue(allowEmptyValue, value) {
if (!allowEmptyValue && !value) {
throw new Error('value is required');
}
}
}, {
key: "validateDefaultValue",
value: function validateDefaultValue(props) {
if (props.defaultValue !== undefined && !_Validators.default.isValidValue(props.defaultValue, props.valueType)) {
throw new Error('defaultValue must be integer/decimal number depending on valueType');
}
}
}, {
key: "validateDoubleClickStepAmount",
value: function validateDoubleClickStepAmount(props) {
if (!_Validators.default.isValidValue(props.doubleClickStepAmount, props.valueType)) {
throw new Error('doubleClickStepAmount must be integer/decimal number depending on valueType');
}
if (props.doubleClickStepAmount !== 0 && props.doubleClickStepAmount <= props.stepAmount) {
throw new Error('doubleClickStepAmount must be greater than stepAmount');
}
}
}, {
key: "validateMinValue",
value: function validateMinValue(props) {
if (props.minValue > props.maxValue) {
throw new Error('maxValue must greater than or equal to minValue');
}
PropValidators.validateMinOrMaxValue(props.valueType, props.minValue, 'minValue', props.maxLength);
}
}, {
key: "validateMinOrMaxValue",
value: function validateMinOrMaxValue(valueType, value, valueName, maxLength) {
if (!_Validators.default.isValidValue(value, valueType)) {
throw new Error("".concat(valueName, " must be integer/decimal number depending on valueType"));
}
if (value.toString().length > maxLength) {
throw new Error("".concat(valueName, " does not fit in maxLength"));
}
}
}, {
key: "validateMaxLength",
value: function validateMaxLength(props) {
_Validators.default.validatePositiveInteger(props.maxLength, 'maxLength');
}
}, {
key: "validatePrecision",
value: function validatePrecision(props) {
_Validators.default.validatePositiveInteger(props.precision, 'precision');
}
}, {
key: "validateStepAmount",
value: function validateStepAmount(props) {
if (!_Validators.default.isValidValue(props.stepAmount, props.valueType) || props.stepAmount <= 0) {
throw new Error('stepAmount must be a positive integer/decimal number depending on valueType');
}
}
}, {
key: "validatePropsInDevelopmentMode",
value: function validatePropsInDevelopmentMode(props) {
if (process.env.NODE_ENV === 'development') {
PropValidators.validateValue(props);
PropValidators.validateDefaultValue(props);
PropValidators.validateDoubleClickStepAmount(props);
PropValidators.validateMinValue(props);
PropValidators.validateMaxValue(props);
PropValidators.validateMaxLength(props);
PropValidators.validatePrecision(props);
PropValidators.validateStepAmount(props);
}
}
}]);
return PropValidators;
}();
exports.default = PropValidators;
_defineProperty(PropValidators, "validateMaxValue", function (props) {
return PropValidators.validateMinOrMaxValue(props.valueType, props.maxValue, 'maxValue', props.maxLength);
});
//# sourceMappingURL=PropValidators.js.map