npm install react-peasy-form
The base form component and context provider for all enclosed form fields.
<Form
defaults={{
title: 'hello',
name: { first: 'John', last: 'Doe' }
}}
onSubmit={values => console.log(values)}
>
<div>
<h2>Inputs</h2>
<Input
name="title"
transform={v => v.toUpperCase()}
type="text"
/>
<Input
name="name.first"
type="text"
validate={v => {
if (v === 'bob') {
const err = (
<span className="error">
No Bobs allowed!
</span>
)
return [err]
}
return true
}}
/>
<Input name="name.last" type="text" />
</div>
<div>
<h2>Values</h2>
<ul>
<li><Value name="title" /></li>
<li>
<Value
name={['name.first', 'name.last']}
render={([firstName, lastName]) => {
return (
<span>
<strong>Name:</strong>
{firstName} {lastName}
</span>
)
}}
/>
</li>
</ul>
</div>
<div>
<h2>Errors</h2>
<Error name="name.first" />
</div>
<Status render={status => {
const disabled = status === FormStatus.VALIDATING ||
status == FormStatus.INVALID
return (
<button disabled={disabled} type="submit">Submit</button>
)
}} />
</Form>
Default form values in the form of key/value pairs where the key is a form field name, and the value is the form field value. Values in nested objects can be accessed through dot notation.
The default setting describing when to trigger field validation. This can be overridden on a per-field basis
Applies a field validation to a field value. This can be used to support various third party validation libraries.
Executed when the form is submitted. onSubmit
gets passed an object mapping field names to transformed field values. Nested values are supported through dot notation.
Executed before a form's onSubmit validation occurs.
Executed when a form's onSubmit validation fails
<Input />
<Select />
<Textarea />
Each field supports all the props of the field it is shadowing, plus:
The key associated with a form field's value. Supports nested objects through dot notation.
Used to transform a fields before validation occurs. The transformed value is returned by onSubmit
A function used to validate a field's value. The function signature when using the default validation strategy is (value: any) => true | Array<string | Error | JSX.Element>
Override the form's default validateOn
Outputs the transformed value of a form field.
The field name or names to render
enum FormStatus {
PRISTINE,
DIRTY,
INVALID,
}
Outputs the status of the form or a specific field.
If defined, only this field's status will be passed to render
Outputs any errors associated with a field.
The desired field's name
- Add live examples