@grafana/ui
Version:
Grafana Components Library
45 lines (40 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
;
const ElementSelectionContext = React.createContext(void 0);
function useElementSelection(id) {
if (!id) {
return {};
}
const context = React.useContext(ElementSelectionContext);
if (!context) {
return {};
}
const isSelected = context.selected.some((item) => item.id === id);
const onSelect = React.useCallback(
(evt, options = {}) => {
var _a, _b;
if (!context.enabled) {
return;
}
evt.stopPropagation();
if (evt.shiftKey) {
evt.preventDefault();
(_a = window.getSelection()) == null ? void 0 : _a.empty();
}
context.onSelect({ id }, { ...options, multi: (_b = options.multi) != null ? _b : evt.shiftKey });
},
[context, id]
);
const onClear = React.useCallback(() => {
if (!context.enabled) {
return;
}
context.onClear();
}, [context]);
return { isSelected, onSelect, onClear, isSelectable: context.enabled };
}
exports.ElementSelectionContext = ElementSelectionContext;
exports.useElementSelection = useElementSelection;
//# sourceMappingURL=ElementSelectionContext.cjs.map