@bigfishtv/cockpit
Version:
102 lines (87 loc) • 4.21 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 _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 PropTypes from 'prop-types';
import React, { Component } from 'react';
import { SketchPicker } from 'react-color';
import classnames from 'classnames';
/**
* Displays a color block that can be clicked to modify color using 'react-color' color picker
*/
var Color = (_temp = _class = function (_Component) {
_inherits(Color, _Component);
function Color(props) {
_classCallCheck(this, Color);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.toggleDisplay = function () {
_this.setState({ displayColorPicker: !_this.state.displayColorPicker });
};
_this.state = { displayColorPicker: false };
return _this;
}
Color.prototype.render = function render() {
var _this2 = this;
var displayColorPicker = this.state.displayColorPicker;
var _props = this.props,
size = _props.size,
value = _props.value,
style = _props.style,
disableAlpha = _props.disableAlpha,
transformValue = _props.transformValue,
componentProps = _props.componentProps;
return React.createElement(
'div',
{
className: classnames('color', size),
style: _extends({}, style, { backgroundColor: value }),
onClick: function onClick() {
return !displayColorPicker && _this2.toggleDisplay();
} },
this.state.displayColorPicker && React.createElement(
'div',
{ className: 'color-picker-wrapper' },
React.createElement('div', { className: 'color-picker-cover', onClick: this.toggleDisplay }),
React.createElement(SketchPicker, _extends({}, componentProps, {
disableAlpha: disableAlpha,
color: value || 'rgba(255,255,255,0)',
onChangeComplete: function onChangeComplete(value) {
return _this2.props.onChange(transformValue(value));
}
}))
)
);
};
return Color;
}(Component), _class.propTypes = {
value: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
/** xsmall, small, medium, large, xlarge, appends to className. Default is medium */
size: PropTypes.string,
/** Style to apply to wrapper div */
style: PropTypes.object,
/** Show / hide alpha slider. Should typically be disabled when used as a Field input */
disableAlpha: PropTypes.bool,
/** Reference to a different react-color component, is SketchPicker by default. */
Component: PropTypes.any,
/** Transformation function that provides value to onChange. Returns HEX by default, options include: hex, hsl {h,s,l,a}, hsv {h,s,v,a}, rgb {r,g,b,a} */
transformValue: PropTypes.func,
/** Returns value supplied by transformValue prop */
onChange: PropTypes.func,
/** props to be spread over Component, e.g. presetColors[] */
componentProps: PropTypes.object
}, _class.defaultProps = {
size: null,
value: '#FFFFFF',
style: {},
disableAlpha: true,
Component: SketchPicker,
onChange: function onChange() {
return console.warn('no onChange prop provided for Color');
},
transformValue: function transformValue(value) {
return value.hex;
},
componentProps: {}
}, _temp);
export { Color as default };