-
Notifications
You must be signed in to change notification settings - Fork 616
ActionList.Item accepts a polymorphic 'as' prop #1463
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
Changes from all commits
15e3129
0614f42
b1abc4d
2d97288
19c3010
c55c126
7cfe964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/components': minor | ||
--- | ||
|
||
`ActionList.item` accepts an `as` prop, allowing it to be a link, or (in combination with the renderItem prop) a Next.js or React Router link |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React from 'react' | ||
import React, {Key} from 'react' | ||
import type {AriaRole} from '../utils/types' | ||
import {Group, GroupProps} from './Group' | ||
import {Item, ItemProps} from './Item' | ||
|
@@ -8,7 +8,9 @@ import {get} from '../constants' | |
import {SystemCssProperties} from '@styled-system/css' | ||
import {hasActiveDescendantAttribute} from '../behaviors/focusZone' | ||
|
||
export type ItemInput = ItemProps | (Partial<ItemProps> & {renderItem: typeof Item}) | ||
type RenderItemFn = (props: ItemProps) => React.ReactElement | ||
|
||
export type ItemInput = ItemProps | ((Partial<ItemProps> & {renderItem: RenderItemFn}) & {key?: Key}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, what's the reason for including There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing as the new props for item — added because tsc caught issues that it wasn't catching before. in this case, it's something we allow in an ItemInput but remove before the ItemInput becomes ItemProps. |
||
|
||
/** | ||
* Contract for props passed to the `List` component. | ||
|
@@ -34,7 +36,7 @@ export interface ListPropsBase { | |
* without a `Group`-level or `Item`-level custom `Item` renderer will be | ||
* rendered using this function component. | ||
*/ | ||
renderItem?: typeof Item | ||
renderItem?: RenderItemFn | ||
|
||
/** | ||
* A `List`-level custom `Group` renderer. Every `Group` within this `List` | ||
|
@@ -72,14 +74,14 @@ export interface GroupedListProps extends ListPropsBase { | |
*/ | ||
groupMetadata: (( | ||
| Omit<GroupProps, 'items'> | ||
| Omit<Partial<GroupProps> & {renderItem?: typeof Item; renderGroup?: typeof Group}, 'items'> | ||
| Omit<Partial<GroupProps> & {renderItem?: RenderItemFn; renderGroup?: typeof Group}, 'items'> | ||
) & {groupId: string})[] | ||
|
||
/** | ||
* A collection of `Item` props, plus associated group identifiers | ||
* and `Item`-level custom `Item` renderers. | ||
*/ | ||
items: ((ItemProps | (Partial<ItemProps> & {renderItem: typeof Item})) & {groupId: string})[] | ||
items: ((ItemProps | (Partial<ItemProps> & {renderItem: RenderItemFn})) & {groupId: string})[] | ||
} | ||
|
||
/** | ||
|
@@ -162,7 +164,7 @@ export const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwarde | |
const renderItem = (itemProps: ItemInput, item: ItemInput, itemIndex: number) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
const ItemComponent = ('renderItem' in itemProps && itemProps.renderItem) || props.renderItem || Item | ||
const key = itemProps.key ?? itemProps.id?.toString() ?? itemIndex.toString() | ||
const key = ('key' in itemProps ? itemProps.key : undefined) ?? itemProps.id?.toString() ?? itemIndex.toString() | ||
return ( | ||
<ItemComponent | ||
showDivider={props.showItemDividers} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why we needed to add these props?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three fields were because tsc told me to :-) ... but also, they all make sense and I wonder if they only worked before because of gaps in the type coverage