remix-demo
react router (remix) demo
git clone https://9o.is/git/remix-demo.git
Input.tsx
(1260B)
1 import { useField } from "remix-validated-form";
2 import InputError from "./InputError";
3
4 const styles = {
5 label: {
6 base: "block mb-2 font-medium",
7 error: "text-red-700",
8 success: "text-gray-900",
9 },
10 input: {
11 base: "block w-full rounded-lg border p-2.5 border-gray-300",
12 error:
13 "bg-red-50 border-red-500 text-red-900 placeholder-red-700 focus:outline-red-500",
14 success: "bg-gray-50 text-gray-900 focus:outline-blue-500",
15 },
16 };
17
18 export function getInputStyles(
19 name: "label" | "input",
20 error: string | undefined
21 ): string {
22 return `${styles[name].base} ${
23 error ? styles[name].error : styles[name].success
24 }`;
25 }
26
27 export default function Input(props: any) {
28 const { label, name } = props;
29 const attributes = { ...props, label: undefined };
30 const { error, getInputProps } = useField(name);
31
32 return (
33 <div>
34 <label htmlFor={name} className={getInputStyles("label", error)}>
35 {label}
36 </label>
37 <input
38 {...getInputProps({ id: name })}
39 {...attributes}
40 className={getInputStyles("input", error)}
41 aria-invalid={Boolean(error)}
42 aria-describedby={`${name}-error`}
43 />
44 <InputError error={error} id={`${name}-error`} />
45 </div>
46 );
47 }