1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import * as React from 'react';
|
3 | import { useTabsContext } from '../Tabs';
|
4 | import { TabsListActionTypes } from './useTabsList.types';
|
5 | import { useCompoundParent } from '../utils/useCompound';
|
6 | import useList from '../useList';
|
7 | import tabsListReducer from './tabsListReducer';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function useTabsList(parameters) {
|
20 | var _selectedValues$;
|
21 | var externalRef = parameters.rootRef;
|
22 | var _useTabsContext = useTabsContext(),
|
23 | _useTabsContext$direc = _useTabsContext.direction,
|
24 | direction = _useTabsContext$direc === void 0 ? 'ltr' : _useTabsContext$direc,
|
25 | onSelected = _useTabsContext.onSelected,
|
26 | _useTabsContext$orien = _useTabsContext.orientation,
|
27 | orientation = _useTabsContext$orien === void 0 ? 'horizontal' : _useTabsContext$orien,
|
28 | value = _useTabsContext.value,
|
29 | registerTabIdLookup = _useTabsContext.registerTabIdLookup,
|
30 | selectionFollowsFocus = _useTabsContext.selectionFollowsFocus;
|
31 | var _useCompoundParent = useCompoundParent(),
|
32 | subitems = _useCompoundParent.subitems,
|
33 | compoundComponentContextValue = _useCompoundParent.contextValue;
|
34 | var tabIdLookup = React.useCallback(function (tabValue) {
|
35 | var _subitems$get;
|
36 | return (_subitems$get = subitems.get(tabValue)) == null ? void 0 : _subitems$get.id;
|
37 | }, [subitems]);
|
38 | registerTabIdLookup(tabIdLookup);
|
39 | var subitemKeys = React.useMemo(function () {
|
40 | return Array.from(subitems.keys());
|
41 | }, [subitems]);
|
42 | var getTabElement = React.useCallback(function (tabValue) {
|
43 | var _subitems$get$ref$cur, _subitems$get2;
|
44 | if (tabValue == null) {
|
45 | return null;
|
46 | }
|
47 | return (_subitems$get$ref$cur = (_subitems$get2 = subitems.get(tabValue)) == null ? void 0 : _subitems$get2.ref.current) != null ? _subitems$get$ref$cur : null;
|
48 | }, [subitems]);
|
49 | var isRtl = direction === 'rtl';
|
50 | var listOrientation;
|
51 | if (orientation === 'vertical') {
|
52 | listOrientation = 'vertical';
|
53 | } else {
|
54 | listOrientation = isRtl ? 'horizontal-rtl' : 'horizontal-ltr';
|
55 | }
|
56 | var handleChange = React.useCallback(function (event, newValue) {
|
57 | var _newValue$;
|
58 | onSelected(event, (_newValue$ = newValue[0]) != null ? _newValue$ : null);
|
59 | }, [onSelected]);
|
60 | var controlledProps = React.useMemo(function () {
|
61 | if (value === undefined) {
|
62 | return {};
|
63 | }
|
64 | return value != null ? {
|
65 | selectedValues: [value]
|
66 | } : {
|
67 | selectedValues: []
|
68 | };
|
69 | }, [value]);
|
70 | var isItemDisabled = React.useCallback(function (item) {
|
71 | var _subitems$get$disable, _subitems$get3;
|
72 | return (_subitems$get$disable = (_subitems$get3 = subitems.get(item)) == null ? void 0 : _subitems$get3.disabled) != null ? _subitems$get$disable : false;
|
73 | }, [subitems]);
|
74 | var _useList = useList({
|
75 | controlledProps: controlledProps,
|
76 | disabledItemsFocusable: !selectionFollowsFocus,
|
77 | focusManagement: 'DOM',
|
78 | getItemDomElement: getTabElement,
|
79 | isItemDisabled: isItemDisabled,
|
80 | items: subitemKeys,
|
81 | rootRef: externalRef,
|
82 | onChange: handleChange,
|
83 | orientation: listOrientation,
|
84 | reducerActionContext: React.useMemo(function () {
|
85 | return {
|
86 | selectionFollowsFocus: selectionFollowsFocus || false
|
87 | };
|
88 | }, [selectionFollowsFocus]),
|
89 | selectionMode: 'single',
|
90 | stateReducer: tabsListReducer
|
91 | }),
|
92 | listContextValue = _useList.contextValue,
|
93 | dispatch = _useList.dispatch,
|
94 | getListboxRootProps = _useList.getRootProps,
|
95 | _useList$state = _useList.state,
|
96 | highlightedValue = _useList$state.highlightedValue,
|
97 | selectedValues = _useList$state.selectedValues,
|
98 | mergedRootRef = _useList.rootRef;
|
99 | React.useEffect(function () {
|
100 | if (value === undefined) {
|
101 | return;
|
102 | }
|
103 |
|
104 |
|
105 | if (value != null) {
|
106 | dispatch({
|
107 | type: TabsListActionTypes.valueChange,
|
108 | value: value
|
109 | });
|
110 | }
|
111 | }, [dispatch, value]);
|
112 | var getRootProps = function getRootProps() {
|
113 | var otherHandlers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
114 | return _extends({}, otherHandlers, getListboxRootProps(otherHandlers), {
|
115 | 'aria-orientation': orientation === 'vertical' ? 'vertical' : undefined,
|
116 | role: 'tablist'
|
117 | });
|
118 | };
|
119 | return {
|
120 | contextValue: _extends({}, compoundComponentContextValue, listContextValue),
|
121 | dispatch: dispatch,
|
122 | getRootProps: getRootProps,
|
123 | highlightedValue: highlightedValue,
|
124 | isRtl: isRtl,
|
125 | orientation: orientation,
|
126 | rootRef: mergedRootRef,
|
127 | selectedValue: (_selectedValues$ = selectedValues[0]) != null ? _selectedValues$ : null
|
128 | };
|
129 | }
|
130 | export default useTabsList; |
\ | No newline at end of file |