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;