@salesforce/design-system-react
Version:
Salesforce Lightning Design System for React
139 lines (108 loc) • 6.64 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import findIndex from 'lodash.findindex';
import SwatchOption from './swatch-option';
import KEYS from '../../../utilities/key-code';
import EventUtil from '../../../utilities/event';
var SwatchPicker =
/*#__PURE__*/
function (_React$Component) {
_inherits(SwatchPicker, _React$Component);
function SwatchPicker(_props) {
var _this;
_classCallCheck(this, SwatchPicker);
_this = _possibleConstructorReturn(this, _getPrototypeOf(SwatchPicker).call(this, _props));
_defineProperty(_assertThisInitialized(_this), "selectPreviousColor", function (event, props) {
var index = findIndex(props.swatchColors, function (item) {
return item === props.color.hex;
});
var nextIndex = index === -1 || index === props.swatchColors.length - 1 ? 0 : index + 1;
var prevColor = props.swatchColors[nextIndex];
props.onSelect(event, {
hex: prevColor
});
_this.swatchColorRefs[prevColor].focus();
});
_defineProperty(_assertThisInitialized(_this), "selectNextColor", function (event, props) {
var index = findIndex(props.swatchColors, function (item) {
return item === props.color.hex;
});
var prevIndex;
if (index === 0) {
prevIndex = props.swatchColors.length - 1;
} else if (index === -1) {
prevIndex = 0;
} else {
prevIndex = index - 1;
}
var nextColor = props.swatchColors[prevIndex];
props.onSelect(event, {
hex: nextColor
});
_this.swatchColorRefs[nextColor].focus();
});
_defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (event, props) {
var _keyDownCallbacks;
var keyDownCallbacks = (_keyDownCallbacks = {}, _defineProperty(_keyDownCallbacks, KEYS.RIGHT, function () {
_this.selectPreviousColor(event, props);
}), _defineProperty(_keyDownCallbacks, KEYS.DOWN, function () {
_this.selectPreviousColor(event, props);
}), _defineProperty(_keyDownCallbacks, KEYS.LEFT, function () {
_this.selectNextColor(event, props);
}), _defineProperty(_keyDownCallbacks, KEYS.UP, function () {
_this.selectNextColor(event, props);
}), _keyDownCallbacks);
if (event.keyCode) {
if (keyDownCallbacks[event.keyCode]) {
EventUtil.trapEvent(event);
keyDownCallbacks[event.keyCode]();
}
}
});
_defineProperty(_assertThisInitialized(_this), "addRef", function (color) {
return function (el) {
_this.swatchColorRefs[color] = el;
};
});
_this.swatchColorRefs = {};
return _this;
}
_createClass(SwatchPicker, [{
key: "render",
value: function render() {
var _this2 = this;
var isSelectedColorInSwatch = this.props.swatchColors.includes(this.props.color.hex);
return React.createElement("ul", {
className: "slds-color-picker__swatches",
role: "listbox",
onKeyDown: function onKeyDown(event) {
_this2.handleKeyDown(event, _objectSpread({}, _this2.props));
}
}, this.props.swatchColors.map(function (color, index) {
return React.createElement(SwatchOption, {
color: color,
key: color,
labels: _this2.props.labels,
onSelect: _this2.props.onSelect,
swatchOptionRef: _this2.addRef(color),
workingColor: _this2.props.color,
tabIndex: _this2.props.color && _this2.props.color.hex === color || index === 0 && !isSelectedColorInSwatch ? 0 : -1
});
}));
}
}]);
return SwatchPicker;
}(React.Component);
_defineProperty(SwatchPicker, "displayName", 'SLDSSwatchPicker');
export default SwatchPicker;
//# sourceMappingURL=swatch-picker.js.map