diff --git a/packages/smarthr-ui/src/components/Button/AnchorButton.tsx b/packages/smarthr-ui/src/components/Button/AnchorButton.tsx index 876da01193..51c6bf87ed 100644 --- a/packages/smarthr-ui/src/components/Button/AnchorButton.tsx +++ b/packages/smarthr-ui/src/components/Button/AnchorButton.tsx @@ -1,18 +1,37 @@ -import React, { AnchorHTMLAttributes, forwardRef, useMemo } from 'react' +import React, { + ComponentPropsWithoutRef, + ElementType, + FC, + PropsWithoutRef, + ReactElement, + Ref, + forwardRef, + useMemo, +} from 'react' import { tv } from 'tailwind-variants' +import { ElementRef, ElementRefProps } from '../../types' + import { ButtonInner } from './ButtonInner' import { ButtonWrapper } from './ButtonWrapper' import { BaseProps } from './types' -type ElementProps = Omit, keyof BaseProps> +type ElementProps = Omit< + ComponentPropsWithoutRef, + keyof Props & ElementRefProps +> + +type Props = BaseProps & { + /** next/linkなどのカスタムコンポーネントを指定します。指定がない場合はデフォルトで `a` タグが使用されます。 */ + elementAs?: T +} const anchorButton = tv({ base: 'smarthr-ui-AnchorButton', }) -export const AnchorButton = forwardRef( - ( +const AnchorButton = forwardRef( + ( { size = 'default', square = false, @@ -22,12 +41,13 @@ export const AnchorButton = forwardRef { + }: PropsWithoutRef> & ElementProps, + ref: Ref>, + ): ReactElement => { const styles = useMemo(() => anchorButton({ className }), [className]) const actualRel = useMemo( () => (rel === undefined && target === '_blank' ? 'noopener noreferrer' : rel), @@ -46,6 +66,7 @@ export const AnchorButton = forwardRef {children} @@ -54,5 +75,17 @@ export const AnchorButton = forwardRef( + props: Props & ElementProps & ElementRefProps, +) => ReturnType + +const ForwardedAnchorButton = AnchorButton as unknown as AnchorButtonType & { + displayName: string +} + // BottomFixedArea での判定に用いるために displayName を明示的に設定する -AnchorButton.displayName = 'AnchorButton' +ForwardedAnchorButton.displayName = 'AnchorButton' + +export { ForwardedAnchorButton as AnchorButton } diff --git a/packages/smarthr-ui/src/components/Button/ButtonWrapper.tsx b/packages/smarthr-ui/src/components/Button/ButtonWrapper.tsx index 519188ef70..54bdfd6637 100644 --- a/packages/smarthr-ui/src/components/Button/ButtonWrapper.tsx +++ b/packages/smarthr-ui/src/components/Button/ButtonWrapper.tsx @@ -1,6 +1,7 @@ import React, { AnchorHTMLAttributes, ButtonHTMLAttributes, + ElementType, ForwardedRef, ReactNode, useMemo, @@ -17,6 +18,7 @@ type BaseProps = { $loading?: boolean className: string children: ReactNode + elementAs?: ElementType } type ButtonProps = BaseProps & { @@ -56,9 +58,10 @@ export function ButtonWrapper({ }, [$loading, className, size, square, variant, wide]) if (props.isAnchor) { - const { anchorRef, isAnchor: _, ...others } = props + const { anchorRef, elementAs, isAnchor: _, ...others } = props + const Component = elementAs || 'a' // eslint-disable-next-line smarthr/a11y-anchor-has-href-attribute, jsx-a11y/anchor-has-content - return + return } else { const { buttonRef, disabled, onClick, ...others } = props return ( diff --git a/packages/smarthr-ui/src/components/TextLink/TextLink.tsx b/packages/smarthr-ui/src/components/TextLink/TextLink.tsx index 60a716db72..8f715b106b 100644 --- a/packages/smarthr-ui/src/components/TextLink/TextLink.tsx +++ b/packages/smarthr-ui/src/components/TextLink/TextLink.tsx @@ -1,5 +1,4 @@ import React, { - ComponentPropsWithRef, ComponentPropsWithoutRef, ElementType, FC, @@ -11,12 +10,9 @@ import React, { } from 'react' import { tv } from 'tailwind-variants' +import { ElementRef, ElementRefProps } from '../../types' import { FaExternalLinkAltIcon } from '../Icon' -type ElementRef = ComponentPropsWithRef['ref'] - -type ElementRefProps = { ref?: ElementRef } - type ElementProps = Omit< ComponentPropsWithoutRef, (keyof Props & ElementRefProps) | 'color' diff --git a/packages/smarthr-ui/src/types/ComponentTypes.ts b/packages/smarthr-ui/src/types/ComponentTypes.ts new file mode 100644 index 0000000000..53f97f681d --- /dev/null +++ b/packages/smarthr-ui/src/types/ComponentTypes.ts @@ -0,0 +1,5 @@ +import { ComponentPropsWithRef, ElementType } from 'react' + +export type ElementRef = ComponentPropsWithRef['ref'] + +export type ElementRefProps = { ref?: ElementRef } diff --git a/packages/smarthr-ui/src/types/index.ts b/packages/smarthr-ui/src/types/index.ts index b5915dc5f9..34b138e3c0 100644 --- a/packages/smarthr-ui/src/types/index.ts +++ b/packages/smarthr-ui/src/types/index.ts @@ -2,3 +2,4 @@ export type { Gap, SeparateGap } from './Gap' export type { DecoratorsType, DecoratorType } from './Decorator' export type { ResponseMessageType } from './ResponseMessage' export type { LocaleMap } from './Locale' +export type { ElementRef, ElementRefProps } from './ComponentTypes'