UNPKG

1.22 kBJavaScriptView Raw
1import * as React from 'react';
2import useMessageBus from '../utils/useMessageBus';
3const SELECTION_CHANGE_TOPIC = 'select:change-selection';
4const HIGHLIGHT_CHANGE_TOPIC = 'select:change-highlight';
5/**
6 * @ignore - internal hook.
7 *
8 * This hook is used to notify any interested components about changes in the Select's selection and highlight.
9 */
10export default function useSelectChangeNotifiers() {
11 const messageBus = useMessageBus();
12 const notifySelectionChanged = React.useCallback(newSelectedItems => {
13 messageBus.publish(SELECTION_CHANGE_TOPIC, newSelectedItems);
14 }, [messageBus]);
15 const notifyHighlightChanged = React.useCallback(newHighlightedItem => {
16 messageBus.publish(HIGHLIGHT_CHANGE_TOPIC, newHighlightedItem);
17 }, [messageBus]);
18 const registerSelectionChangeHandler = React.useCallback(handler => {
19 return messageBus.subscribe(SELECTION_CHANGE_TOPIC, handler);
20 }, [messageBus]);
21 const registerHighlightChangeHandler = React.useCallback(handler => {
22 return messageBus.subscribe(HIGHLIGHT_CHANGE_TOPIC, handler);
23 }, [messageBus]);
24 return {
25 notifySelectionChanged,
26 notifyHighlightChanged,
27 registerSelectionChangeHandler,
28 registerHighlightChangeHandler
29 };
30}
\No newline at end of file