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

Update types for IconButton, ActionMenu.Button, TabNav.Link and internal consumers #2677

Merged
merged 18 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions src/Button/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import styled from 'styled-components'
import {IconProps} from '@primer/octicons-react'
import sx, {SxProp} from '../sx'
import getGlobalFocusStyles from '../_getGlobalFocusStyles'

Expand Down Expand Up @@ -37,16 +36,16 @@ export type ButtonProps = {
/**
* The leading icon comes before button content
*/
leadingIcon?: React.FunctionComponent<React.PropsWithChildren<IconProps>> | null | undefined
leadingIcon?: React.ComponentType | null | undefined
/**
* The trailing icon comes after button content
*/
trailingIcon?: React.FunctionComponent<React.PropsWithChildren<IconProps>> | null | undefined
trailingIcon?: React.ComponentType | null | undefined
children: React.ReactNode
} & ButtonBaseProps

export type IconButtonProps = ButtonA11yProps & {
icon: React.FunctionComponent<React.PropsWithChildren<IconProps>>
icon: React.ComponentType
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

broadens these to they work with arbitrary components, and not only primer octicons

} & Omit<ButtonBaseProps, 'aria-label' | 'aria-labelledby'>
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved

// adopted from React.AnchorHTMLAttributes
Expand Down
10 changes: 5 additions & 5 deletions src/TabNav.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ForwardRefComponent as PolymorphicForwardRefComponent} from './utils/polymorphic'
import classnames from 'classnames'
import {To} from 'history'
import React, {useRef, useState} from 'react'
Expand Down Expand Up @@ -67,18 +68,18 @@ function TabNav({children, 'aria-label': ariaLabel, ...rest}: TabNavProps) {
)
}

type StyledTabNavLinkProps = {
export type TabNavLinkProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement> & {
to?: To
selected?: boolean
} & SxProp

const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
const TabNavLink = styled.a.attrs<TabNavLinkProps>(props => ({
activeClassName: typeof props.to === 'string' ? 'selected' : '',
className: classnames(ITEM_CLASS, props.selected && SELECTED_CLASS, props.className),
role: 'tab',
'aria-selected': !!props.selected,
tabIndex: -1,
}))<StyledTabNavLinkProps>`
}))<TabNavLinkProps>`
padding: 8px 12px;
font-size: ${get('fontSizes.1')};
line-height: 20px;
Expand All @@ -105,9 +106,8 @@ const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
}

${sx};
`
` as PolymorphicForwardRefComponent<'a', TabNavLinkProps>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is used polymorphically, but the styled-components as types conflict some with the polymorphic helper types - overriding the styled-components version gives better typing, and avoids those issues


TabNavLink.displayName = 'TabNav.Link'

export type TabNavLinkProps = ComponentProps<typeof TabNavLink>
export default Object.assign(TabNav, {Link: TabNavLink})
9 changes: 9 additions & 0 deletions src/__tests__/TabNav.types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import TabNav from '../TabNav'
import {Button} from '../Button'

export function shouldAcceptCallWithNoProps() {
return (
Expand All @@ -20,3 +21,11 @@ export function shouldNotAcceptSystemProps() {
</>
)
}

export function shouldAcceptButtonAsProps() {
return <TabNav.Link as={Button} />
}

export function shouldAcceptTabNavLinkprops() {
return <TabNav.Link to="to something" selected as={Button} />
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

validates that we can accept specific props, and as

}