UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = useSelectChangeNotifiers;
8var React = _interopRequireWildcard(require("react"));
9var _useMessageBus = _interopRequireDefault(require("../utils/useMessageBus"));
10function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12const SELECTION_CHANGE_TOPIC = 'select:change-selection';
13const HIGHLIGHT_CHANGE_TOPIC = 'select:change-highlight';
14/**
15 * @ignore - internal hook.
16 *
17 * This hook is used to notify any interested components about changes in the Select's selection and highlight.
18 */
19function useSelectChangeNotifiers() {
20 const messageBus = (0, _useMessageBus.default)();
21 const notifySelectionChanged = React.useCallback(newSelectedItems => {
22 messageBus.publish(SELECTION_CHANGE_TOPIC, newSelectedItems);
23 }, [messageBus]);
24 const notifyHighlightChanged = React.useCallback(newHighlightedItem => {
25 messageBus.publish(HIGHLIGHT_CHANGE_TOPIC, newHighlightedItem);
26 }, [messageBus]);
27 const registerSelectionChangeHandler = React.useCallback(handler => {
28 return messageBus.subscribe(SELECTION_CHANGE_TOPIC, handler);
29 }, [messageBus]);
30 const registerHighlightChangeHandler = React.useCallback(handler => {
31 return messageBus.subscribe(HIGHLIGHT_CHANGE_TOPIC, handler);
32 }, [messageBus]);
33 return {
34 notifySelectionChanged,
35 notifyHighlightChanged,
36 registerSelectionChangeHandler,
37 registerHighlightChangeHandler
38 };
39}
\No newline at end of file