From 4b394f8143069091127765f7f3f2cb5a365e352d Mon Sep 17 00:00:00 2001 From: Vixalie Date: Fri, 22 Aug 2025 10:11:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(components):=20=E6=B7=BB=E5=8A=A0=20Input?= =?UTF-8?q?=20=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了一个通用的 Input 组件,支持多种变体和自定义样式 - 组件属性包括 name、variant、left、right、value、defaultValue 等 - 支持 onInput 事件处理 - 优化了组件的默认值设置和响应式行为 --- src/components/Input.tsx | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/components/Input.tsx diff --git a/src/components/Input.tsx b/src/components/Input.tsx new file mode 100644 index 0000000..e86496f --- /dev/null +++ b/src/components/Input.tsx @@ -0,0 +1,73 @@ +import cx from 'clsx'; +import { isNotNil } from 'es-toolkit'; +import { Component, createEffect, createSignal, JSX, mergeProps, onMount, Show } from 'solid-js'; + +interface InputProps extends JSX.HTMLAttributes { + name?: string; + variant?: 'normal' | 'underlined' | 'immersive'; + left?: JSX.Element; + right?: JSX.Element; + value?: string; + defaultValue?: string; + placeholder?: string; + disabled?: boolean; + onInput?: (value: string | null) => void; + wrapperClass?: JSX.HTMLAttributes['class']; + inputClass?: JSX.HTMLAttributes['class']; +} + +const Input: Component = (props) => { + const mProps = mergeProps( + { + variant: 'normal', + disabled: false, + placeholder: '', + }, + props, + ); + const [internalValue, setInternalValue] = createSignal(''); + onMount(() => { + if (isNotNil(mProps.defaultValue)) { + setInternalValue(mProps.defaultValue); + } + }); + createEffect(() => { + if (isNotNil(mProps.value) && mProps.value !== internalValue()) { + setInternalValue(mProps.value); + } + }); + + const handleInput: JSX.EventHandler = (evt) => { + const value = evt.currentTarget.value; + setInternalValue(value); + mProps.onInput?.(value); + }; + + return ( +
+ {mProps.left} + + {mProps.right} +
+ ); +}; + +export default Input;