Skip to content

Commit

Permalink
Simplify Menu's onItemSelect handling internally
Browse files Browse the repository at this point in the history
  • Loading branch information
gnapse committed Mar 7, 2024
1 parent 7bf3341 commit cca366b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Reactist follows [semantic versioning](https://semver.org/) and doesn't introduce breaking changes (API-wise) in minor or patch releases. However, the appearance of a component might change in a minor or patch release so keep an eye on redesigns and make sure your app still looks and feels like you expect it.

# v24.1.1-beta

- [Fix] It was possible to leave a tooltip in a state in which it remained visible all the time. This release fixes the issue.

# v24.1.0-beta

- [Feat] Include changes from [v23.2.0](#v2320) in the beta release
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "[email protected]",
"url": "http://doist.com"
},
"version": "24.1.0-beta",
"version": "24.1.1-beta",
"license": "MIT",
"homepage": "https://github.com/Doist/reactist#readme",
"repository": {
Expand Down
17 changes: 5 additions & 12 deletions src/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type NativeProps<E extends HTMLElement> = React.DetailedHTMLProps<React.HTMLAttr

type MenuContextState = {
menuStore: MenuStore
handleItemSelect: (value: string | null | undefined) => void
handleItemSelect?: (value: string | null | undefined) => void
getAnchorRect: (() => { x: number; y: number }) | null
setAnchorRect: (rect: { x: number; y: number } | null) => void
}
Expand Down Expand Up @@ -76,16 +76,9 @@ function Menu({ children, onItemSelect, ...props }: MenuProps) {
const getAnchorRect = React.useMemo(() => (anchorRect ? () => anchorRect : null), [anchorRect])
const menuStore = useMenuStore({ focusLoop: true, ...props })

const handleItemSelect = React.useCallback(
function handleItemSelect(value: string | null | undefined) {
onItemSelect?.(value)
},
[onItemSelect],
)

const value: MenuContextState = React.useMemo(
() => ({ menuStore, handleItemSelect, getAnchorRect, setAnchorRect }),
[menuStore, handleItemSelect, getAnchorRect, setAnchorRect],
() => ({ menuStore, handleItemSelect: onItemSelect, getAnchorRect, setAnchorRect }),
[menuStore, onItemSelect, getAnchorRect, setAnchorRect],
)

return <MenuContext.Provider value={value}>{children}</MenuContext.Provider>
Expand Down Expand Up @@ -249,7 +242,7 @@ const MenuItem = polymorphicComponent<'button', MenuItemProps>(function MenuItem
const onSelectResult: unknown =
onSelect && !event.defaultPrevented ? onSelect() : undefined
const shouldClose = onSelectResult !== false && hideOnSelect
handleItemSelect(value)
handleItemSelect?.(value)
if (shouldClose) hide()
},
[onSelect, onClick, handleItemSelect, hideOnSelect, hide, value],
Expand Down Expand Up @@ -307,7 +300,7 @@ const SubMenu = React.forwardRef<HTMLDivElement, SubMenuProps>(function SubMenu(
const handleSubItemSelect = React.useCallback(
function handleSubItemSelect(value: string | null | undefined) {
if (onItemSelect) onItemSelect(value)
parentMenuItemSelect(value)
parentMenuItemSelect?.(value)
parentMenuHide()
},
[parentMenuHide, parentMenuItemSelect, onItemSelect],
Expand Down

0 comments on commit cca366b

Please sign in to comment.