react-color
Version:
A Collection of Color Pickers from Sketch, Photoshop, Chrome & more
86 lines (67 loc) • 4.08 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
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, { Component, PureComponent } from 'react';
import debounce from 'lodash-es/debounce';
import * as color from '../../helpers/color';
export var ColorWrap = function ColorWrap(Picker) {
var ColorPicker = function (_ref) {
_inherits(ColorPicker, _ref);
function ColorPicker(props) {
_classCallCheck(this, ColorPicker);
var _this = _possibleConstructorReturn(this, (ColorPicker.__proto__ || Object.getPrototypeOf(ColorPicker)).call(this));
_this.handleChange = function (data, event) {
var isValidColor = color.simpleCheckForValidColor(data);
if (isValidColor) {
var colors = color.toState(data, data.h || _this.state.oldHue);
_this.setState(colors);
_this.props.onChangeComplete && _this.debounce(_this.props.onChangeComplete, colors, event);
_this.props.onChange && _this.props.onChange(colors, event);
}
};
_this.handleSwatchHover = function (data, event) {
var isValidColor = color.simpleCheckForValidColor(data);
if (isValidColor) {
var colors = color.toState(data, data.h || _this.state.oldHue);
_this.props.onSwatchHover && _this.props.onSwatchHover(colors, event);
}
};
_this.state = _extends({}, color.toState(props.color, 0));
_this.debounce = debounce(function (fn, data, event) {
fn(data, event);
}, 100);
return _this;
}
_createClass(ColorPicker, [{
key: 'render',
value: function render() {
var optionalEvents = {};
if (this.props.onSwatchHover) {
optionalEvents.onSwatchHover = this.handleSwatchHover;
}
return React.createElement(Picker, _extends({}, this.props, this.state, {
onChange: this.handleChange
}, optionalEvents));
}
}], [{
key: 'getDerivedStateFromProps',
value: function getDerivedStateFromProps(nextProps, state) {
return _extends({}, color.toState(nextProps.color, state.oldHue));
}
}]);
return ColorPicker;
}(PureComponent || Component);
ColorPicker.propTypes = _extends({}, Picker.propTypes);
ColorPicker.defaultProps = _extends({}, Picker.defaultProps, {
color: {
h: 250,
s: 0.50,
l: 0.20,
a: 1
}
});
return ColorPicker;
};
export default ColorWrap;