inline defaultState

Otherwise we will be mutating the refs, and items from the first Menu on the page
This commit is contained in:
Robin Malfait
2020-09-17 15:58:37 +02:00
parent 6757fad6a4
commit 59870594c1
@@ -211,15 +211,6 @@ function useMenuContext(component: string) {
return context
}
const defaultState: StateDefinition = {
menuState: MenuStates.Closed,
buttonRef: React.createRef(),
itemsRef: React.createRef(),
items: [],
searchQuery: '',
activeItemIndex: null,
}
function stateReducer(state: StateDefinition, action: Actions) {
return match(action.type, reducers, state, action)
}
@@ -234,7 +225,14 @@ export function Menu<TTag extends React.ElementType = typeof DEFAULT_MENU_TAG>(
props: Props<TTag, MenuRenderPropArg>
) {
const d = useDisposables()
const reducerBag = React.useReducer(stateReducer, defaultState)
const reducerBag = React.useReducer(stateReducer, {
menuState: MenuStates.Closed,
buttonRef: React.createRef(),
itemsRef: React.createRef(),
items: [],
searchQuery: '',
activeItemIndex: null,
} as StateDefinition)
const [{ menuState, itemsRef, buttonRef }, dispatch] = reducerBag
React.useEffect(() => {