import { Component, createEffect, createSignal, createUniqueId, onMount, Show, } from "solid-js"; import formStyles from "./form.module.css"; export type TextFieldProps = { label?: string; helperText?: string; type?: "text" | "password"; onChange?: (value: string) => void; onInput?: (value: string) => void; inputId?: string; error?: boolean; required?: boolean; name?: string; }; const TextField: Component = (props) => { let input: HTMLInputElement; let field: HTMLDivElement; const [hasContent, setHasContent] = createSignal(false); const altInputId = createUniqueId(); createEffect(() => { if (hasContent()) { field.classList.add("float-label"); } else { field.classList.remove("float-label"); } }); onMount(() => { setHasContent(input.value.length > 0); }); const onInputChange = (e: { currentTarget: HTMLInputElement }) => { const value = (e.currentTarget as HTMLInputElement).value; setHasContent(value.length > 0); props.onInput?.(value); }; const inputId = () => props.inputId ?? altInputId; const fieldClass = () => { const cls = [formStyles.textfield]; if (typeof props.helperText !== "undefined") { cls.push(formStyles.withHelperText); } if (props.error) { cls.push(formStyles.error); } return cls.join(" "); }; return (
props.onChange?.(e.currentTarget.value)} placeholder="" required={props.required} name={props.name} /> {props.helperText}
); }; export default TextField;