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: Resolve warnings against missing array keys in Button #1736

Merged
merged 9 commits into from
Nov 4, 2024
51 changes: 14 additions & 37 deletions packages/react/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import clsx from 'clsx'
import { forwardRef } from 'react'
import type { ButtonHTMLAttributes, ForwardedRef, PropsWithChildren, ReactNode } from 'react'
import type { ButtonHTMLAttributes, ForwardedRef, PropsWithChildren } from 'react'
import { Icon } from '../Icon'
import type { IconProps } from '../Icon'

Expand Down Expand Up @@ -42,42 +42,19 @@ export const Button = forwardRef(
(
{ children, className, disabled, icon, iconBefore, iconOnly, type, variant = 'primary', ...restProps }: ButtonProps,
ref: ForwardedRef<HTMLButtonElement>,
) => {
const content = (): ReactNode => {
switch (true) {
case !icon:
return children
case iconBefore:
return [<Icon svg={icon} size="level-5" />, children]
case iconOnly:
return [
<Icon key={1} svg={icon} size="level-5" square={true} />,
<span className="ams-visually-hidden" key={2}>
{children}
</span>,
]
default:
return [children, <Icon svg={icon} size="level-5" />]
}
}

return (
<button
{...restProps}
className={clsx(
'ams-button',
`ams-button--${variant}`,
icon && iconOnly && !iconBefore && `ams-button--icon-only`,
className,
)}
disabled={disabled}
ref={ref}
type={type || 'button'}
>
{content()}
</button>
)
},
) => (
<button
{...restProps}
className={clsx('ams-button', `ams-button--${variant}`, icon && iconOnly && `ams-button--icon-only`, className)}
disabled={disabled}
ref={ref}
type={type || 'button'}
>
{icon && (iconBefore || iconOnly) && <Icon svg={icon} size="level-5" square={iconOnly} />}
{icon && iconOnly ? <span className="ams-visually-hidden">{children}</span> : children}
{icon && !iconBefore && !iconOnly && <Icon svg={icon} size="level-5" />}
</button>
),
)

Button.displayName = 'Button'