11import React , { isValidElement , useEffect , useMemo } from "react" ;
22import styles from "./styles/index.module.scss" ;
33import classNames from "classnames" ;
4- import { getDisplayName } from "@react-ck/react-utils" ;
4+ import {
5+ getDisplayName ,
6+ PolymorphicComponent ,
7+ type ConsumerPolymorphicProps ,
8+ type HTMLTag ,
9+ } from "@react-ck/react-utils" ;
510
611/**
712 * Props for the Button component
813 */
9- export interface ButtonProps extends React . ButtonHTMLAttributes < HTMLButtonElement > {
14+ export interface ButtonProps < T extends HTMLTag >
15+ extends React . ButtonHTMLAttributes < HTMLButtonElement > ,
16+ ConsumerPolymorphicProps < T > {
1017 /** Defines the color scheme of the button */
1118 skin ?: "primary" | "secondary" | "ghost" | "negative" ;
1219 /** Defines the scale of the button */
@@ -26,14 +33,15 @@ export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
2633 * @returns a React element
2734 */
2835
29- export const Button = ( {
36+ export const Button = < T extends HTMLTag > ( {
37+ as,
3038 skin = "primary" ,
3139 size = "m" ,
3240 icon,
3341 children,
3442 className,
3543 ...otherProps
36- } : Readonly < ButtonProps > ) : React . ReactElement => {
44+ } : Readonly < ButtonProps < T > > ) : React . ReactElement => {
3745 const isIconOnly = useMemo (
3846 ( ) => icon !== undefined && React . Children . count ( children ) === 0 ,
3947 [ children , icon ] ,
@@ -49,22 +57,23 @@ export const Button = ({
4957 } , [ children ] ) ;
5058
5159 return (
52- < button
53- className = { classNames (
54- styles . root ,
55- styles [ skin ] ,
56- styles [ `size-${ size } ` ] ,
57- {
58- [ `${ styles [ "icon-only" ] } ` ] : isIconOnly ,
59- } ,
60- className ,
61- ) }
62- { ...otherProps } >
60+ < PolymorphicComponent
61+ as = { as }
62+ fallback = { [ "button" , otherProps ] }
63+ commonProps = { {
64+ className : classNames (
65+ styles . root ,
66+ styles [ skin ] ,
67+ styles [ `size-${ size } ` ] ,
68+ {
69+ [ `${ styles [ "icon-only" ] } ` ] : isIconOnly ,
70+ } ,
71+ className ,
72+ ) ,
73+ } } >
6374 { icon && ! isIconOnly ? < span className = { styles . icon } > { icon } </ span > : null }
64-
6575 { children }
66-
6776 { isIconOnly ? icon : null }
68- </ button >
77+ </ PolymorphicComponent >
6978 ) ;
7079} ;
0 commit comments