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

fix: limit styled button type #693

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions .storybook/src/v4.0.0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ After
<Button component={NextLink} href="...">Link</Button>
```

### ButtonのGeneric propsについて

[Styled Componentの制限](https://github.com/styled-components/styled-components/issues/1803)により`styled(Button)`で拡張する場合propsがうまく推論されない場合があります。
Charcoal v4.1.0から過剰なpropsを検出する処理を追加したが、`component` propと合わせて使う時の型を配慮する必要があります。

サンプル

```tsx
// allow component
const StyledButton = styled(Button)`` as typeof Button
<StyledButton component={Link} to="#" />

// preserve component type
const StyledButtonA = styled(Button<'a'>)``
<StyledButtonA component="a" href="#" />
```

その他のケースは [styledButtonTypeTest.d.tsx](https://github.com/pixiv/charcoal/blob/main/packages/react/src/components/Button/styledButtonTypeTest.d.tsx) を参考してください。

## Checkbox

- `Checkbox`の`input`要素を`children`がない場合に label で囲わないようにしました。
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ const Button = forwardRef(function Button<T extends React.ElementType>(
ref={ref}
/>
)
}) as <T extends React.ElementType = 'button'>(p: ButtonProps<T>) => JSX.Element
}) as <T extends React.ElementType = 'button'>(
p: 'button' extends T
? ButtonProps<'button'>
: ButtonProps<T> & {
component: T // required
}
) => JSX.Element
export default Button
81 changes: 81 additions & 0 deletions packages/react/src/components/Button/styledButtonTypeTest.d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// This file is for type testing only

// only use the type
import type { default as ButtonType } from './index'
import type styledType from 'styled-components'
Copy link
Contributor Author

@yue4u yue4u Jan 27, 2025

Choose a reason for hiding this comment

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

直接な依存ではないが動くはず。styled側に入れたかったが、ビルドに影響してできませんでした。pnpm移して諸々依存問題が解消したらまた移動しましょう


declare const Button: typeof ButtonType
declare const styled: typeof styledType

const Custom = ({ custom }: { custom: string }) => <>{custom}</>
const CustomGeneric = <C extends string>({ custom }: { custom: C }) => (
<>{custom}</>
)

const StyledButton = styled(Button)``
const StyledButtonAsButton = styled(Button<'button'>)``
const StyledButtonA = styled(Button<'a'>)``
const StyledButtonCustom = styled(Button<typeof Custom>)``
const StyledButtonCustomAsButton = styled(
Button<typeof Custom>
)`` as typeof Button
const StyledButtonCustomGeneric = styled(Button<typeof CustomGeneric>)``
const StyledButtonCustomGenericFoo = styled(
Button<typeof CustomGeneric<'foo'>>
)``

// for type test only
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function Tests() {
return (
<>
{/* OK */}

<StyledButton />
<StyledButton type="button" disabled />
<StyledButtonAsButton type="button" disabled />
<StyledButtonA component="a" href="#" />
<StyledButtonCustom component={Custom} custom="" />
<StyledButtonCustomAsButton<'a'> component="a" href="#" />
<StyledButtonCustomAsButton<typeof CustomGeneric<'bar'>>
component={CustomGeneric}
custom="bar"
/>
<StyledButtonCustomAsButton component="a" href="#" />
<StyledButtonCustomGeneric component={CustomGeneric} custom="" />
<StyledButtonCustomGenericFoo component={CustomGeneric} custom="foo" />
<StyledButtonCustomGenericFoo
component={CustomGeneric<'foo'>}
custom="foo"
/>

{/* NG */}

{/* @ts-expect-error Property 'href' does not exist on type */}
<StyledButton href="" />
{/* @ts-expect-error Property 'href' does not exist on type */}
<StyledButtonAsButton href="" />
{/* @ts-expect-error Property 'component' is missing */}
<StyledButtonA href="#" />
{/* @ts-expect-error Property 'disabled' does not exist on type */}
<StyledButtonA disabled />
{/* @ts-expect-error Type '"button"' is not assignable to type '"a"' */}
<StyledButtonA component="button" href="" />
{/* @ts-expect-error Property 'component' is missing */}
<StyledButtonCustom custom="" />
{/* @ts-expect-error Type 'string' is not assignable */}
<StyledButtonCustom component="a" custom="" />
{/* @ts-expect-error Property 'custom' does not exist on type */}
<StyledButtonCustomAsButton custom="" />
{/* @ts-expect-error Type 'href' is not assignable */}
<StyledButtonCustomAsButton<'button'> href="#" />
<StyledButtonCustomGenericFoo
/* @ts-expect-error Type '"foo"' is not assignable to type '"bar"' */
component={CustomGeneric<'bar'>}
custom="foo"
/>
{/* @ts-expect-error '""' is not assignable to type '"foo"' */}
<StyledButtonCustomGenericFoo custom="" />
</>
)
}
Loading