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 Button props to ensure they correctly type check #2445

Merged
merged 8 commits into from
Oct 25, 2022
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/many-windows-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

fixes types for Button
6 changes: 3 additions & 3 deletions src/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Divider} from './ActionList/Divider'
import {ActionListContainerContext} from './ActionList/ActionListContainerContext'
import {Button, ButtonProps} from './Button'
import {MandateProps} from './utils/types'
import {SxProp, merge} from './sx'
import {merge, BetterSystemStyleObject} from './sx'

export type MenuContextProps = Pick<
AnchoredOverlayProps,
Expand Down Expand Up @@ -83,12 +83,12 @@ const MenuButton = React.forwardRef<AnchoredOverlayProps['anchorRef'], ButtonPro
<Button
type="button"
trailingIcon={TriangleDownIcon}
sx={merge(
sx={merge<BetterSystemStyleObject>(
{
// override the margin on caret for optical alignment
'[data-component=trailingIcon]': {marginX: -1}
},
sxProp as SxProp
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
sxProp
)}
{...props}
/>
Expand Down
20 changes: 11 additions & 9 deletions src/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React, {forwardRef} from 'react'
import {ButtonProps} from './types'
import {ButtonBase} from './ButtonBase'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'

const ButtonComponent = forwardRef<HTMLButtonElement, ButtonProps>(
({children, ...props}, forwardedRef): JSX.Element => {
return (
<ButtonBase ref={forwardedRef} as="button" {...props}>
{children}
</ButtonBase>
)
}
)
const ButtonComponent: PolymorphicForwardRefComponent<'button', ButtonProps> = forwardRef<
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
HTMLButtonElement,
ButtonProps
>(({children, ...props}, forwardedRef): JSX.Element => {
return (
<ButtonBase ref={forwardedRef} as="button" {...props}>
{children}
</ButtonBase>
)
})

ButtonComponent.displayName = 'Button'

Expand Down
4 changes: 2 additions & 2 deletions src/Button/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {HTMLAttributes, ComponentPropsWithRef} from 'react'
import React, {ComponentPropsWithRef} from 'react'
import styled from 'styled-components'
import {IconProps} from '@primer/octicons-react'
import sx, {SxProp} from '../sx'
Expand Down Expand Up @@ -34,7 +34,7 @@ export type ButtonBaseProps = {
*/
disabled?: boolean
} & SxProp &
HTMLAttributes<HTMLButtonElement> &
React.ButtonHTMLAttributes<HTMLButtonElement> &
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
StyledButtonProps

export type ButtonProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/SegmentedControl/SegmentedControlButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {HTMLAttributes} from 'react'
import React, {ButtonHTMLAttributes} from 'react'
import {IconProps} from '@primer/octicons-react'
import styled from 'styled-components'
import Box from '../Box'
Expand All @@ -15,7 +15,7 @@ export type SegmentedControlButtonProps = {
/** The leading icon comes before item label */
leadingIcon?: React.FunctionComponent<React.PropsWithChildren<IconProps>>
} & SxProp &
HTMLAttributes<HTMLButtonElement | HTMLLIElement>
ButtonHTMLAttributes<HTMLButtonElement | HTMLLIElement>

const SegmentedControlButtonStyled = styled.button`
${sx};
Expand Down
4 changes: 2 additions & 2 deletions src/SegmentedControl/SegmentedControlIconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {HTMLAttributes} from 'react'
import React, {ButtonHTMLAttributes} from 'react'
import {IconProps} from '@primer/octicons-react'
import styled from 'styled-components'
import sx, {merge, SxProp} from '../sx'
Expand All @@ -15,7 +15,7 @@ export type SegmentedControlIconButtonProps = {
/** Whether the segment is selected. This is used for uncontrolled SegmentedControls to pick one SegmentedControlButton that is selected on the initial render. */
defaultSelected?: boolean
} & SxProp &
HTMLAttributes<HTMLButtonElement | HTMLLIElement>
ButtonHTMLAttributes<HTMLButtonElement | HTMLLIElement>

const SegmentedControlIconButtonStyled = styled.button`
${sx};
Expand Down
2 changes: 1 addition & 1 deletion src/_TextInputInnerAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Button, IconButton, ButtonProps} from './Button'
import Tooltip from './Tooltip'
import {BetterSystemStyleObject, merge, SxProp} from './sx'

type TextInputActionProps = Omit<React.HTMLProps<HTMLButtonElement>, 'aria-label' | 'size'> & {
type TextInputActionProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'aria-label' | 'size'> & {
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
/** @deprecated Text input action buttons should only use icon buttons */
children?: React.ReactNode
/** Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies. */
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/Button.types.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import {Button} from '../Button'

export function shouldAcceptOnlyAChildProp() {
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
return <Button>child</Button>
}

export function shouldNotAcceptOutlandishProps() {
// @ts-expect-error system props should not be accepted
return <Button anOutlandshPropThatShouldNotBeAllowedOnA={'Button'} />
}
4 changes: 2 additions & 2 deletions src/drafts/MarkdownEditor/_ViewSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export const ViewSwitch = ({selectedView, onViewSelect, onLoadPreview, disabled}
const {label, icon, ...sharedProps} =
selectedView === 'preview'
? {
variant: 'invisible',
variant: 'invisible' as const,
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
sx: {color: 'fg.default', px: 2},
onClick: () => onViewSelect?.('edit'),
icon: PencilIcon,
label: 'Edit'
}
: {
variant: 'invisible',
variant: 'invisible' as const,
sx: {color: 'fg.default', px: 2},
onClick: () => {
onLoadPreview()
Expand Down
2 changes: 1 addition & 1 deletion src/stories/ActionMenu/fixtures.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function ExternalAnchor(): JSX.Element {
<Button
ref={anchorRef}
onClick={() => setOpen(!open)}
onKeyDown={(event: KeyboardEvent) => {
onKeyDown={event => {
mattcosta7 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: This should happen from AnchoredOverlay?
if (['ArrowDown', 'ArrowUp'].includes(event.code)) setOpen(true)
}}
Expand Down