Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom children in ActionItem #1196

Merged
merged 8 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
smockle marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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}
smockle marked this conversation as resolved.
Show resolved Hide resolved
{(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 || '')
smockle marked this conversation as resolved.
Show resolved Hide resolved
dgreif marked this conversation as resolved.
Show resolved Hide resolved
}
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