UNPKG

wix-style-react

Version:
264 lines (220 loc) • 9.03 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["placeholder", "size"]; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import Input from '../Input'; import { Hash } from './components/Hash'; import { ColorViewer } from './components/ColorViewer'; import { validateHex, normalizeHexInput } from './hex-helpers'; import Box from '../Box'; var ColorInput = /*#__PURE__*/function (_React$Component) { _inherits(ColorInput, _React$Component); var _super = _createSuper(ColorInput); function ColorInput(props) { var _this; _classCallCheck(this, ColorInput); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "_renderPrefix", function () { var _this$props = _this.props, disabled = _this$props.disabled, size = _this$props.size; var _this$state = _this.state, active = _this$state.active, value = _this$state.value; var hash = /*#__PURE__*/React.createElement(Input.Affix, null, /*#__PURE__*/React.createElement(Hash, { disabled: disabled, size: size })); return active || value ? hash : undefined; }); _defineProperty(_assertThisInitialized(_this), "_renderSuffix", function () { var _this$state2 = _this.state, value = _this$state2.value, active = _this$state2.active; var _this$props2 = _this.props, size = _this$props2.size, popoverPlacement = _this$props2.popoverPlacement, popoverAppendTo = _this$props2.popoverAppendTo, disabled = _this$props2.disabled, colorPickerChildren = _this$props2.colorPickerChildren, onAddColor = _this$props2.onAddColor, addTooltipContent = _this$props2.addTooltipContent, placeholder = _this$props2.placeholder, popoverProps = _this$props2.popoverProps; return /*#__PURE__*/React.createElement(Box, { verticalAlign: "middle" }, /*#__PURE__*/React.createElement(ColorViewer, { value: value, active: active, disabled: disabled, size: size, placement: popoverPlacement, appendTo: popoverAppendTo, onClick: _this.click, onChange: _this._onPickerChange, onCancel: _this.cancel, onConfirm: _this.confirm, onClickOutside: _this.confirm, children: colorPickerChildren, onAddColor: onAddColor, addTooltipContent: addTooltipContent, placeholder: placeholder, popoverProps: popoverProps })); }); _defineProperty(_assertThisInitialized(_this), "_onChange", function (evt) { var onChange = _this.props.onChange; var value = normalizeHexInput(evt.target.value); _this.setState({ value: value }, function () { return onChange(value); }); }); _defineProperty(_assertThisInitialized(_this), "_onPickerChange", function (value) { var onChange = _this.props.onChange; _this.setState({ active: true, value: value }, function () { return onChange(value); }); }); _defineProperty(_assertThisInitialized(_this), "_onFocus", function () { return _this.setState({ active: true }); }); _defineProperty(_assertThisInitialized(_this), "_keyDown", function (e) { e.stopPropagation(); e.key === 'Enter' && _this.confirm(); e.key === 'Escape' && _this.cancel(); }); _defineProperty(_assertThisInitialized(_this), "click", function () { _this.input.focus(); _this.setState({ active: true }); }); _defineProperty(_assertThisInitialized(_this), "confirm", function () { var _this$props3 = _this.props, onConfirm = _this$props3.onConfirm, onChange = _this$props3.onChange; var value = validateHex(_this.state.value); _this.setState({ active: false, value: value, previous: value }, function () { onConfirm(value); onChange(value); }); }); _defineProperty(_assertThisInitialized(_this), "cancel", function () { var _this$props4 = _this.props, onCancel = _this$props4.onCancel, onChange = _this$props4.onChange; var previous = _this.state.previous; _this.setState({ active: false, value: previous }, function () { onCancel(previous); onChange(previous); }); }); _this.state = { active: false, previous: props.value, value: '' }; return _this; } _createClass(ColorInput, [{ key: "render", value: function render() { var _this2 = this; var _this$props5 = this.props, placeholder = _this$props5.placeholder, size = _this$props5.size, rest = _objectWithoutProperties(_this$props5, _excluded); var _this$state3 = this.state, active = _this$state3.active, value = _this$state3.value; return /*#__PURE__*/React.createElement(Input, _extends({}, rest, { ref: function ref(input) { return _this2.input = input; }, placeholder: active ? '' : placeholder, size: size, onKeyDown: this._keyDown, onChange: this._onChange, onFocus: this._onFocus, onInputClicked: this.click, value: value.replace('#', ''), prefix: this._renderPrefix(), suffix: this._renderSuffix() })); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (!state.active && props.value !== state.value) { return { value: normalizeHexInput(props.value) }; } return {}; } }]); return ColorInput; }(React.Component); _defineProperty(ColorInput, "displayName", 'ColorInput'); _defineProperty(ColorInput, "propTypes", { /** Defines placeholder text to display on an empty input */ placeholder: PropTypes.string, /** Specifies whether the input should be disabled */ disabled: PropTypes.bool, /** Specifies the status of a field */ status: PropTypes.oneOf(['error', 'warning', 'loading']), /** Defines the message to display on status icon hover. If not given or empty, there will be no tooltip. */ statusMessage: PropTypes.node, /** Controls input size */ size: PropTypes.oneOf(['small', 'medium', 'large']), /** Defines input value */ value: PropTypes.string.isRequired, /** Returns confirmed value */ onConfirm: PropTypes.func, /** Returns last confirmed value from `value` prop */ onCancel: PropTypes.func, /** Returns changed value of input or color picker */ onChange: PropTypes.func, /** Defines child items to render inside of a component. Accepts any kind of content. It receives the `changeColor` function to control <ColorInput/> value. */ colorPickerChildren: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), /** Defines a callback handler with color HEX string. Handler is called whenever the `Add Color` button is pressed. */ onAddColor: PropTypes.func, /** Defines content to show in add button tooltip. Does not appear if `onAdd` is not passed. */ addTooltipContent: PropTypes.node, /** Allows to pass popover props. See <Popover/> API for a full list. */ popoverProps: PropTypes.object }); _defineProperty(ColorInput, "defaultProps", { placeholder: '', size: 'medium', onChange: function onChange() {}, onConfirm: function onConfirm() {}, onCancel: function onCancel() {}, popoverProps: {}, value: '' }); export default ColorInput;