UNPKG

wix-style-react

Version:
180 lines (158 loc) 7.74 kB
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 _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; } 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, { Children } from 'react'; import { any } from 'prop-types'; import WixComponent from '../../BaseComponents/WixComponent'; import styles from './FieldWithSelectionComposite.scss'; import classNames from 'classnames'; import FieldLabelAttributes from '../../FieldLabelAttributes/FieldLabelAttributes'; var FieldWithSelectionComposite = function (_WixComponent) { _inherits(FieldWithSelectionComposite, _WixComponent); function FieldWithSelectionComposite(props) { _classCallCheck(this, FieldWithSelectionComposite); var _this = _possibleConstructorReturn(this, (FieldWithSelectionComposite.__proto__ || Object.getPrototypeOf(FieldWithSelectionComposite)).call(this, props)); _this._onFocusFirst = _this._onFocusFirst.bind(_this); _this._onBlurFirst = _this._onBlurFirst.bind(_this); _this._onFocusLast = _this._onFocusLast.bind(_this); _this._onBlurLast = _this._onBlurLast.bind(_this); _this.state = { hasFocusFirst: false, hasFocusLast: false }; return _this; } _createClass(FieldWithSelectionComposite, [{ key: 'getPrototypeDisplayName', value: function getPrototypeDisplayName(selectionInput) { var type = Object.getPrototypeOf(selectionInput.type); return type && type.displayName; } }, { key: 'getDisplayName', value: function getDisplayName(selectionInput) { return selectionInput.type.displayName || this.getPrototypeDisplayName(selectionInput); } }, { key: '_getTextInput', value: function _getTextInput() { return this.props.children.length === 3 ? this.props.children[1] : this.props.children[0]; } }, { key: '_onFocusFirst', value: function _onFocusFirst() { this.setState({ hasFocusFirst: true }); } }, { key: '_onBlurFirst', value: function _onBlurFirst(e) { var textInput = this._getTextInput(); this.setState({ hasFocusFirst: false }, function () { textInput.props.onBlur && textInput.props.onBlur(e); }); } }, { key: '_onFocusLast', value: function _onFocusLast() { this.setState({ hasFocusLast: true }); } }, { key: '_onBlurLast', value: function _onBlurLast() { this.setState({ hasFocusLast: false }); } }, { key: 'withBorderWrapper', value: function withBorderWrapper(checkboxSelectionInput) { var checkboxWrapperClassNames = _defineProperty({}, styles.borderWrapper, true); checkboxWrapperClassNames[styles.error] = this.props.error; checkboxWrapperClassNames[styles.disabled] = this.props.disabled; return React.createElement( 'div', { className: classNames(checkboxWrapperClassNames) }, checkboxSelectionInput ); } }, { key: 'cloneSelectionInput', value: function cloneSelectionInput(selectionInput, selectionInputType) { var _React$cloneElement; var isCheckbox = selectionInputType === 'Checkbox'; var errorPropName = isCheckbox ? 'hasError' : 'error'; var clonedSelectionInput = React.cloneElement(selectionInput, (_React$cloneElement = { noLeftBorderRadius: true, disabled: this.props.disabled }, _defineProperty(_React$cloneElement, errorPropName, this.props.error), _defineProperty(_React$cloneElement, 'onFocus', this._onFocusLast), _defineProperty(_React$cloneElement, 'onBlur', this._onBlurLast), _React$cloneElement)); return isCheckbox ? this.withBorderWrapper(clonedSelectionInput) : clonedSelectionInput; } }, { key: 'getSelectionInputType', value: function getSelectionInputType(selectionInputChild) { var type = this.getDisplayName(selectionInputChild); // HACK to handle withFocusable(Checkbox) if (type.includes('Checkbox')) { type = 'Checkbox'; } return type; } }, { key: 'render', value: function render() { var children = Children.toArray(this.props.children); var label = children.length === 3 ? React.createElement( 'div', { className: styles.label }, children[0], this.props.required || this.props.info || this.props.tooltip ? React.createElement(FieldLabelAttributes, { required: this.props.required, info: this.props.info, tooltip: this.props.tooltip, appendToParent: this.props.appendToParent }) : null ) : null; var textInput = this._getTextInput(); var originalSelectionInput = label ? children[2] : children[1]; var inputsWrapperClassNames = _defineProperty({}, styles.inputs, true); var selectionInputType = this.getSelectionInputType(originalSelectionInput); if (selectionInputType) { inputsWrapperClassNames[styles[selectionInputType.toLowerCase()]] = true; } inputsWrapperClassNames[styles.hasFocusFirst] = this.state.hasFocusFirst; inputsWrapperClassNames[styles.hasFocusLast] = this.state.hasFocusLast; inputsWrapperClassNames[styles.error] = this.props.error; inputsWrapperClassNames[styles.disabled] = this.props.disabled; return React.createElement( 'div', { className: styles.wrapper }, label, React.createElement( 'div', { className: classNames(inputsWrapperClassNames), 'data-hook': 'input-wrappers' }, React.cloneElement(textInput, { noRightBorderRadius: true, onFocus: this._onFocusFirst, onBlur: this._onBlurFirst, error: this.props.error, disabled: this.props.disabled, withSelection: true }), this.cloneSelectionInput(originalSelectionInput, selectionInputType) ) ); } }]); return FieldWithSelectionComposite; }(WixComponent); FieldWithSelectionComposite.propTypes = { children: any }; FieldWithSelectionComposite.defaultProps = { appendToParent: false }; FieldWithSelectionComposite.displayName = 'FieldWithSelectionComposite'; export default FieldWithSelectionComposite;