UNPKG

wix-style-react

Version:
135 lines (102 loc) 4.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; 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; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _color2 = require('color'); var _color3 = _interopRequireDefault(_color2); var _propTypes = require('prop-types'); var _WixComponent2 = require('../BaseComponents/WixComponent'); var _WixComponent3 = _interopRequireDefault(_WixComponent2); var _Input = require('../Input'); var _Input2 = _interopRequireDefault(_Input); var _ColorPickerConverter = require('./ColorPickerConverter.scss'); var _ColorPickerConverter2 = _interopRequireDefault(_ColorPickerConverter); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 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 _react2.default.createElement( 'div', { className: _ColorPickerConverter2.default.root }, _react2.default.createElement(_Input2.default, { 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; }(_WixComponent3.default), _class.propTypes = { current: _propTypes.object.isRequired, onChange: _propTypes.func.isRequired }, _temp); exports.default = ColorPickerConverterHex; function safeColor(input) { try { return (0, _color3.default)(input); } catch (error) { return null; } }