Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/element-plus-solidjs. element-plus-solidjs.vercel.app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import { 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<ComponentProps<'button'>, '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 ( <button {...restProps} class={classList()} type={props.nativeType} aria-disabled={props.disabled || props.loading} disabled={props.disabled || props.loading} > <span classList={{ [ns().em('text', 'expand')]: shouldAddSpace() }}> {props.children} </span> </button> )}