wix-style-react
Version:
wix-style-react
96 lines (77 loc) • 4.07 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import FormField from 'wix-style-react/FormField';
import Input from 'wix-style-react/Input';
var min = -5;
var max = 5;
/**
*This example does NOT cover all edge-cases, does not support:
* - step validation
* - snap-to-stap
* - correct error messages
*/
var ExampleNumberInput = function (_React$Component) {
_inherits(ExampleNumberInput, _React$Component);
function ExampleNumberInput(props) {
_classCallCheck(this, ExampleNumberInput);
var _this = _possibleConstructorReturn(this, (ExampleNumberInput.__proto__ || Object.getPrototypeOf(ExampleNumberInput)).call(this, props));
_this.handleChange = function (event) {
_this.setState({ value: event.target.value });
};
_this.handleTickerUp = function () {
_this.applyStep(1);
};
_this.handleTickerDown = function () {
_this.applyStep(-1);
};
_this.state = { value: '0' };
return _this;
}
_createClass(ExampleNumberInput, [{
key: 'applyStep',
value: function applyStep(step) {
var currentValue = Number.parseInt(this.state.value);
if (Number.isNaN(currentValue)) {
return;
} else {
this.setState({
value: Math.min(Math.max(currentValue + step, min), max)
});
}
}
}, {
key: 'render',
value: function render() {
var value = this.state.value;
var numberValue = Number.parseInt(value);
var isValidNumber = !Number.isNaN(numberValue) && !(numberValue < min || numberValue > max);
var errorProps = isValidNumber || value === '' || value === '-' ? {} : {
status: 'error',
statusMessage: 'Number must be between ' + min + ' and ' + max
};
return React.createElement(
FormField,
{ label: 'This is the FormField label' },
React.createElement(Input, _extends({
type: 'number',
value: this.state.value,
onChange: this.handleChange,
placeholder: 'Enter an integer number',
min: min,
max: max
}, errorProps, {
suffix: React.createElement(Input.Ticker, {
onDown: this.handleTickerDown,
onUp: this.handleTickerUp
})
}))
);
}
}]);
return ExampleNumberInput;
}(React.Component);
export { ExampleNumberInput as default };