import { h, type FunctionComponent } from 'preact' import { useCallback, useEffect, useRef, useState } from 'preact/hooks' import cn from 'classnames' import mergeStyles from '../utils/merge_styles.ts' type Styles = { base?: string touched?: string element?: string label?: string icon?: string } & Record & Record & Record type Options = { styles: Styles } type Props = { autoFocus?: boolean className?: string color?: C defaultValue?: string design?: D disabled?: boolean icon: ANY label: string name: string noEmpty?: boolean onChange?: ANY options: string[] | number[] | { label: string; value: string | number }[] placeholder?: string required?: boolean size: S } // designs / color / size export default function selectFactory({ styles, }: Options) { if (Array.isArray(styles)) { styles = mergeStyles(styles) } const Select: FunctionComponent> = ({ autoFocus, className, color, defaultValue, design, disabled, icon, name, label, noEmpty, onChange, placeholder, required, size, options, }) => { const [touched, setTouched] = useState(false) const selectRef = useRef(null) const onBlur = useCallback(() => setTouched(true), []) useEffect(() => { const form = selectRef.current!.form const resetTouched = setTouched.bind(null, false) form!.addEventListener('reset', resetTouched) return () => form!.removeEventListener('reset', resetTouched) }, []) useEffect(() => { if (autoFocus) selectRef.current!.focus() }, [autoFocus]) return (
{icon &&
} {/** *
) } return Select }