UNPKG

@multiplay/ui

Version:

UI kit for Unity and Multiplay applications

318 lines (272 loc) 9.84 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _icons = require('../../atoms/icons'); var _icons2 = _interopRequireDefault(_icons); var _styles = require('./_styles'); 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 _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; } // COMPONENTS // STYLES var InputField = function (_React$Component) { _inherits(InputField, _React$Component); function InputField(props) { _classCallCheck(this, InputField); var _this = _possibleConstructorReturn(this, (InputField.__proto__ || Object.getPrototypeOf(InputField)).call(this, props)); _this.handleChange = function (e) { var onChange = _this.props.onChange; onChange({ e: e, value: e.target.value }); }; _this.handleBlur = function (e) { var onBlur = _this.props.onBlur; _this.setState({ focussed: false, touched: true }); onBlur({ e: e, value: e.target.value }); }; _this.handleFocus = function () { _this.setState({ focussed: true }); }; _this.renderRequired = function () { var required = _this.props.required; if (!required) return null; return _react2.default.createElement( 'sup', { 'data-testid': 'required' }, '*' ); }; _this.renderLabel = function () { var _this$props = _this.props, label = _this$props.label, name = _this$props.name, skin = _this$props.skin, value = _this$props.value, error = _this$props.error, disabled = _this$props.disabled, outlined = _this$props.outlined, icon = _this$props.icon; var _this$state = _this.state, focussed = _this$state.focussed, touched = _this$state.touched; if (label) { return _react2.default.createElement( _styles.Label, { 'data-testid': 'label', htmlFor: name, skin: skin, active: focussed, error: error && touched, disabled: disabled, outlined: outlined && skin !== 'dark', hasValue: value || value === 0, hasIcon: !!icon }, label, ' ', _this.renderRequired() ); } return null; }; _this.renderHelperText = function () { var _this$props2 = _this.props, helperText = _this$props2.helperText, error = _this$props2.error, skin = _this$props2.skin, disabled = _this$props2.disabled, outlined = _this$props2.outlined; var touched = _this.state.touched; if (helperText && (!error || !touched || disabled)) { return _react2.default.createElement( _styles.Helper, { 'data-testid': 'helper-text', skin: skin, disabled: disabled, outlined: outlined && skin !== 'dark' }, helperText ); } return null; }; _this.renderErrorText = function () { var _this$props3 = _this.props, errorMessage = _this$props3.errorMessage, error = _this$props3.error, disabled = _this$props3.disabled, outlined = _this$props3.outlined, skin = _this$props3.skin; var touched = _this.state.touched; if (error && touched && !disabled) { return _react2.default.createElement( _styles.ErrorMessage, { 'data-testid': 'error-text', outlined: outlined && skin !== 'dark' }, errorMessage ); } return null; }; _this.renderIcon = function () { var _this$props4 = _this.props, skin = _this$props4.skin, icon = _this$props4.icon, outlined = _this$props4.outlined, disabled = _this$props4.disabled; if (icon) { return _react2.default.createElement( _styles.FieldIcon, { disabled: disabled, outlined: outlined, skin: skin, 'data-testid': 'icon' }, _react2.default.createElement(_icons2.default, { icon: icon, margin: '0 5px 0 0' }) ); } return null; }; _this.renderInput = function () { var _this$props5 = _this.props, name = _this$props5.name, value = _this$props5.value, type = _this$props5.type, skin = _this$props5.skin, disabled = _this$props5.disabled, required = _this$props5.required, error = _this$props5.error, outlined = _this$props5.outlined, icon = _this$props5.icon; return _react2.default.createElement( _react2.default.Fragment, null, _this.renderIcon(), _this.renderLabel(), _react2.default.createElement(_styles.Input, { 'data-testid': 'input', name: name, id: name, value: value, onChange: _this.handleChange, onFocus: _this.handleFocus, onBlur: _this.handleBlur, type: type, skin: skin, 'aria-required': required.toString(), 'aria-invalid': error.toString(), disabled: disabled, outlined: outlined && skin !== 'dark', hasIcon: !!icon }) ); }; _this.renderField = function () { var _this$props6 = _this.props, outlined = _this$props6.outlined, skin = _this$props6.skin, error = _this$props6.error, disabled = _this$props6.disabled; var _this$state2 = _this.state, touched = _this$state2.touched, focussed = _this$state2.focussed; if (!outlined || skin === 'dark') { return _react2.default.createElement( _styles.InputWrapper, { skin: skin, error: error && touched, active: focussed && !disabled, disabled: disabled, 'data-testid': 'input-wrapper' }, _this.renderInput() ); } return _react2.default.createElement( _styles.InputWrapperOutlined, { skin: skin, error: error && touched, active: focussed && !disabled, disabled: disabled, 'data-testid': 'input-wrapper-outlined' }, _this.renderInput() ); }; _this.state = { focussed: false, touched: props.forceTouched }; return _this; } _createClass(InputField, [{ key: 'render', value: function render() { var outlined = this.props.outlined; return _react2.default.createElement( _styles.Wrapper, { 'data-testid': 'field-wrapper', outlined: outlined }, this.renderField(), this.renderHelperText(), this.renderErrorText() ); } }]); return InputField; }(_react2.default.Component); InputField.defaultProps = { value: '', skin: 'light', label: '', helperText: '', forceTouched: false, required: false, error: false, disabled: false, outlined: false, errorMessage: 'Field invalid', icon: '', onChange: function onChange() { return null; }, onBlur: function onBlur() { return null; } }; InputField.propTypes = { name: _propTypes2.default.string.isRequired, onChange: _propTypes2.default.func, onBlur: _propTypes2.default.func, label: _propTypes2.default.string, helperText: _propTypes2.default.string, forceTouched: _propTypes2.default.bool, required: _propTypes2.default.bool, error: _propTypes2.default.bool, disabled: _propTypes2.default.bool, outlined: _propTypes2.default.bool, errorMessage: _propTypes2.default.string, icon: _propTypes2.default.string, type: _propTypes2.default.oneOf(['date', 'email', 'number', 'text', 'tel', 'time', 'url', 'password', 'hidden']).isRequired, skin: _propTypes2.default.oneOf(['light', 'dark']), value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]) }; exports.default = InputField;