gather-content-ui
Version:
GatherContent UI Library
59 lines (58 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useObjectSelector = useObjectSelector;
var _react = require("react");
var _index = require("./index");
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 useObjectSelector(id, type, idsToSelect) {
var isDisabledChecker = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
return false;
};
var data = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
var _useContext = (0, _react.useContext)(_index.SelectionContext),
selected = _useContext.selected,
selectMultiple = _useContext.selectMultiple,
deselectMultiple = _useContext.deselectMultiple,
updateSelected = _useContext.updateSelected,
currentSelectedType = _useContext.currentSelectedType,
intendedToSelect = _useContext.intendedToSelect,
setIntendedToSelect = _useContext.setIntendedToSelect;
var isSelected = selected.indexOf(id) !== -1;
// @ts-expect-error TS(2554): Expected 0 arguments, but got 2.
var isDisabled = isDisabledChecker(currentSelectedType, isSelected);
var isHovered = intendedToSelect.indexOf(id) !== -1;
var getIds = function getIds() {
return _toConsumableArray(new Set(_toConsumableArray(idsToSelect)));
};
var handleClick = idsToSelect.length > 1 ? function () {
return isSelected ? deselectMultiple(getIds(), type) : selectMultiple(getIds(), type, data);
} :
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
function () {
return updateSelected(idsToSelect[0], type, data[id]);
};
var handleMouseEnter = function handleMouseEnter() {
return setIntendedToSelect(idsToSelect);
};
var handleMouseLeave = function handleMouseLeave() {
return setIntendedToSelect(intendedToSelect.filter(function (s) {
return idsToSelect.indexOf(s) === -1;
}));
};
return {
isSelected: isSelected,
handleClick: isDisabled ? function () {} : handleClick,
isDisabled: isDisabled,
isHovered: isHovered,
handleMouseEnter: handleMouseEnter,
handleMouseLeave: handleMouseLeave
};
}
//# sourceMappingURL=useObjectSelector.js.map