-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmenu.tsx
175 lines (163 loc) · 5.86 KB
/
menu.tsx
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, {
Children,
cloneElement,
FC,
KeyboardEvent,
MouseEvent,
ReactElement,
useEffect,
useState
} from 'react'
import classNames from 'classnames'
import useRovingIndex from '../common/event-utils/use-roving-index'
import { isActionKey } from '../common/event-utils'
import { withForwardRef } from '../common/component-utils'
import EbayMenuItem, { MenuItemProps } from './menu-item'
import { Key } from '../common/event-utils/types'
import type { EbayMenuProps } from './types'
const EbayMenu: FC<EbayMenuProps> = ({
baseEl: Container = 'span',
type,
priority = 'secondary',
checked,
className,
autofocus,
onClick = () => {},
onKeyDown = () => {},
onChange = () => {},
onSelect = () => {},
forwardedRef,
menuRef,
children,
...rest
}) => {
const childrenArray = Children.toArray(children) as ReactElement[]
const [focusedIndex, setFocusedIndex] = useRovingIndex(children, EbayMenuItem, autofocus === true ? 0 : undefined)
const [checkedIndexes, setCheckedIndexes] = useState<boolean[]>(childrenArray.map(() => false))
const valuesFromChecked = (indexes: boolean[]): string[] =>
childrenArray.reduce((values, item, i) =>
indexes[i] ? [...values, item.props.value] : values, [])
const indexesFromChecked = (indexes: boolean[]): number[] =>
indexes.reduce((all, value, i) => value ? [...all, i] : all, [])
const eventProps = (index: number, indexes: boolean[]) => ({
index,
checked: indexesFromChecked(indexes)
})
const checkboxEventProps = (index: number, indexes: boolean[]) => ({
...eventProps(index, indexes),
indexes: indexesFromChecked(indexes),
checkedValues: valuesFromChecked(indexes)
})
const updateIndex = (index: number, value: boolean, resetOthers = false): boolean[] | void => {
let anyChanges = false
const newValues = checkedIndexes.map((indexChecked, i) => {
const defaultValue = resetOthers ? false : indexChecked
if (index === i) {
if (indexChecked !== value) {
anyChanges = true
}
return value
}
return defaultValue
})
if (anyChanges) {
setCheckedIndexes(newValues)
return newValues
}
}
const selectIndex = (index: number): boolean[] | void => {
switch (type) {
case 'radio':
return updateIndex(index, true, true)
case 'checkbox':
return updateIndex(index, !checkedIndexes[index], false)
default:
return checkedIndexes.map((_, i) => i === index)
}
}
useEffect(() => {
if (type === 'radio') {
if (checked === undefined) {
const checkedIndex = childrenArray.findIndex(child => child.props.checked)
if (checkedIndex > -1) {
selectIndex(checkedIndex)
}
} else {
selectIndex(checked)
}
} else if (type === 'checkbox') {
setCheckedIndexes(childrenArray.map(child => Boolean(child.props.checked)))
}
}, [])
const handleChange = (
e: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>,
index: number,
newValues: boolean[]
) => {
switch (type) {
case 'radio':
case 'checkbox':
return onChange(e, checkboxEventProps(index, newValues))
default:
return onSelect(e, eventProps(index, newValues))
}
}
const handleKeyDown = (e: KeyboardEvent<HTMLElement>, index: number) => {
let newValues
if (isActionKey(e.key as Key)) {
newValues = selectIndex(index)
if (newValues) {
handleChange(e, index, newValues)
}
}
switch (type) {
case 'radio':
case 'checkbox':
return onKeyDown(e, checkboxEventProps(index, newValues || checkedIndexes))
default:
return onKeyDown(e, eventProps(index, newValues || checkedIndexes))
}
}
const handleClick = (e: MouseEvent<HTMLElement>, index: number) => {
setFocusedIndex(index)
const newValues = selectIndex(index)
if (newValues) {
handleChange(e, index, newValues)
}
}
return (
<Container {...rest} className={classNames(className, 'menu')} ref={forwardedRef as any}>
<div className="menu__items" role="menu" ref={menuRef}>
{childrenArray.map((child: ReactElement, i) => {
const {
onClick: onItemClick = () => {},
onFocus: onItemFocus = () => {},
onKeyDown: onItemKeyDown = () => {},
...itemRest
}: MenuItemProps = child.props
return cloneElement(child, {
...itemRest,
type,
focused: i === focusedIndex,
tabIndex: focusedIndex === undefined ? 0 : -1,
checked: checkedIndexes[i],
onFocus: e => {
setFocusedIndex(i)
onItemFocus(e)
},
onClick: e => {
handleClick(e, i)
onItemClick(e)
onClick(e)
},
onKeyDown: e => {
handleKeyDown(e, i)
onItemKeyDown(e)
}
} as MenuItemProps)
})}
</div>
</Container>
)
}
export default withForwardRef(EbayMenu)