Wrap the given Form component and provide Props related to the form.
import React from 'react';
import { dripForm } from 'react-drip-form';
const Form = (props) => {
/* form component */
};
export default dripForm(/* options */)(Form);
All options are Optional.
Object
{}
You can specify the default Props For the wrapped component.
Object
{}
You can specify a validation rule.
dripForm({
validations: {
username: {
required: true,
max: 255,
},
userEmail: {
required: true,
email: true,
},
},
})(Form);
Please refer to drip-form-validator
Built-in Rules for specifiable rules.
Object
{}
You can specify Normalizers.
dripForm({
normalizers: {
age: {
min: 18,
},
},
})(Form);
Please refer to drip-form-validator
Built-in Normalizers for specifiable rules.
Object
{}
Specify the error message corresponding to the validation rule.
dripForm({
messages: {
username: {
required: 'The Username is required...',
max: 'The Username is too long...',
},
},
})(Form);
Object
{}
Specify the label of the field. It is mainly used in error messages.
dripForm({
labels: {
username: 'User Name',
age: 'Age',
},
})(Form);
boolean
true
Specify whether validation should be executed depending on field change.
This is useful when real-time verification is unnecessary.
dripForm({
validateOnChange: false,
})(Form);
boolean
true
Specify whether to perform validation according to the blur of the field.
dripForm({
validateOnBlur: false,
})(Form);
boolean
true
Specify whether normalize should be executed depending on field change.
dripForm({
normalizeOnChange: false,
})(Form);
boolean
true
Specify whether to perform normalize according to the blur of the field.
dripForm({
normalizeOnBlur: false,
})(Form);
List of Props provided for wrapped components.
All other than Props below are passed directly to Props.
Object
It is a value with name
of each field as a key.
The signature is as follows.
{
username: 'value',
nest: {
deep: 'value',
},
}
Object
It is a errors with name
of each field as a key.
The signature is as follows.
{
username: ['message1', 'message2'],
'nest.deep': ['message1'],
}
An object with properties with the following form state.
boolean
If all validation passes, it is true
.
boolean
The opposite of meta.valid
.
boolean
If you touch any field it is true
.
onBlur
is used to determine if you are touching.
boolean
The opposite of meta.touched
.
boolean
true
if the value of any field has been changed.
boolean
The opposite of meta.dirty
.
boolean
true
if asynchronous validation is in progress.
An object for performing field operations with the following properties.
For nested objects, please use dot notation. In addition, use wildcards when including arrays.
// nest
fields.get('nest.deep')
// nest + array
fields.get('nest.*.deep.key')
Retrieves the value of the specified field.
Sets the value in the specified field.
Remove the value of the specified field.
Adds a value to the specified field.
It works only when the value is an array.
Removes the last element from the value of the specified field and returns that element.
It works only when the value is an array.
Removes the first element from the value of the specified field and returns that element.
It works only when the value is an array.
Adds one or more elements to the beginning of the value of the specified field.
It works only when the value is an array.
Swaps the value of the specified field.
It works only when the value is an array.
Move the value of the specified field.
It works only when the value is an array.
Call Iteratee on all elements for the value of the specified field and return a new array from the result.
iteratee
has the following signature:
(path: string, index: string | number, value: any) => any
Iteratee is executed for each element of the value of the specified field.
iteratee
has the following signature:
(path: string, index: string | number, value: any) => void
Returns as boolean
whether the specified field is Valid.
Returns as boolean
whether the specified field is validating.
An object for event handling of forms with the following properties.
Handle submit
. If Event is passed as an argument, preventDefault()
is executed automatically.
It internally calls the clear()
method.
const Form = ({ handlers }) => (
<form onSubmit={handlers.onSubmit}>
{/* More components */}
<button onClick={handlers.onSubmit}>Submit</button>
</form>
);
Handle clear
. If Event is passed as an argument, preventDefault()
is executed automatically.
It internally calls the clear()
method.
const Form = ({ handlers }) => (
<form>
{/* More components */}
<button onClick={handlers.onClear}>Clear values</button>
</form>
);
Handle reset
. If Event is passed as an argument, preventDefault()
is executed automatically.
It internally calls the reset()
method.
const Form = ({ handlers }) => (
<form>
{/* More components */}
<button onClick={handlers.onReset}>Reset values</button>
</form>
);
Components after wrapping can specify the following Props.
All props are Optional.
Object
Specify an object whose key is the name
attribute of the field.
Values set in values
can also be used as initial values.
Also, if it is detected that componentWillReceiveProps()
is not equivalent, overwrite the current form value.
<EnhancedForm
values={{
username: 'value',
age: 18,
}}
/>
onInitialize()
is called in the constructor. A DripFom instance is passed as an argument.
It is mainly used for obtaining DripForm instance.
<EnhancedForm
onInitialize={form => this.form = form}
/>
onClear() is called if
clear()or
handlers.onClear()` is called.
<EnhancedForm
onClear={() => console.log('Clear values')}
/>
onValidSubmit()
will be called if all validation passes after submit()
or handlers.onSubmit()
has been called.
<EnhancedForm
onValidSubmit={values => console.log('Valid Submit!', values)}
/>
onInvalidSubmit()
will be called if at least one validation failed after submit()
or handlers.onSubmit()
was called.
<EnhancedForm
onInvalidSubmit={values => console.log('Invalid values...', values)}
/>
List of APIs of DripForm instance.
You can get a DripForm instance using onInitialize()
as follows.
<EnhancedForm
onInitialize={form => this.form = form}
/>
It is a value with name
of each field as a key.
Specify an object whose key is the name
attribute of the field.
If silent
istrue
, onChange()
will not be called.
Get the initial value of the form.
It is a errors with name
of each field as a key.
Specify an error with the field name
attribute as a key.
form.setErrors({
// string
username: 'Invalid username...',
// or string array
age: ['Too long...', 'Other error message'],
});
Gets a DripFormField instance with the specified name
attribute.
Perform validations.
Performs asynchronous validation of fields with the specified name
attribute.
Perform normalizing.
Trigger Submit manually.
Clear all values of the form.
All values of the form are reset to their initial values.