-
Notifications
You must be signed in to change notification settings - Fork 141
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
feat: AnchorButtonをnext/linkなどのラップされたコンポーネントに対応 #4901
Conversation
// 型キャストなしで ForwardRefExoticComponent に合わせた型をエクスポートするための処理 | ||
type AnchorButtonType = <T extends ElementType = 'a'>( | ||
props: Props<T> & ElementProps<T> & ElementRefProps<T>, | ||
) => ReturnType<FC> | ||
|
||
const ForwardedAnchorButton = AnchorButton as unknown as AnchorButtonType & { | ||
displayName: string | ||
} | ||
|
||
ForwardedAnchorButton.displayName = 'AnchorButton' | ||
|
||
export { ForwardedAnchorButton as AnchorButton } |
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.
正しく型推論が働くようにするTextLinkと同様に型を指定したかったのですが AnchorButton
の場合displayName
が必要だったため、AnchorButtonType
にキャストし、さらに displayName
を持つようにしています。
もっといい方法あったら指摘お願いします! 🙇
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.
displayName
って普通に React コンポーネントの型に勝手に含まれると思ったんですがそんなことないんですね。
この辺見てもサッパリですが。
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/81f33824fa7f8f0d37e1c899d2be5fbe73f92dce/types/react/index.d.ts#L639-L653
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.
NamedExoticComponent
ってのがあったんですね
export const AnchorButton: AnchorButtonType & NamedExoticComponent<Props<any>>
と定義すればいけそう
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.
今のままのがまだわかりやすいか...一旦このままにします!
commit: |
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.
👍
const ForwardedAnchorButton = AnchorButton as unknown as AnchorButtonType & { | ||
displayName: string | ||
} | ||
|
||
// BottomFixedArea での判定に用いるために displayName を明示的に設定する |
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.
そもそもこれ本当にまだ必要なんだろうか…?
BottomFixedArea の実装見ても、primary/secondary 属性が Button と AnchorButton のどちらかをランタイムで判定するようなコードには見えないんですがわからない。
smarthr-ui/packages/smarthr-ui/src/components/BottomFixedArea/BottomFixedArea.tsx
Lines 34 to 37 in fb978dc
/** 表示する `Button` または `AnchorButton` (`variant="primary"` である必要がある) */ | |
primaryButton?: Primary | |
/** 表示する `Button` または `AnchorButton` (`variant="secondary"` である必要がある)*/ | |
secondaryButton?: Secondary |
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.
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.
ほえー、displayName ってそういう使い方してもいいのか…。
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.
👍 プロダクト側から動作確認できましたー。
関連URL
JIRA
概要
TextLinkと同様にnext/link react-routerなどのコンポーネントを使えるようにした。
#4867
変更内容
任意のカスタムコンポーネントを
elementAs
プロパティを通じて指定できるようにした。確認方法
sandbox環境などで
AnchorButton
を定義して確認