Default Image

Form

High performance Form component with data scope management. Including data collection, verification, and styles.

import { Form } from '@redshank/native';

When To Use

  • When you need to create an instance or collect information.
  • When you need to validate fields in certain rules.

Examples

Default

Default form element.

Completed example

Complete form example.

Manipulation

Manipulation form example.

APIs

Form

Api props extends from rc-field-form

AttributeTypeAccepted valuesDescriptionDefault
componentstring Component falseComponentCustomize Form render componentView
fieldsFieldData[]FieldDataControl Form fields status. Only use when in Redux-
formFormInstanceFormInstanceSet form instance created by useForm-
initialValuesobject{ [key]: value }Initial value of Form-
preservebooleantrue falsePreserve value when field removedfalse
validateMessagesValidateMessageFormInstanceSet validate message template-
onFieldsChangefunction(changedFields, allFields) => voidTrigger when any value of Field changed-
onFinishfunction(values) => voidTrigger when form submit and success-
onFinishFailedfunction({ values, errorFields, outOfDate }) => voidTrigger when form submit and failed-
onValuesChangefunction(changedValues, values) => voidTrigger when any value of Field changed-
borderBottomnumbertrue falseSet the bottom margin into fields15

Form.Item

Api props extends from Field

AttributeTypeAccepted valuesDescriptionDefault
dependenciesNamePath[]NamePathWill re-render if dependencies changed-
getValueFromEventfunction(..args: any[]) => anySpecify how to get value from event-
getValuePropsfunction(value) => anyCustomize additional props with value. This prop will disable valuePropName-
initialValueanyanyField initial value-
isSubmitbooleanbooleanPass onPress works like submit to child-
labelstring ReactNodeText ComponentField label-
nameanyNamePathField name path-
normalizefunction(value, prevValue, prevValues) => anyNormalize value before update-
preservebooleantrue falsePreserve value when field removed-
requiredbooleanbooleanField required-
rulesRule[]ruleValidate rules-
shouldUpdateboolean functionboolean (prevValues, nextValues) => booleanCheck if Field should update-
triggerstringTextCollect value update by event triggeronChange
validateTriggerstring string[]Text Text ArrayConfig trigger point with rule validateonChange
valuePropNamestringTextConfig value mapping prop with elementvalue
styleobjectStyleProp<ViewStyle>Style for the wrapper elementvalue

Form.useForm

Form component default create an form instance by Form.useForm. But you can create it and pass to Form also. This allow you to call some function on the form instance. Api props extends from useForm.

const Demo = () => {
  const [form, onSubmit] = Form.useForm();
  return <Form form={form} />;
};

FormInstance

Api props extends from useForm.

AttributeTypeAccepted valuesDescriptionDefault
getFieldValue(name: NamePath) => anyNamePathGet field value by name path-
getFieldsValue(nameList: NamePath[]) => anyNamePathGet list of field values by name path list-
getFieldError(name: NamePath) => string[]NamePathGet field errors by name path-
getFieldsError(nameList: NamePath) => FieldError[]NamePathGet list of field errors by name path list-
isFieldsTouched(nameList?: NamePath[], allTouched?: boolean) => booleanNamePathCheck if list of fields are touched-
isFieldTouched(name: NamePath) => booleanNamePathCheck if a field is touched-
isFieldValidating(name: NamePath) => booleanNamePathCheck if a field is validating-
resetFields(fields?: NamePath[]) => voidNamePathReset fields status-
setFields(fields: FieldData[]) => voidFieldDataSet fields status-
setFieldsValuefunction(values) => voidSet fields value-
submitfunction() => voidTrigger form submit-
validateFields(nameList?: NamePath[], options?: ValidateOptions) => PromiseNamePathTrigger fields to validate-

NamePath

Api props extends from NamePath.

type NamePath = string | number | (string | number)[]

FieldData

Api props extends from FieldData.

PropType
touchedboolean
validatingboolean
errorsstring[]
namestring number string[] number[]
valueany

Rule

Api props extends from Rule.

PropType
enumany[]
lennumber
maxnumber
messagestring
minnumber
patternRegExp
requiredboolean
transform(value) => any
typestring
validator(rule, value, callback: (error?: string) => void, form) => Promise or void
whitespaceboolean
validateTriggerstring string[]

Community:

We're excited to see the community adopt @redshank/native, raise issues, and provide feedback. Whether it's a feature request, bug report, or a project to showcase, please get involved!

DiscordGithub

Contributing:

PR's on @redshank/native are always welcome, please see our contribution guidelines to learn how you can contribute to this project.

Copyright © 2023 @redshank.