Skip to content

Commit

Permalink
fix: removed polymorphic typing from types.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
benjitrosch committed Apr 5, 2022
1 parent 61ddca7 commit 0000986
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Button/Button.stories.tsx
Expand Up @@ -113,7 +113,7 @@ export const AsHref: Story<ButtonProps> = (args) => {
>
I'm a {`<button>`}
</Button>
<Button href="https://google.com" target="_blank">
<Button href="https://google.com">
I'm an {`<a>`}
</Button>
</div>
Expand Down
58 changes: 23 additions & 35 deletions src/Button/Button.tsx
Expand Up @@ -9,35 +9,30 @@ import {
ComponentSize,
} from '../types'

type ButtonBaseProps = IComponentBaseProps & {
shape?: ComponentShape
size?: ComponentSize
variant?: 'outline' | 'link'
color?: ComponentColor
fullWidth?: boolean
responsive?: boolean
animation?: boolean
loading?: boolean
active?: boolean
startIcon?: ReactNode
endIcon?: ReactNode
}

type ButtonElementProps = ButtonBaseProps & React.ButtonHTMLAttributes<HTMLButtonElement> & {
as?: 'button'
}

type AnchorElementProps = ButtonBaseProps & React.AnchorHTMLAttributes<HTMLAnchorElement> & {
as: 'link'
href: string
}

export type ButtonProps = ButtonElementProps | AnchorElementProps
export type ButtonProps = Omit<
React.ButtonHTMLAttributes<HTMLButtonElement>,
'color'
> &
IComponentBaseProps & {
href?: string
shape?: ComponentShape
size?: ComponentSize
variant?: 'outline' | 'link'
color?: ComponentColor
fullWidth?: boolean
responsive?: boolean
animation?: boolean
loading?: boolean
active?: boolean
startIcon?: ReactNode
endIcon?: ReactNode
}

const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
children,
href,
shape,
size,
variant,
Expand Down Expand Up @@ -76,19 +71,12 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
})
)

if ('href' in props)
{
props.as = 'link'
}

if (props.as === 'link') {
if (href) {
return (
<a
{...props}
ref={ref as React.ForwardedRef<HTMLAnchorElement>}
className={classes}
style={style}
href={props.href}
href={href}
>
{startIcon && startIcon}
{children}
Expand All @@ -99,7 +87,7 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
return (
<button
{...props}
ref={ref as React.ForwardedRef<HTMLButtonElement>}
ref={ref}
data-theme={dataTheme}
className={classes}
style={style}
Expand Down
9 changes: 0 additions & 9 deletions src/types.ts
@@ -1,5 +1,3 @@
import React from 'react'

import {
componentStatuses,
componentColors,
Expand All @@ -25,10 +23,3 @@ export type ComponentSize = typeof componentSizes[number]
export type ComponentStatus = typeof componentStatuses[number]
export type ComponentBrandColors = typeof brandColors[number]
export type ComponentBgColors = typeof bgColors[number]

// Allow anchor elements to have disabled attribute
declare module 'react' {
interface AnchorHTMLAttributes<T> extends HTMLAttributes<T> {
disabled?: boolean
}
}

0 comments on commit 0000986

Please sign in to comment.