Skip to content

Commit

Permalink
Update SelectMenuItem types
Browse files Browse the repository at this point in the history
  • Loading branch information
colebemis committed Apr 1, 2021
1 parent 1c7ea7f commit f7bdcbf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/SelectMenu/SelectMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,41 @@ export const listItemStyles = css`
}
`

type StyledItemProps = SystemCommonProps & SxProp

const StyledItem = styled.a.attrs(() => ({
role: 'menuitemcheckbox'
}))<SystemCommonProps & SxProp>`
}))<StyledItemProps>`
${listItemStyles}
${COMMON}
${sx};
`

type SelectMenuItemInteralProps = {
type SelectMenuItemInternalProps = {
as?: React.ElementType
selected?: boolean
} & ComponentProps<typeof StyledItem>
onClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void
disabled?: boolean
} & StyledItemProps

const SelectMenuItem = forwardRef<HTMLAnchorElement, SelectMenuItemInteralProps>(
({children, selected, theme, onClick, ...rest}, forwardedRef) => {
const SelectMenuItem = forwardRef<HTMLElement, SelectMenuItemInternalProps>(
({children, selected, onClick, ...rest}, forwardedRef) => {
const menuContext = useContext(MenuContext)
const backupRef = useRef<HTMLAnchorElement>(null)
const backupRef = useRef<HTMLElement>(null)
const itemRef = forwardedRef ?? backupRef

// close the menu when an item is clicked
// this can be overriden if the user provides a `onClick` prop and prevents default in it
const handleClick = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
const handleClick = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
onClick && onClick(e)

if (!e.defaultPrevented) {
menuContext.setOpen?.(false)
}
}
return (
<StyledItem ref={itemRef} {...rest} theme={theme} onClick={handleClick} aria-checked={selected}>
<StyledOcticon theme={theme} className="SelectMenu-icon SelectMenu-selected-icon" icon={CheckIcon} />
<StyledItem ref={itemRef} {...rest} onClick={handleClick} aria-checked={selected}>
<StyledOcticon className="SelectMenu-icon SelectMenu-selected-icon" icon={CheckIcon} />
{children}
</StyledItem>
)
Expand Down

0 comments on commit f7bdcbf

Please sign in to comment.