Repository URL to install this package:
|
Version:
3.12.16 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["colors", "value", "multiple", "hideClearBtn", "onChange", "height", "width", "itemMargin", "showPickerPanel"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { CrossOutline } from '@scaleflex/icons';
import { useCallback } from 'react';
import Styled from './ColorPicker.styled';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var pickerTimeout = null;
var whiteColor = '#ffffff';
var ColorPicker = function ColorPicker(_ref) {
var _ref$colors = _ref.colors,
colors = _ref$colors === void 0 ? [] : _ref$colors,
_ref$value = _ref.value,
value = _ref$value === void 0 ? '' : _ref$value,
_ref$multiple = _ref.multiple,
multiple = _ref$multiple === void 0 ? false : _ref$multiple,
_ref$hideClearBtn = _ref.hideClearBtn,
hideClearBtn = _ref$hideClearBtn === void 0 ? false : _ref$hideClearBtn,
_ref$onChange = _ref.onChange,
onChange = _ref$onChange === void 0 ? function () {
return '';
} : _ref$onChange,
_ref$height = _ref.height,
height = _ref$height === void 0 ? 28 : _ref$height,
_ref$width = _ref.width,
width = _ref$width === void 0 ? 28 : _ref$width,
itemMargin = _ref.itemMargin,
showPickerPanel = _ref.showPickerPanel,
others = _objectWithoutProperties(_ref, _excluded);
var prepareMultipleValues = function prepareMultipleValues(color) {
if (Array.isArray(value)) {
return value.includes(color) ? value.filter(function (val) {
return val !== color;
}) : [].concat(_toConsumableArray(value), [color]);
}
return value ? value === color ? [color] : [value, color] : [color];
};
var selectColor = function selectColor(color) {
onChange(multiple ? prepareMultipleValues(color) : color);
};
var clearAll = function clearAll() {
if (!value === '' || value.length > 0) {
onChange('');
}
};
var updateColorFromPicker = useCallback(function (e) {
var value = e.target.value;
if (pickerTimeout) {
clearTimeout(pickerTimeout);
}
pickerTimeout = setTimeout(function () {
selectColor(value);
pickerTimeout = null;
}, 30);
}, []);
var isSelected = function isSelected(color) {
return Array.isArray(value) ? value.includes(color) : value.toLowerCase() === (color === null || color === void 0 ? void 0 : color.toLowerCase());
};
var renderSelectedColorIfNotThere = useCallback(function () {
if (value && !multiple && !colors.includes(value)) {
return renderColor(value);
}
}, [colors, value, multiple]);
var renderColor = function renderColor(color) {
var clearButton = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
return /*#__PURE__*/_jsx(Styled.ColorItem, {
className: "filerobot-common-ColorPicker-colorItem",
style: {
background: color.color || color,
height: height,
width: width,
margin: itemMargin
},
isWhite: (color.color || color) === whiteColor,
isSelected: isSelected(color.value || color),
clearButton: clearButton,
onClick: clearButton ? clearAll : function () {
return selectColor(color.value || color);
}
// $background={color}
,
children: clearButton && /*#__PURE__*/_jsx(CrossOutline, {
size: 12
})
}, color.value || color);
};
return /*#__PURE__*/_jsxs(Styled.ColorPickerWrapper, _objectSpread(_objectSpread({
className: "filerobot-common-ColorPicker"
}, others), {}, {
children: [!hideClearBtn && renderColor(whiteColor, true), renderSelectedColorIfNotThere(), colors.map(function (color) {
return renderColor(color);
}), showPickerPanel && /*#__PURE__*/_jsx("input", {
type: "color",
onInput: updateColorFromPicker
})]
}));
};
export default ColorPicker;