Skip to content

Commit b55d823

Browse files
jdrush89joshblack
andauthored
Jdrush89/fix linkitem onclick (#2757)
* Fixing the onClick of ActionList.LinkItem * Adding changeset * removing console statement * removing unused import * Fixing test Co-authored-by: Josh Black <[email protected]>
1 parent 9cb0119 commit b55d823

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

.changeset/poor-melons-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Making onClick of ActionList.LinkItem functional

src/ActionList/LinkItem.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,24 @@ export const LinkItem = React.forwardRef(({sx = {}, active, as: Component, ...pr
3939
<Item
4040
active={active}
4141
sx={{paddingY: 0, paddingX: 0}}
42-
_PrivateItemWrapper={({children, ...rest}) => (
43-
<Link as={Component} sx={merge(styles, sx as SxProp)} {...props} {...rest} ref={forwardedRef}>
44-
{children}
45-
</Link>
46-
)}
42+
_PrivateItemWrapper={({children, onClick, ...rest}) => {
43+
const clickHandler = (event: React.MouseEvent) => {
44+
onClick && onClick(event)
45+
props.onClick && props.onClick(event as React.MouseEvent<HTMLAnchorElement>)
46+
}
47+
return (
48+
<Link
49+
as={Component}
50+
sx={merge(styles, sx as SxProp)}
51+
{...rest}
52+
{...props}
53+
onClick={clickHandler}
54+
ref={forwardedRef}
55+
>
56+
{children}
57+
</Link>
58+
)
59+
}}
4760
>
4861
{props.children}
4962
</Item>

src/ActionList/shared.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ export type ActionListItemProps = {
4242
/**
4343
* Private API for use internally only. Used by LinkItem to wrap contents in an anchor
4444
*/
45-
_PrivateItemWrapper?: React.FC<React.PropsWithChildren<unknown>>
45+
_PrivateItemWrapper?: React.FC<React.PropsWithChildren<MenuItemProps>>
4646
} & SxProp
4747

48+
type MenuItemProps = {
49+
onClick?: (event: React.MouseEvent) => void
50+
onKeyPress?: (event: React.KeyboardEvent) => void
51+
'aria-disabled'?: boolean
52+
tabIndex?: number
53+
'aria-labelledby'?: string
54+
'aria-describedby'?: string
55+
role?: string
56+
}
57+
4858
export type ItemContext = Pick<ActionListItemProps, 'variant' | 'disabled'> & {
4959
inlineDescriptionId: string
5060
blockDescriptionId: string

src/__tests__/ActionList.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ describe('ActionList', () => {
155155
expect(option).toBeInTheDocument()
156156
})
157157

158+
it('should call onClick for a link item', async () => {
159+
const onClick = jest.fn()
160+
const component = HTMLRender(
161+
<ActionList role="listbox">
162+
<ActionList.LinkItem role="link" onClick={onClick}>
163+
Primer React
164+
</ActionList.LinkItem>
165+
</ActionList>,
166+
)
167+
const link = await waitFor(() => component.getByRole('link'))
168+
fireEvent.click(link)
169+
expect(onClick).toHaveBeenCalled()
170+
})
171+
158172
checkStoriesForAxeViolations('', '../ActionList/ActionList.features')
159173
checkStoriesForAxeViolations('', '../ActionList/ActionList.examples')
160174
})

0 commit comments

Comments
 (0)