Skip to content

Commit

Permalink
Allow custom children in ActionItem (#1196)
Browse files Browse the repository at this point in the history
Co-authored-by: Clay Miller <clay@smockle.com>
Co-authored-by: Trevor Gau <trevorsg@gmail.com>
  • Loading branch information
3 people authored May 3, 2021
1 parent 5296405 commit d29741c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-plums-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Allow custom `children` in `ActionItem`. `text` and `description` can still be provided as a shortcut, but `children` is now available if you need more control over the rending of the item, without sacrificing benefits from `Item` by using `renderItem`.
14 changes: 9 additions & 5 deletions src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ItemProps extends React.ComponentPropsWithoutRef<'div'>, SxProp
/**
* Primary text which names an `Item`.
*/
text: string
text?: string

/**
* Secondary text which provides additional information about an `Item`.
Expand Down Expand Up @@ -179,6 +179,7 @@ export function Item(itemProps: Partial<ItemProps> & {item?: ItemInput}): JSX.El
disabled,
onAction,
onKeyPress,
children,
onClick,
...props
} = itemProps
Expand Down Expand Up @@ -224,10 +225,13 @@ export function Item(itemProps: Partial<ItemProps> & {item?: ItemInput}): JSX.El
<LeadingVisual />
</LeadingVisualContainer>
)}
<StyledTextContainer descriptionVariant={descriptionVariant}>
<div>{text}</div>
{description && <DescriptionContainer>{description}</DescriptionContainer>}
</StyledTextContainer>
{children}
{(text || description) && (
<StyledTextContainer descriptionVariant={descriptionVariant}>
{text && <div>{text}</div>}
{description && <DescriptionContainer>{description}</DescriptionContainer>}
</StyledTextContainer>
)}
{(TrailingIcon || trailingText) && (
<TrailingVisualContainer variant={variant} disabled={disabled}>
{trailingText && <div>{trailingText}</div>}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('ActionMenu', () => {
})
portalRoot = menu.baseElement.querySelector('#__primerPortalRoot__')
expect(portalRoot).toBeTruthy()
const menuItem = await menu.queryByText(items[0].text)
const menuItem = await menu.queryByText(items[0].text!)
act(() => {
fireEvent.click(menuItem as Element)
})
Expand Down
30 changes: 28 additions & 2 deletions src/stories/ActionList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
NoteIcon,
ProjectIcon,
FilterIcon,
GearIcon
GearIcon,
ArrowRightIcon,
ArrowLeftIcon
} from '@primer/octicons-react'
import {Meta} from '@storybook/react'
import React from 'react'
import styled from 'styled-components'
import {ThemeProvider} from '..'
import {Label, ThemeProvider} from '..'
import {ActionList as _ActionList} from '../ActionList'
import {Header} from '../ActionList/Header'
import BaseStyles from '../BaseStyles'
Expand Down Expand Up @@ -238,3 +240,27 @@ export function HeaderStory(): JSX.Element {
)
}
HeaderStory.storyName = 'Header'

export function CustomItemChildren(): JSX.Element {
return (
<>
<h1>Custom Item Children</h1>
<ErsatzOverlay>
<ActionList
items={[
{
leadingVisual: ArrowRightIcon,
children: (
<Label outline borderColor="border.success">
Choose this one
</Label>
),
trailingIcon: ArrowLeftIcon
}
]}
/>
</ErsatzOverlay>
</>
)
}
CustomItemChildren.storyName = 'Custom Item Children'
8 changes: 4 additions & 4 deletions src/stories/ActionMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ErsatzOverlay = styled.div`
export function ActionsStory(): JSX.Element {
const [option, setOption] = useState('Select an option')
const onAction = (itemProps: ItemProps) => {
setOption(itemProps.text)
setOption(itemProps.text ?? '')
}
return (
<>
Expand Down Expand Up @@ -85,7 +85,7 @@ ActionsStory.storyName = 'Actions'
export function SimpleListStory(): JSX.Element {
const [option, setOption] = useState('Select an option')
const onAction = (itemProps: ItemProps) => {
setOption(itemProps.text)
setOption(itemProps.text || '')
}
return (
<>
Expand Down Expand Up @@ -116,7 +116,7 @@ SimpleListStory.storyName = 'Simple List'
export function ComplexListStory(): JSX.Element {
const [option, setOption] = useState('Select an option')
const onAction = (itemProps: ItemProps) => {
setOption(itemProps.text)
setOption(itemProps.text || '')
}
return (
<>
Expand Down Expand Up @@ -177,7 +177,7 @@ export function CustomTrigger(): JSX.Element {
const customAnchor = (props: LinkProps) => <Link {...props} sx={{cursor: 'pointer'}} />
const [option, setOption] = useState('Select an option')
const onAction = useCallback((itemProps: ItemProps) => {
setOption(itemProps.text)
setOption(itemProps.text || '')
}, [])
return (
<>
Expand Down

1 comment on commit d29741c

@vercel
Copy link

@vercel vercel bot commented on d29741c May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.