np-form Form

A configuration-driven form that dynamically generates the form through columns, simple to use and highly extensible.

Basic Usage

Form fields are mainly configured with label, key, type to set the form label, form field and field type. More config depends on the props of the actual type once type is set.

浏览器不支持,请手动复制 basic.en.vue

Component Configuration

Props

PropDescriptionTypeDefault
optionConfigurationFormOption-

Methods

After getting the form instance via ref, you can imperatively call the following methods. See Imperative Methods.

MethodDescriptionType
submitSubmit the form manually() => Promise<void>
resetReset the form and trigger onReset() => Promise<void>
validateFieldsValidate the given fields, supports __ prefix matching(fields: string | string[]) => Promise<void>

Types

The component exports the following type definitions:

import type { FormOption } from 'naive-plus'

FormOption

PropDescriptionTypeDefault
columnsForm fieldsArray<FormColumn>-
onSubmitSubmit event(data)=>Promise<void>-
onResetReset event(data)=>Promise<void>-
propsForm propsobject-
gridGrid layout config, see Grid Layoutobject-

FormColumn

PropDescriptionTypeDefault
labelField labelstring / VNode-
keyForm field namestring-
typeForm field typestring / (data)=>VNode-
defaultValueDefault valueany-
optionsDictionary options, e.g. checkbox, select, etc.Array<any> / ()=>Promise<Array<any>>-
propsComponent props, passed through to the actual componentobject-
formItemPropsForm item props, passed through to n-form-item-gi; supports span / offsetobject-

Global Defaults

Use NpConfigProvider to configure enterSubmitMode / grid / props and the default column of every form item. The same config in option always wins.

<np-config-provider
  :component-options="{
    Form: {
      enterSubmitMode: 'all',
      grid: { cols: 24, xGap: 12 },
      props: { labelWidth: 100 },
      column: { formItemProps: { labelWidth: 100 }, props: { size: 'small' } },
    },
  }"
>
  <np-form v-model="data" :option="option" />
</np-config-provider>