wix-style-react
Version:
wix-style-react
203 lines (168 loc) • 8.78 kB
JavaScript
'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 _WixComponent2 = require('../../BaseComponents/WixComponent');
var _WixComponent3 = _interopRequireDefault(_WixComponent2);
var _FieldWithSelectionComposite = require('./FieldWithSelectionComposite.scss');
var _FieldWithSelectionComposite2 = _interopRequireDefault(_FieldWithSelectionComposite);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _FieldLabelAttributes = require('../../FieldLabelAttributes/FieldLabelAttributes');
var _FieldLabelAttributes2 = _interopRequireDefault(_FieldLabelAttributes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
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({}, _FieldWithSelectionComposite2.default.borderWrapper, true);
checkboxWrapperClassNames[_FieldWithSelectionComposite2.default.error] = this.props.error;
checkboxWrapperClassNames[_FieldWithSelectionComposite2.default.disabled] = this.props.disabled;
return _react2.default.createElement(
'div',
{ className: (0, _classnames2.default)(checkboxWrapperClassNames) },
checkboxSelectionInput
);
}
}, {
key: 'cloneSelectionInput',
value: function cloneSelectionInput(selectionInput, selectionInputType) {
var _React$cloneElement;
var isCheckbox = selectionInputType === 'Checkbox';
var errorPropName = isCheckbox ? 'hasError' : 'error';
var clonedSelectionInput = _react2.default.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 = _react.Children.toArray(this.props.children);
var label = children.length === 3 ? _react2.default.createElement(
'div',
{ className: _FieldWithSelectionComposite2.default.label },
children[0],
this.props.required || this.props.info || this.props.tooltip ? _react2.default.createElement(_FieldLabelAttributes2.default, {
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({}, _FieldWithSelectionComposite2.default.inputs, true);
var selectionInputType = this.getSelectionInputType(originalSelectionInput);
if (selectionInputType) {
inputsWrapperClassNames[_FieldWithSelectionComposite2.default[selectionInputType.toLowerCase()]] = true;
}
inputsWrapperClassNames[_FieldWithSelectionComposite2.default.hasFocusFirst] = this.state.hasFocusFirst;
inputsWrapperClassNames[_FieldWithSelectionComposite2.default.hasFocusLast] = this.state.hasFocusLast;
inputsWrapperClassNames[_FieldWithSelectionComposite2.default.error] = this.props.error;
inputsWrapperClassNames[_FieldWithSelectionComposite2.default.disabled] = this.props.disabled;
return _react2.default.createElement(
'div',
{ className: _FieldWithSelectionComposite2.default.wrapper },
label,
_react2.default.createElement(
'div',
{
className: (0, _classnames2.default)(inputsWrapperClassNames),
'data-hook': 'input-wrappers'
},
_react2.default.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;
}(_WixComponent3.default);
FieldWithSelectionComposite.propTypes = {
children: _propTypes.any
};
FieldWithSelectionComposite.defaultProps = {
appendToParent: false
};
FieldWithSelectionComposite.displayName = 'FieldWithSelectionComposite';
exports.default = FieldWithSelectionComposite;