-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSwitcher.js
119 lines (101 loc) · 2.96 KB
/
Switcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, { useRef } from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
import { usePrefix } from '../../internal/usePrefix';
import { useMergedRefs } from '../../internal/useMergedRefs';
const Switcher = React.forwardRef(function Switcher(props, forwardRef) {
const switcherRef = useRef(null);
const ref = useMergedRefs([switcherRef, forwardRef]);
const prefix = usePrefix();
const {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
className: customClassName,
children,
expanded,
} = props;
const accessibilityLabel = {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
};
const className = cx(`${prefix}--switcher`, {
[customClassName]: !!customClassName,
});
const handleSwitcherItemFocus = ({ currentIndex, direction }) => {
const enabledIndices = React.Children.toArray(children).reduce(
(acc, curr, i) => {
if (Object.keys(curr.props).length !== 0) {
acc.push(i);
}
return acc;
},
[]
);
const nextValidIndex = (() => {
const nextIndex = enabledIndices.indexOf(currentIndex) + direction;
switch (enabledIndices[nextIndex]) {
case undefined:
if (direction === -1) {
return enabledIndices[enabledIndices.length - 1];
}
return 0;
default:
return enabledIndices[nextIndex];
}
})();
const switcherItem =
switcherRef.current.children[nextValidIndex].children[0];
switcherItem?.focus();
};
const childrenWithProps = React.Children.toArray(children).map(
(child, index) => {
// handleSwitcherItemFocus should only be passed down if the child is a SwitcherItem
// SwitcherDivider, for example, does not accept a handleSwitcherItemFocus prop
if (child.type?.displayName === 'SwitcherItem') {
return React.cloneElement(child, {
handleSwitcherItemFocus,
index,
key: index,
expanded,
});
}
return React.cloneElement(child, {
index,
key: index,
expanded,
});
}
);
return (
<ul ref={ref} className={className} {...accessibilityLabel}>
{childrenWithProps}
</ul>
);
});
Switcher.displayName = 'Switcher';
Switcher.propTypes = {
/**
* Required props for accessibility label on the underlying menu
*/
...AriaLabelPropType,
/**
* expects to receive <SwitcherItem />
*/
children: PropTypes.node.isRequired,
/**
* Optionally provide a custom class to apply to the underlying `<ul>` node
*/
className: PropTypes.string,
/**
* Specify whether the panel is expanded
*/
expanded: PropTypes.bool,
};
export default Switcher;