wix-style-react
Version:
wix-style-react
162 lines (135 loc) • 6.12 kB
JavaScript
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, string, func, bool, oneOfType, node } from 'prop-types';
import WixComponent from '../BaseComponents/WixComponent';
import ColorPickerHsb from './ColorPickerHsb';
import ColorPickerHue from './ColorPickerHue';
import ColorPickerHistory from './ColorPickerHistory';
import ColorPickerConverter from './ColorPickerConverter';
import ColorPickerActions from './ColorPickerActions';
import css from './ColorPicker.scss';
var FALLBACK_COLOR = color('#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 React.createElement(
'div',
{ className: css.root },
React.createElement(ColorPickerHsb, { current: current, onChange: this.change }),
React.createElement(ColorPickerHue, { current: current, onChange: this.change }),
React.createElement(ColorPickerHistory, {
show: showHistory,
current: current,
previous: previous,
onClick: this.change
}),
React.createElement(ColorPickerConverter, {
dataHook: 'color-picker-converter',
showConverter: showConverter,
showInput: showInput,
current: current,
onChange: this.change,
onEnter: this.confirm
}),
children && React.createElement(
'div',
{ className: css.children },
children
),
React.createElement(ColorPickerActions, { 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;
}(WixComponent), _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: oneOfType([string, object]).isRequired,
/** Should current/previous color be displayed */
showHistory: bool,
/** Should `HEX`/`RGB`/`HSB` converter tabs be displayed */
showConverter: bool,
/** Should color input (in `HEX` mode) be displayed. This is relevant only if `showConverter` is `true` */
showInput: bool,
/** Handle color change event. */
onChange: func.isRequired,
/** Handle cancel button click */
onCancel: func.isRequired,
/** Handle confirm button click */
onConfirm: func.isRequired,
/** Children would be rendered above action buttons */
children: node
}, _class.defaultProps = {
showHistory: false,
showConverter: true,
showInput: true
}, _temp);
export { ColorPicker as default };
function equal(color1, color2) {
return color1.hex() === color2.hex();
}
function safeColor(input) {
try {
return color(input);
} catch (error) {
return null;
}
}