@7span/vue-form 中文文档教程

发布于 3年前 浏览 25 更新于 3年前

vue-form

从 JSON 生成动态表单。

??? 这是正在进行的工作。 不建议在生产中使用。

Installation

npm install @7span/vue-form

✨ Usage

<v-form ref="vForm" v-model="values" :config="form.config" :fields="form.fields"></v-form>

⚙️ Props

1. config

Config 定义了如何处理表单。 配置选项如下

OptionDefaultPossible ValuesWhat is does
defaults{}Default configuration for the fields.
endpointnullValid URLAn endpoint to process data for CRUD

2. fields

一个包含表单字段配置对象的数组。 对象的 key 将作为字段的 name 添加。 每个对象都可以有以下选项。

OptionDefaultPossible ValuesWhat is does
interfaceinputinput,choice,select,file,textareaRenders input elements. choice will render either radio or checkbox. Know more.
typetexttext,email,radio,checkbox etc.Based on input, the type can be diffrent. I.e. the input can have email,text,url etc.
labelCamel cased value of keyStringThe label of the field
placeholdernullStringThe placeholder. Will be added where supported.
valuenullString, ArrayThe default value to set whilte rendering form. If repeater is set, provide an array of values.
hidefalseBooleanIf the field is hidden.
choices[]ArrayOptions for radio,checkbox & select. Know more.
messagesnullObjectKnow more.
design{}ObjectKnow more.
repeaternullObjectIf allow to add multiple values to field. Know more.
beforenullStringValue to prepend in Field Group
afternullStringValue to append in Field Group
fieldsnullObjectIf interface is set to group, you can provide fields under this which follows same structure as fields prop. This is recursive.

interfaces

添加到现场配置中的可能接口列表。

NameTypesDesc
inputtext,email,url,[any valid input type]
choiceradio, checkbox
selectNA
fileNA
textareaNA
groupNAYou can group fields under one with this. This will recursively create form fields.

choices

Choices 是具有以下键的对象数组。

OptionDefaultPossible ValuesWhat it does
labelnullStringThe display text.
valuenullString ,NumberThe value.

messages

该对象包含要显示的不同消息以进行验证和帮助文本。

OptionDefaultPossible ValuesWhat it does
descnullStringA helper message to show below field.
invalidnullStringA warning message to show when field validation fails.
validnullStringA success message to show when field validation is truthy.

design

OptionDefaultPossible ValuesWhat it does
col12Number from 1-12How many cols to occupy in grid.
gridNumberWhen rendering field choices in radio/checkbox, how many items should be displayed in single row

repeater

OptionDefaultPossible ValuesWhat it does
maxnullNumberMaximum values to allowed to add.
minnullNumberMinimum values required.

Events

1. change

更改事件将以下值公开为一个对象。

keyTypeWhat it contains
changedArrayAn array of changed values. It will only contain multiple values if the fields are grouped. Know more
valuesObjectAll the values in current state.
valuesObjObjectAll the values with metadata in current state.

changed

changed 对象包含以下键。

  • action : What action taken which triggered the change event. Possible values are:
    • input: When the field value is updated.
    • child-input: In a case of grouped fields, if the child value is updated, the parent action will be this.
    • repeater-add: When a new repeater field is added.
    • repeater-remove: When a new repeater field is removed.
  • field: Name of the field.
  • value: Value of the field.
  • valueObj: Value of the field with metadata.
  • index: If the field is repeater one, the index of affected repeater item.

Slots

  • form--start
  • form--end
  • field--before--{field-name}
  • field--before--{field-name}--{index}
  • field--after--{field-name}
  • field--after--{field-name}--{index}
  • field--start--{field-name}
  • field--start--{field-name}--${index}
  • field--end--{field-name}
  • field--end--{field-name}--${index}
  • repeater--add
  • repeater--remove

⚔️ Methods

1. setValue

您可以通过调用此函数直接从您的组件设置值。

this.$refs.vForm.setValue({
  field: "e_company", 
  value: "7Span Tech",
  index: 1 // Required only if you want to set value at specific index in repeater fields.
});

2. setConfig

您可以通过调用此函数从您的组件修改配置。

this.$refs.vForm.setConfig({
  field: "e_company", 
  key:"after", // The configuration key as provided in fields
  value: "#", // The value of the configuration.
  index: 1 // Required only if you want to set value at specific index in repeater fields.
});

