When to use Form
Murray inputs can be used alone or among the <Form/> component to manage complex forms.
Complex form with data validation
On the web, when you need more than simple inputs, we encourage you to use the Form component.
It handles validation schemas (through yup) and manages data for you.
It relies on Formik under the hood.
It provides all form components with data and validation bound to the current Form.
For example, here is how to create a simple form with two required fields and a submit button.
import { Form } from "murray";
import Yup from "yup";
return (
<Form
onSubmit={(values) => {
// Do something with the values
}}
initialValues={{
firstname: "John",
lastname: "Doe",
}}
validationSchema={Yup.object().shape({
firstname: Yup.string().required(),
lastname: Yup.string().required(),
})}
>
{/* This is important to note that we don't use the component from Murray but directly from the Form component */}
<Form.TextInput name="firstname" />
<Form.TextInput name="lastname" />
<Form.Submit>Submit</Form.Submit>
</Form>
);
Without data validation
If you are managing UX, or want to do something simpler, you can simply use the components provided by Murray. They don't provide data validation but you can use them to build your own form like a search feature for example.
import { TextInput } from "murray";
function MySearchFieldComponent() {
const [searchTerm, setSearchTerm] = useState("");
return (
<TextInput
placeholder="Search anything"
name="search"
prefix={
<Box sx={{ marginRight: "$px8" }}>
<Icon visual="MagnifyingGlass" color="$grey500" />
</Box>
}
value={searchTerm}
onChange={(value) => {
setSearchTerm(value);
}}
/>
);
}