UNPKG

wix-style-react

Version:
113 lines (91 loc) 3.95 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; }; }(); var _class, _temp; 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 from 'react'; import color from 'color'; import { object, func } from 'prop-types'; import WixComponent from '../BaseComponents/WixComponent'; import Input from '../Input'; import css from './ColorPickerConverter.scss'; var ColorPickerConverterHex = (_temp = _class = function (_WixComponent) { _inherits(ColorPickerConverterHex, _WixComponent); function ColorPickerConverterHex(props) { _classCallCheck(this, ColorPickerConverterHex); var _this = _possibleConstructorReturn(this, (ColorPickerConverterHex.__proto__ || Object.getPrototypeOf(ColorPickerConverterHex)).call(this, props)); _this.handleOnFocus = function () { _this.setState({ inFocus: true }); }; _this.handleOnBlur = function () { _this.setState({ inFocus: false, hex: _this.props.current.hex() }); }; _this.handleKeyDown = function (event) { var key = event.key; if (key === 'Enter') { _this.props.onEnter(); } }; _this.change = _this.change.bind(_this); _this.state = { hex: props.current.hex(), inFocus: false }; return _this; } _createClass(ColorPickerConverterHex, [{ key: 'render', value: function render() { return React.createElement( 'div', { className: css.root }, React.createElement(Input, { size: 'small', value: this.state.hex, onChange: this.change, onFocus: this.handleOnFocus, onBlur: this.handleOnBlur, onKeyDown: this.handleKeyDown }) ); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(props) { if (!this.state.inFocus && this.state.hex !== props.current.hex()) { this.setState({ hex: props.current.hex() }); } } }, { key: 'change', value: function change(_ref) { var _this2 = this; var value = _ref.target.value; this.setState({ hex: value }, function () { var _color = safeColor(value); if (_color) { _this2.props.onChange(_color); } }); } }]); return ColorPickerConverterHex; }(WixComponent), _class.propTypes = { current: object.isRequired, onChange: func.isRequired }, _temp); export { ColorPickerConverterHex as default }; function safeColor(input) { try { return color(input); } catch (error) { return null; } }