vue-form

Generates dynamic forms from JSON.

???? This is Work in progress. Not recommended to use in production.

???? Installation

npm install @7span/vue-form

✨ Usage

<v-form ref="vForm" v-model="values" :config="form.config" :fields="form.fields"></v-form>

⚙️ Props

1. config

Config defines how to process the form. Configuration options are as below

OptionDefaultPossible ValuesWhat is does
defaults{}Default configuration for the fields.
endpointnullValid URLAn endpoint to process data for CRUD

2. fields

An array containing configuration object of the fields of form. The key of the object will be added as name of field. Each object can have below options.

OptionDefaultPossible ValuesWhat is does
interfaceinputinput,choice,select,file,textareaRenders input elements. choice will render either radio or checkbox. Know more.
typetexttext,email,radio,checkbox etc.Based on input, the type can be diffrent. I.e. the input can have email,text,url etc.
labelCamel cased value of keyStringThe label of the field
placeholdernullStringThe placeholder. Will be added where supported.
valuenullString, ArrayThe default value to set whilte rendering form. If repeater is set, provide an array of values.
hidefalseBooleanIf the field is hidden.
choices[]ArrayOptions for radio,checkbox & select. Know more.
messagesnullObjectKnow more.
design{}ObjectKnow more.
repeaternullObjectIf allow to add multiple values to field. Know more.
beforenullStringValue to prepend in Field Group
afternullStringValue to append in Field Group
fieldsnullObjectIf interface is set to group, you can provide fields under this which follows same structure as fields prop. This is recursive.

interfaces

List of possible interfaces to add into field configuration.

NameTypesDesc
inputtext,email,url,[any valid input type]
choiceradio, checkbox
selectNA
fileNA
textareaNA
groupNAYou can group fields under one with this. This will recursively create form fields.

choices

Choices is an array of objects with below keys.

OptionDefaultPossible ValuesWhat it does
labelnullStringThe display text.
valuenullString ,NumberThe value.

messages

The object contains diffrent messages to show for validation and helper text.

OptionDefaultPossible ValuesWhat it does
descnullStringA helper message to show below field.
invalidnullStringA warning message to show when field validation fails.
validnullStringA success message to show when field validation is truthy.

design

OptionDefaultPossible ValuesWhat it does
col12Number from 1-12How many cols to occupy in grid.
gridNumberWhen rendering field choices in radio/checkbox, how many items should be displayed in single row

repeater

OptionDefaultPossible ValuesWhat it does
maxnullNumberMaximum values to allowed to add.
minnullNumberMinimum values required.

???? Events

1. change

The change event exposes following values as an object.

keyTypeWhat it contains
changedArrayAn array of changed values. It will only contain multiple values if the fields are grouped. Know more
valuesObjectAll the values in current state.
valuesObjObjectAll the values with metadata in current state.

changed

The changed object contains following keys.

  • action : What action taken which triggered the change event. Possible values are:
    • input: When the field value is updated.
    • child-input: In a case of grouped fields, if the child value is updated, the parent action will be this.
    • repeater-add: When a new repeater field is added.
    • repeater-remove: When a new repeater field is removed.
  • field: Name of the field.
  • value: Value of the field.
  • valueObj: Value of the field with metadata.
  • index: If the field is repeater one, the index of affected repeater item.

???? Slots

  • form--start
  • form--end
  • field--before--{field-name}
  • field--before--{field-name}--{index}
  • field--after--{field-name}
  • field--after--{field-name}--{index}
  • field--start--{field-name}
  • field--start--{field-name}--${index}
  • field--end--{field-name}
  • field--end--{field-name}--${index}
  • repeater--add
  • repeater--remove

⚔️ Methods

1. setValue

You can set the value directly from your component by calling this function.

this.$refs.vForm.setValue({
  field: "e_company", 
  value: "7Span Tech",
  index: 1 // Required only if you want to set value at specific index in repeater fields.
});

2. setConfig

You can modify the config from your component by calling this function.

this.$refs.vForm.setConfig({
  field: "e_company", 
  key:"after", // The configuration key as provided in fields
  value: "#", // The value of the configuration.
  index: 1 // Required only if you want to set value at specific index in repeater fields.
});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文