import { createMemo, splitProps, useContext } from 'solid-js' import classnames from 'classnames' import { useGlobalConfig, useNamespace, useSize } from '../composables' import { ButtonGroupContext } from './button-group' import type { ComponentSize } from '../constants' import type { Component, ComponentProps } from 'solid-js' export type ButtonType = | '' | 'text' | 'default' | 'primary' | 'success' | 'warning' | 'info' | 'danger' export type ButtonProps = { size?: ComponentSize type?: ButtonType disabled?: boolean plain?: boolean loading?: boolean round?: boolean circle?: boolean text?: boolean link?: boolean bg?: boolean autoInsertSpace?: boolean nativeType?: ComponentProps<'button'>['type'] } export const ElButton: Component< ButtonProps & Omit, 'type'> > = (props) => { const [, restProps] = splitProps(props, [ 'size', 'type', 'disabled', 'plain', 'loading', 'round', 'circle', 'text', 'link', 'bg', 'autoInsertSpace', 'nativeType', ]) const ns = useNamespace('button') const globalConfig = useGlobalConfig() const buttonGroupContext = useContext(ButtonGroupContext) // eslint-disable-next-line unicorn/explicit-length-check const size = useSize(createMemo(() => props.size || buttonGroupContext?.size)) const type = createMemo(() => props.type || buttonGroupContext?.type || '') const autoInsertSpace = createMemo( () => props.autoInsertSpace ?? globalConfig?.().button?.autoInsertSpace ?? false ) const classList = createMemo(() => { return classnames([ ns().b(), ns().m(type()), ns().m(size()), ns().is('disabled', props.disabled), ns().is('loading', props.loading), ns().is('plain', props.plain), ns().is('round', props.round), ns().is('circle', props.circle), ns().is('text', props.text), ns().is('link', props.link), ns().is('has-bg', props.bg), ]) }) const shouldAddSpace = createMemo(() => { if (!autoInsertSpace()) return false if (typeof props.children !== 'string') return false return /^\p{Unified_Ideograph}{2}$/u.test(props.children.trim()) }) return ( ) }