FormField
Animated inputs with validation, like shadcn's Form but with no form library. The border animates on focus, invalid fields shake and slide in a message, passwords get an eye toggle, and the submit button shows loading and success states.
Preview
Preview coming soon
Try it live: run the CLI command below in an Expo app, or open the Code tab.
Installation
npx @codewithrabby/ui@latest add form-fieldUsage
import {
Form, FormField, FormSubmit, useForm,
required, email, minLength, matches,
} from "@/components/uir/form-field";
import { Mail } from "lucide-react-native";
const form = useForm({
defaultValues: { email: "", password: "", repeat: "" },
rules: {
email: [required(), email()],
password: [required(), minLength(8)],
repeat: [matches("password", "Passwords don't match.")],
},
// mode: "onBlur", // validate when leaving a field (default: on submit)
});
<Form form={form}>
<FormField name="email" label="Email" icon={Mail} keyboardType="email-address" />
<FormField name="password" label="Password" secure description="At least 8 characters." />
<FormField name="repeat" label="Repeat password" secure />
<FormSubmit
successLabel="Account created"
onSubmit={async (values) => {
const res = await signUp(values);
if (!res.ok) {
form.setError("email", "That email is already taken.");
throw new Error(); // back to idle
}
}}
>
Create Account
</FormSubmit>
</Form>
// custom rule: return a message to fail
const noSpaces = (v: string) => (v.includes(" ") ? "No spaces allowed." : undefined);
// schema libraries (zod, yup, valibot…): return { field: message }
useForm({ defaultValues, validate: (values) => fromZod(schema.safeParse(values)) });Props
| Prop | Type | Default |
|---|---|---|
useForm({ defaultValues })Starting values. Every field is a string. | T | — |
useForm({ rules })Checked in order; the first message wins. Built in: required, email, minLength, maxLength, pattern, matches. | { [field]: Rule[] } | — |
useForm({ validate })Whole-form check for schema libraries. Merged with rules. | (values) => FormErrors | — |
useForm({ mode })When fields first validate. After a submit they always re-validate as you type. | "onSubmit" | "onBlur" | "onChange" | "onSubmit" |
formvalues, errors, status, isValid, setValue, setError, clearErrors, reset, trigger, handleSubmit. | FormApi | — |
<FormField name>Which value this input edits. | string | — |
<FormField label>Label above the input. Gets a * when the rules include required(). | string | — |
<FormField description>Hint under the input, replaced by the error when there is one. | string | — |
<FormField icon>Icon at the start of the input. Turns accent / red with the state. | LucideIcon | — |
<FormField secure>Password field with a show / hide toggle. | boolean | — |
<FormField accentColor>Border and icon colour when focused. | string | "#111111" |
<FormField errorColor>Border, icon and message colour when invalid. | string | "#ef4444" |
<FormField …TextInputProps>placeholder, keyboardType, autoComplete, etc. are passed through. | TextInputProps | — |
<FormSubmit onSubmit>Runs only when the form is valid. Return a promise to show the spinner; throw to go back to idle. | (values) => void | Promise | — |
<FormSubmit successLabel>Shown with a check after onSubmit resolves. Leave out to go straight back. | string | — |
<FormSubmit color>Button colour (successColor for the done state). | string | "#111111" |
useFormField(name){ value, error, onChange, onBlur } to wire your own control into the form. | hook | — |