|
| 1 | +# el-form-renderer |
| 2 | + |
| 3 | +Form renderer is based on [element-ui](https://github.com/ElemeFE/element). It inherits all of the element's attribute definitions completely and extends them briefly. So users can render a complete element form by using a piece of preset data. |
| 4 | + |
| 5 | +- [Demo](https://leezng.github.io/el-form-renderer/) |
| 6 | +- [中文文档](./README.md) |
| 7 | + |
| 8 | +## Quick start |
| 9 | + |
| 10 | +```html |
| 11 | +// Step1: Make sure you have properly installed element-ui and used it correctly. |
| 12 | + |
| 13 | +// Step2: Install |
| 14 | +yarn add el-form-renderer |
| 15 | + |
| 16 | +// Step3: In the .vue file that needs the renderer |
| 17 | +<template> |
| 18 | + <el-form-renderer :content="content"></el-form-renderer> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script> |
| 22 | +import ElFormRenderer from 'el-form-renderer' |
| 23 | +
|
| 24 | +export default { |
| 25 | + components: { |
| 26 | + ElFormRenderer |
| 27 | + } |
| 28 | +} |
| 29 | +</script> |
| 30 | +``` |
| 31 | + |
| 32 | +## Props |
| 33 | + |
| 34 | +* Support all attributes on [el-form](http://element.eleme.io/#/en-US/component/form). |
| 35 | + |
| 36 | +* `disabled` [Boolean] Set `true` to disable all atomic forms. If the `element-ui` version is still below `2.1.0`, it is still compatible. |
| 37 | + |
| 38 | +* `content` [ObjectArray] Define the contents of the form, each `Object` represents an atomic form (such as `el-input, el-select, ...`), All attributes on the `el-form-item` are declared here, and attributes on the `el-input` etc. are declared on the `$el` attribute. There are other attributes on the Object such as: `$id, $type, $default,$enableWhen[optional], $options[optional], $attrs[optional]`. |
| 39 | + |
| 40 | +```js |
| 41 | +// content example |
| 42 | +[ |
| 43 | + { |
| 44 | + $id: "form1", // Each atomic form uses an id to store its value, be careful not to repeat |
| 45 | + $type: "input", // Type, all the form types provided by element, like el-xxx |
| 46 | + $enableWhen: { form2: 'beijing' }, // Optional attribute, which means that the this atomic form will display when form2's value is beijing |
| 47 | + $attrs: { 'data-name': 'form1' }, // Optional attribute, wording follows the Render function specification of Vue |
| 48 | + label: "Input", // A property on the el-form-item |
| 49 | + $default: "default value", |
| 50 | + rules: [{ required: true, message: 'Please enter the name of the activity name', trigger: 'blur' }] // A property on the el-form-item |
| 51 | + }, { |
| 52 | + $id: "form2", |
| 53 | + $type: "select", |
| 54 | + label: "Select", |
| 55 | + // $el: Used to define the properties of a specific atomic form (el-select in this case) |
| 56 | + $el: { |
| 57 | + placeholder: "Please select your zone" |
| 58 | + }, |
| 59 | + // $options: Each atomic form with Selection Capabilities use this to define options. (such as: select, radio-group, radio-button, checkbox-group, checkbox-button, etc.) |
| 60 | + $options: [{ |
| 61 | + label: 'Zone one', |
| 62 | + value: 'shanghai' |
| 63 | + }, { |
| 64 | + label: 'Zone two', |
| 65 | + value: 'beijing' |
| 66 | + }] |
| 67 | + } |
| 68 | +] |
| 69 | +``` |
| 70 | + |
| 71 | +In addition, we added an optional value to the `$type` attribute: `group`, which can be used to create more complex data types: |
| 72 | + |
| 73 | +```js |
| 74 | +// This example will get the object data structure: |
| 75 | +// group1: { |
| 76 | +// input1: '', |
| 77 | +// input2: '' |
| 78 | +// } |
| 79 | +{ |
| 80 | + $id: "group1", // Follow the principle of the same level of ID is not repeated, essentially equivalent to the object's key |
| 81 | + $type: "group", |
| 82 | + label: "object data example", |
| 83 | + $items: [{ |
| 84 | + $id: "input1", |
| 85 | + $type: "input", |
| 86 | + label: "input1", |
| 87 | + $default: "default value" |
| 88 | + }, { |
| 89 | + $id: "input2", |
| 90 | + $type: "input", |
| 91 | + label: "input2", |
| 92 | + $default: "default value", |
| 93 | + }] |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Methods |
| 98 | + |
| 99 | +* Supports all methods on [el-form](http://element.eleme.io/#/en-US/component/form). |
| 100 | + |
| 101 | +* Other Methods: |
| 102 | + |
| 103 | +| Method Name | Description | Parameters | |
| 104 | +| ---------- | -------- | ---------- | |
| 105 | +| getFormValue | Get the value of the current form | - | |
| 106 | +| updateValue | Update form value manually | ({ id, value }) | |
| 107 | + |
| 108 | +## Slot |
| 109 | + |
| 110 | +* You can insert a custom `VNode` at the end of the form by using the default `slot`. |
| 111 | + |
| 112 | +## dev |
| 113 | + |
| 114 | +install dependency |
| 115 | + |
| 116 | +```sh |
| 117 | +yarn |
| 118 | + |
| 119 | +yarn add element-ui |
| 120 | +``` |
| 121 | + |
| 122 | +run dev server |
| 123 | + |
| 124 | +```sh |
| 125 | +yarn dev |
| 126 | +``` |
| 127 | + |
| 128 | +publish to npm |
| 129 | + |
| 130 | +```sh |
| 131 | +// remove element-ui in package.json |
| 132 | +git checkout -- . |
| 133 | + |
| 134 | +npm version |
| 135 | + |
| 136 | +yarn dist |
| 137 | + |
| 138 | +cd dist |
| 139 | + |
| 140 | +npm publish --access public |
| 141 | +``` |
0 commit comments