2023-09-03 02:18:05 +08:00
|
|
|
import { JSX, ParentComponent, Show, splitProps } from 'solid-js'
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
|
2023-09-23 14:30:16 +08:00
|
|
|
interface ButtonBaseProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
|
|
loading?: boolean
|
|
|
|
disabled?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ButtonWithIconProps extends ButtonBaseProps {
|
|
|
|
icon: JSX.Element
|
|
|
|
children?: JSX.Element
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ButtonWithoutIconProps extends ButtonBaseProps {
|
|
|
|
icon?: JSX.Element
|
|
|
|
children: JSX.Element
|
|
|
|
}
|
|
|
|
|
2023-09-03 02:18:05 +08:00
|
|
|
export const Button: ParentComponent<
|
2023-09-23 14:30:16 +08:00
|
|
|
ButtonWithIconProps | ButtonWithoutIconProps
|
2023-09-03 02:18:05 +08:00
|
|
|
> = (props) => {
|
2023-09-23 14:30:16 +08:00
|
|
|
const [local, others] = splitProps(props, ['class', 'loading', 'icon'])
|
2023-09-03 02:18:05 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
2023-09-22 16:05:36 +08:00
|
|
|
class={twMerge(
|
|
|
|
'btn flex items-center',
|
|
|
|
local.loading ? 'btn-disabled' : local.class,
|
|
|
|
)}
|
2023-09-03 02:18:05 +08:00
|
|
|
{...others}
|
|
|
|
>
|
|
|
|
<Show when={local.loading}>
|
2023-09-22 16:05:36 +08:00
|
|
|
<div class="loading loading-spinner" />
|
2023-09-03 02:18:05 +08:00
|
|
|
</Show>
|
|
|
|
|
2023-09-23 14:30:16 +08:00
|
|
|
<span class="truncate" classList={{ 'flex-1': !local.icon }}>
|
2023-09-22 16:05:36 +08:00
|
|
|
{props.icon || props.children}
|
|
|
|
</span>
|
2023-09-03 02:18:05 +08:00
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|