UNPKG

wix-style-react

Version:
196 lines (150 loc) • 7.22 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 _ColorPickerHsb = require('./ColorPickerHsb'); var _ColorPickerHsb2 = _interopRequireDefault(_ColorPickerHsb); var _ColorPickerHue = require('./ColorPickerHue'); var _ColorPickerHue2 = _interopRequireDefault(_ColorPickerHue); var _ColorPickerHistory = require('./ColorPickerHistory'); var _ColorPickerHistory2 = _interopRequireDefault(_ColorPickerHistory); var _ColorPickerConverter = require('./ColorPickerConverter'); var _ColorPickerConverter2 = _interopRequireDefault(_ColorPickerConverter); var _ColorPickerActions = require('./ColorPickerActions'); var _ColorPickerActions2 = _interopRequireDefault(_ColorPickerActions); var _ColorPicker = require('./ColorPicker.scss'); var _ColorPicker2 = _interopRequireDefault(_ColorPicker); 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 FALLBACK_COLOR = (0, _color3.default)('#86c6e5'); /** * Color Picker * * Under the hood uses color manipulation library [https://github.com/Qix-/color](https://github.com/Qix-/color). * Value for this component can be given in `string` or `object` format. * The callbacks always respond with color `object` format. */ var ColorPicker = (_temp = _class = function (_WixComponent) { _inherits(ColorPicker, _WixComponent); function ColorPicker(props) { _classCallCheck(this, ColorPicker); var _this = _possibleConstructorReturn(this, (ColorPicker.__proto__ || Object.getPrototypeOf(ColorPicker)).call(this, props)); _this.change = _this.change.bind(_this); _this.confirm = _this.confirm.bind(_this); _this.cancel = _this.cancel.bind(_this); var _color = safeColor(props.value) || FALLBACK_COLOR; _this.state = { current: _color, previous: _color }; return _this; } _createClass(ColorPicker, [{ key: 'render', value: function render() { var _props = this.props, showHistory = _props.showHistory, showInput = _props.showInput, showConverter = _props.showConverter, children = _props.children; var _state = this.state, current = _state.current, previous = _state.previous; return _react2.default.createElement( 'div', { className: _ColorPicker2.default.root }, _react2.default.createElement(_ColorPickerHsb2.default, { current: current, onChange: this.change }), _react2.default.createElement(_ColorPickerHue2.default, { current: current, onChange: this.change }), _react2.default.createElement(_ColorPickerHistory2.default, { show: showHistory, current: current, previous: previous, onClick: this.change }), _react2.default.createElement(_ColorPickerConverter2.default, { dataHook: 'color-picker-converter', showConverter: showConverter, showInput: showInput, current: current, onChange: this.change, onEnter: this.confirm }), children && _react2.default.createElement( 'div', { className: _ColorPicker2.default.children }, children ), _react2.default.createElement(_ColorPickerActions2.default, { onConfirm: this.confirm, onCancel: this.cancel }) ); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(props) { var _color = safeColor(props.value); if (_color && !equal(_color, this.state.current)) { this.setState({ current: _color }); } } }, { key: 'change', value: function change(_color) { var _this2 = this; this.setState({ current: _color }, function () { _this2.props.onChange(_color); }); } }, { key: 'confirm', value: function confirm() { this.setState({ previous: this.state.current }); this.props.onConfirm(this.state.current); } }, { key: 'cancel', value: function cancel() { this.props.onCancel(this.state.previous); } }]); return ColorPicker; }(_WixComponent3.default), _class.displayName = 'ColorPicker', _class.propTypes = { /** Current color, can be given in `string` or `object` format [https://github.com/Qix-/color](https://github.com/Qix-/color) */ value: (0, _propTypes.oneOfType)([_propTypes.string, _propTypes.object]).isRequired, /** Should current/previous color be displayed */ showHistory: _propTypes.bool, /** Should `HEX`/`RGB`/`HSB` converter tabs be displayed */ showConverter: _propTypes.bool, /** Should color input (in `HEX` mode) be displayed. This is relevant only if `showConverter` is `true` */ showInput: _propTypes.bool, /** Handle color change event. */ onChange: _propTypes.func.isRequired, /** Handle cancel button click */ onCancel: _propTypes.func.isRequired, /** Handle confirm button click */ onConfirm: _propTypes.func.isRequired, /** Children would be rendered above action buttons */ children: _propTypes.node }, _class.defaultProps = { showHistory: false, showConverter: true, showInput: true }, _temp); exports.default = ColorPicker; function equal(color1, color2) { return color1.hex() === color2.hex(); } function safeColor(input) { try { return (0, _color3.default)(input); } catch (error) { return null; } }