UNPKG

wix-style-react

Version:
152 lines (131 loc) • 6.65 kB
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 _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 * as PropTypes from 'prop-types'; import WixComponent from '../../BaseComponents/WixComponent'; import styles from './RangeInputWithLabelComposite.scss'; import classNames from 'classnames'; import FieldLabelAttributes from '../../FieldLabelAttributes/FieldLabelAttributes'; var RangeInputWithLabelComposite = function (_WixComponent) { _inherits(RangeInputWithLabelComposite, _WixComponent); function RangeInputWithLabelComposite(props) { _classCallCheck(this, RangeInputWithLabelComposite); var _this = _possibleConstructorReturn(this, (RangeInputWithLabelComposite.__proto__ || Object.getPrototypeOf(RangeInputWithLabelComposite)).call(this, props)); _this.state = { hasFocusFirst: false, hasFocusLast: false }; return _this; } _createClass(RangeInputWithLabelComposite, [{ key: '_doKeyDown', value: function _doKeyDown(e) { var keys = { upArrow: 38, downArrow: 40 }; if (e.keyCode === keys.upArrow && !isNaN(e.target.value)) { e.preventDefault(); e.target.value++; } if (e.keyCode === keys.downArrow && !isNaN(e.target.value)) { e.preventDefault(); e.target.value--; } } }, { key: '_handleFocusFirst', value: function _handleFocusFirst() { this.setState({ hasFocusFirst: true }); } }, { key: '_handleBlurFirst', value: function _handleBlurFirst() { this.setState({ hasFocusFirst: false }); } }, { key: '_handleFocusLast', value: function _handleFocusLast() { this.setState({ hasFocusLast: true }); } }, { key: '_handleBlurLast', value: function _handleBlurLast() { this.setState({ hasFocusLast: false }); } }, { key: 'render', value: function render() { var _this2 = this, _classNames; var children = Children.toArray(this.props.children); var rangeType = this.props.children[1].type.displayName; 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 firstInput = children.length === 3 ? children[1] : children[0]; var lastInput = children.length === 3 ? children[2] : children[1]; var additionalFirstInputProps = { className: rangeType === 'DatePicker' ? styles.firstDate : styles.firstinput, noRightBorderRadius: true, onKeyDown: function onKeyDown(e) { return _this2._doKeyDown(e); }, onFocus: function onFocus(e) { return _this2._handleFocusFirst(e); }, onBlur: function onBlur(e) { return _this2._handleBlurFirst(e); } }; var additionalLastInputProps = { className: rangeType === 'DatePicker' ? styles.lastDate : styles.lastinput, noLeftBorderRadius: true, onKeyDown: function onKeyDown(e) { return _this2._doKeyDown(e); }, onFocus: function onFocus(e) { return _this2._handleFocusLast(e); }, onBlur: function onBlur(e) { return _this2._handleBlurLast(e); } }; var inputWrapperClassNames = classNames((_classNames = {}, _defineProperty(_classNames, styles.hasFocusFirst, this.state.hasFocusFirst), _defineProperty(_classNames, styles.hasFocusLast, this.state.hasFocusLast), _defineProperty(_classNames, styles.inputs, true), _classNames)); return React.createElement( 'div', { className: styles.wrapper }, label, React.createElement( 'div', { className: inputWrapperClassNames }, React.cloneElement(firstInput, rangeType === 'DatePicker' ? { inputProps: additionalFirstInputProps } : additionalFirstInputProps), React.cloneElement(lastInput, rangeType === 'DatePicker' ? { inputProps: additionalLastInputProps } : additionalLastInputProps) ) ); } }]); return RangeInputWithLabelComposite; }(WixComponent); RangeInputWithLabelComposite.propTypes = _extends({}, WixComponent.propTypes, { children: PropTypes.any, required: PropTypes.bool, info: PropTypes.string }); RangeInputWithLabelComposite.defaultProps = { appendToParent: false }; RangeInputWithLabelComposite.displayName = 'RangeInputWithLabelComposite'; export default RangeInputWithLabelComposite;