@vtaits/react-hook-form-schema v1.1.0
@vtaits/react-hook-form-schema
Integration of react-hook-form and @vtaits/form-schema.
Installation
yarn add react-hook-form @vtaits/react-hook-form-schemaor
npm install react-hook-form @vtaits/react-hook-form-schema --saveExamples
Usage
import { useFormSchema } from '@vtaits/react-hook-form-schema';
const {
handleSubmit,
renderField,
...restResult
} = useFormSchema({
defaultValues,
getFieldSchema,
getFieldType,
mapErrors,
names,
...rest
});
const onSubmit = async (values, rawValues) => {
};
<form onSubmit={handleSubmit(onSubmit)}>
{renderField("field1")}
{renderField("field2")}
<hr />
{renderField("field3", "payload")}
</form>It similar to react-hook-form but there is some differences:
getFieldSchema,getFieldType,namesare required. They are described in @vtaits/form-schema;onSubmitreceives serialized values as first argument;mapErrors(not required) can map submission errors to set them with @vtaits/form-schema.
renderField
A function for rendering field by name according to its schema. Arguments:
name- required, string, name of field for render;payload- not required, payload prop of nested field;parents- not required, stack of parent fields for child field.
Built-in field types
Dynamic
Field depends from other fields. Example:
import { dynamic } from '@vtaits/react-hook-form-schema/fields/dynamic';
const schema = {
type: 'dynamic',
getSchema: ({
otherField,
}, phase) => ({
type: 'string',
label: 'String field',
required: Boolean(otherField),
}),
};
const getFieldType = (fieldSchema) => {
if (fieldSchema.type === 'dynamic') {
return dynamic;
}
// ...
}Parameters:
getSchema- required, function, should return schema for render ornull. Arguments:values- object of values of form, depends from 2nd argument;phase- current phase ('parse','serialize','render'). If phase is'parse', 1st argument is initial values before parsing, otherwise it is current values of form.getFieldSchema- see @vtaits/form-schema;getFieldType- see @vtaits/form-schema;parents- stack of parent fields above current field with runtime values;
getSchemaAsync- not required, function. Can be used for asynchronous parsing. Similar togetSchemabut should returnPromisewith result schema;onShow- not required, callback that called when field has shown. Arguments:formResult- result of calling ofreact-hook-form;name- name of field;schema- result schema of subfield;getFieldSchema- currentgetFieldSchema;getFieldType- globalgetFieldType;parents- stack of parent fields above current field with runtime values;
onHide- not required, callback that called when field has hidden. Arguments:formResult- result of calling ofreact-hook-form;name- name of field;getFieldSchema- currentgetFieldSchema;getFieldType- globalgetFieldType;parents- stack of parent fields above current field with runtime values.
Set
The group of fields. It's comfortable when the dynamic field must render several fields. Example:
import { dynamic } from '@vtaits/react-hook-form-schema/fields/dynamic';
import { set } from '@vtaits/react-hook-form-schema/fields/set';
const schema = {
type: 'dynamic',
getSchema: ({
responsibleType,
}) => {
if (responsibleType !== 'human') {
return null;
}
return {
type: 'set',
schemas: {
firstName: {
type: 'string',
label: 'First name',
},
lastName: {
type: 'string',
label: 'Last name',
},
},
};
},
};
const getFieldType = (fieldSchema) => {
if (fieldSchema.type === 'dynamic') {
return dynamic;
}
if (fieldSchema.type === 'set') {
return set;
}
// ...
}Parameters:
schemas- required, object whose keys are field names and values are their schemas;render- not required, render function. . Arguments:renderField- analogous torenderFieldresult ofuseFormSchema;names- fields names (keys of schemas);
Utils
renderBySchema
Similar to renderField of the result of useFormSchema, but have more arguments:
formResult- result ofreact-hook-form;getFieldSchema- see above;getFieldType- see above;getValues- all values at the level of field;name- the name of the fieldpayload- see aboveparents- see above.
import { renderBySchema } from '@vtaits/react-final-form-schema';9 months ago
9 months ago
9 months ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago