Set initial values with async request
import { useEffect } from 'react';
import { useForm } from '@mantine/form';
import { TextInput, Checkbox } from '@mantine/core';
interface FormValues {
email: string;
terms: boolean;
}
function loadInitialValues(): Promise<FormValues> {
return new Promise((resolve) => {
setTimeout(() => resolve({ email: 'test@email', terms: true }), 2000);
});
}
function Demo() {
const form = useForm<FormValues>({
mode: 'uncontrolled',
initialValues: { email: '', terms: false },
});
useEffect(() => {
loadInitialValues().then((values) => {
form.setValues(values);
form.resetDirty(values);
});
}, []);
return (
<form onSubmit={form.onSubmit(console.log)}>
<TextInput
label="Email"
placeholder="Email"
key={form.key('email')}
{...form.getInputProps('email')}
/>
<Checkbox
mt="sm"
label="I accept terms and conditions"
key={form.key('terms')}
{...form.getInputProps('terms', { type: 'checkbox' })}
/>
</form>
);
}
Save form values to local storage
import { useEffect } from 'react';
import { useForm } from '@mantine/form';
import { TextInput, Box } from '@mantine/core';
function Demo() {
const form = useForm({
mode: 'uncontrolled',
initialValues: { name: '', occupation: '' },
onValuesChange: (values) => {
window.localStorage.setItem('user-form', JSON.stringify(values));
},
});
useEffect(() => {
const storedValue = window.localStorage.getItem('user-form');
if (storedValue) {
try {
form.setValues(JSON.parse(window.localStorage.getItem('user-form')!));
} catch (e) {
console.log('Failed to parse stored value');
}
}
}, []);
return (
<Box maw={340} mx="auto">
<TextInput
label="Name"
placeholder="Name"
key={form.key('name')}
{...form.getInputProps('name')}
/>
<TextInput
mt="md"
label="Occupation"
placeholder="Occupation"
key={form.key('occupation')}
{...form.getInputProps('occupation')}
/>
</Box>
);
}
List items reordering
import { Group, TextInput, Button, Center } from '@mantine/core';
import { useForm } from '@mantine/form';
import { randomId } from '@mantine/hooks';
import { IconGripVertical } from '@tabler/icons-react';
import { DndContext, closestCenter, PointerSensor, useSensor, useSensors, DragEndEvent } from '@dnd-kit/core';
import { SortableContext, verticalListSortingStrategy, useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
function SortableItem({ id, index, form }) {
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id });
const style = {
transform: CSS.Transform.toString(transform),
transition,
};
return (
<Group ref={setNodeRef} mt="xs" style={style} {...attributes}>
<Center {...listeners}>
<IconGripVertical size={18} />
</Center>
<TextInput
placeholder="John Doe"
key={form.key(`employees.${index}.name`)}
{...form.getInputProps(`employees.${index}.name`)}
/>
<TextInput
placeholder="example@mail.com"
key={form.key(`employees.${index}.email`)}
{...form.getInputProps(`employees.${index}.email`)}
/>
</Group>
);
}
function Demo() {
const form = useForm({
mode: 'uncontrolled',
initialValues: {
employees: [
{ name: 'John Doe', email: 'john@mantine.dev', key: randomId() },
{ name: 'Bill Love', email: 'bill@mantine.dev', key: randomId() },
{ name: 'Nancy Eagle', email: 'nanacy@mantine.dev', key: randomId() },
{ name: 'Lim Notch', email: 'lim@mantine.dev', key: randomId() },
{ name: 'Susan Seven', email: 'susan@mantine.dev', key: randomId() },
],
},
});
const sensors = useSensors(useSensor(PointerSensor));
const items = form.getValues().employees.map((item) => item.key);
const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
if (over && active.id !== over.id) {
const employees = form.getValues().employees;
const oldIndex = employees.findIndex((e) => e.key === active.id);
const newIndex = employees.findIndex((e) => e.key === over.id);
form.setFieldValue(
'employees',
arrayMove(employees, oldIndex, newIndex)
);
}
};
return (
<div>
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
<SortableContext items={items} strategy={verticalListSortingStrategy}>
{items.map((id, index) => (
<SortableItem key={id} id={id} index={index} form={form} />
))}
</SortableContext>
</DndContext>
<Group justify="center" mt="md">
<Button onClick={() => form.insertListItem('employees', { name: '', email: '', key: randomId() })}>
Add employee
</Button>
</Group>
</div>
);
}
Form with multiple steps
import { useState } from 'react';
import { Stepper, Button, Group, TextInput, PasswordInput, Code } from '@mantine/core';
import { useForm } from '@mantine/form';
function Demo() {
const [active, setActive] = useState(0);
const form = useForm({
mode: 'uncontrolled',
initialValues: {
username: '',
password: '',
name: '',
email: '',
website: '',
github: '',
},
validate: (values) => {
if (active === 0) {
return {
username:
values.username.trim().length < 6
? 'Username must include at least 6 characters'
: null,
password:
values.password.length < 6 ? 'Password must include at least 6 characters' : null,
};
}
if (active === 1) {
return {
name: values.name.trim().length < 2 ? 'Name must include at least 2 characters' : null,
email: /^\S+@\S+$/.test(values.email) ? null : 'Invalid email',
};
}
return {};
},
});
const nextStep = () =>
setActive((current) => {
if (form.validate().hasErrors) {
return current;
}
return current < 3 ? current + 1 : current;
});
const prevStep = () => setActive((current) => (current > 0 ? current - 1 : current));
return (
<>
<Stepper active={active}>
<Stepper.Step label="First step" description="Profile settings">
<TextInput
label="Username"
placeholder="Username"
key={form.key('username')}
{...form.getInputProps('username')}
/>
<PasswordInput
mt="md"
label="Password"
placeholder="Password"
key={form.key('password')}
{...form.getInputProps('password')}
/>
</Stepper.Step>
<Stepper.Step label="Second step" description="Personal information">
<TextInput
label="Name"
placeholder="Name"
key={form.key('name')}
{...form.getInputProps('name')}
/>
<TextInput
mt="md"
label="Email"
placeholder="Email"
key={form.key('email')}
{...form.getInputProps('email')}
/>
</Stepper.Step>
<Stepper.Step label="Final step" description="Social media">
<TextInput
label="Website"
placeholder="Website"
key={form.key('website')}
{...form.getInputProps('website')}
/>
<TextInput
mt="md"
label="GitHub"
placeholder="GitHub"
key={form.key('github')}
{...form.getInputProps('github')}
/>
</Stepper.Step>
<Stepper.Completed>
Completed! Form values:
<Code block mt="xl">
{JSON.stringify(form.getValues(), null, 2)}
</Code>
</Stepper.Completed>
</Stepper>
<Group justify="flex-end" mt="xl">
{active !== 0 && (
<Button variant="default" onClick={prevStep}>
Back
</Button>
)}
{active !== 3 && <Button onClick={nextStep}>Next step</Button>}
</Group>
</>
);
}