UNPKG

wix-style-react

Version:
125 lines (102 loc) 4.54 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import Input from '../Input'; import { st, classes } from './ColorPickerConverter.st.css'; import ColorPickerConverterViewer from './ColorPickerConverterViewer'; import { safeColor, getHexOrEmpty } from './utils'; var ColorPickerConverterHex = /*#__PURE__*/function (_React$PureComponent) { _inherits(ColorPickerConverterHex, _React$PureComponent); var _super = _createSuper(ColorPickerConverterHex); function ColorPickerConverterHex() { var _this; _classCallCheck(this, ColorPickerConverterHex); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { hex: getHexOrEmpty(_this.props.current), inFocus: false }); _defineProperty(_assertThisInitialized(_this), "change", function (_ref) { var value = _ref.target.value; _this.setState({ hex: value }, function () { var _color = safeColor(value, _this.props.allowEmpty); if (_color) { _this.props.onChange(_color); } }); }); _defineProperty(_assertThisInitialized(_this), "handleOnFocus", function () { _this.setState({ inFocus: true }); }); _defineProperty(_assertThisInitialized(_this), "handleOnBlur", function () { _this.setState({ inFocus: false, hex: getHexOrEmpty(_this.props.current) }); }); _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (event) { var key = event.key; if (key === 'Enter') { _this.props.onEnter(); } }); return _this; } _createClass(ColorPickerConverterHex, [{ key: "render", value: function render() { var dataHook = this.props.dataHook; return /*#__PURE__*/React.createElement("div", { className: st(classes.root), "data-hook": dataHook }, /*#__PURE__*/React.createElement(Input, { size: "small", value: this.state.hex, placeholder: this.props.placeholder, onChange: this.change, onFocus: this.handleOnFocus, onBlur: this.handleOnBlur, onKeyDown: this.handleKeyDown, className: classes.colorInput, onInputClicked: function onInputClicked(e) { return e.stopPropagation(); } }), /*#__PURE__*/React.createElement(ColorPickerConverterViewer, _extends({}, this.props, { color: this.props.current }))); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(props) { var hex = getHexOrEmpty(props.current); if (!this.state.inFocus && this.state.hex !== hex) { this.setState({ hex: hex }); } } }]); return ColorPickerConverterHex; }(React.PureComponent); _defineProperty(ColorPickerConverterHex, "propTypes", { current: PropTypes.object.isRequired, onChange: PropTypes.func.isRequired, placeholder: PropTypes.string }); export { ColorPickerConverterHex as default };