|
1 |
| - |
| 1 | + |
2 | 2 |
|
3 | 3 | # Vue Dynamic Forms
|
4 | 4 |
|
5 | 5 | <p align="center">
|
6 | 6 | <a href="https://www.npmjs.com/package/@asigloo/vue-dynamic-forms">
|
7 | 7 | <img src="https://badgen.net/npm/v/@asigloo/vue-dynamic-forms" alt="Current npm version">
|
8 | 8 | </a>
|
9 |
| - <a href="https://bundlephobia.com/result?p=@asigloo/vue-dynamic-forms@0.1.2"> |
| 9 | + <a href="https://bundlephobia.com/result?p=@asigloo/vue-dynamic-forms@0.2.0"> |
10 | 10 | <img src="https://flat.badgen.net/bundlephobia/min/@asigloo/vue-dynamic-forms" alt="Minified size">
|
11 | 11 | </a>
|
12 | 12 | <a href="https://vuejs.org">
|
|
15 | 15 | </p>
|
16 | 16 |
|
17 | 17 | Implementing handcrafted forms can be:
|
| 18 | + |
18 | 19 | 1. Costly
|
19 | 20 | 2. Time-consuming
|
20 | 21 |
|
21 |
| -Specially if you need to create a very large form, in which the inputs are similar to each other, and they change frequently to meet rapidly changing business and regulatory requirements. |
| 22 | +Especially if you need to create a very large form, in which the inputs are similar to each other, and they change frequently to meet rapidly changing business and regulatory requirements. |
22 | 23 |
|
23 |
| -So, will not be more economical to create the forms dynamically? Based on metadata that describes the business object model? |
| 24 | +So, wouldn't it be more economical to create the forms dynamically? Based on metadata that describes the business object model? |
24 | 25 |
|
25 | 26 | That's Vue Dynamic Forms.
|
26 | 27 |
|
27 | 28 | ## Documentation
|
28 | 29 |
|
29 | 30 | Complete documentation and examples available at
|
30 | 31 |
|
31 |
| -- **[API Documentation]()** (soon) |
| 32 | +- **[Documentation](https://vue-dynamic-forms.netlify.app)** |
32 | 33 | - **[Sandbox Demo](https://codesandbox.io/s/vue-dynamic-forms-ftzes)**
|
33 | 34 |
|
34 | 35 | ## Installation
|
@@ -73,207 +74,6 @@ export {
|
73 | 74 | }
|
74 | 75 | ```
|
75 | 76 |
|
76 |
| -### Form Composition |
77 |
| - |
78 |
| -The dynamic form component is really easy to use, you will only need to add it to your template like this: |
79 |
| - |
80 |
| -```html |
81 |
| -<template> |
82 |
| - <dynamic-form :id="testForm.id" :fields="testForm.fields" /> |
83 |
| - <button type="submit" :form="testForm.id">Submit</button> |
84 |
| -</template> |
85 |
| -``` |
86 |
| - |
87 |
| -And then create the fields on your component's data structure, to create the fields you can import the factory function `FormField` from the library core: |
88 |
| - |
89 |
| -```js |
90 |
| -import { FormField } from '@asigloo/vue-dynamic-forms'; |
91 |
| - |
92 |
| -const yourAwesomeComponent = { |
93 |
| - name: 'your-awesome', |
94 |
| - data() { |
95 |
| - return { |
96 |
| - testForm: { |
97 |
| - id: 'test-form', |
98 |
| - fields: [ |
99 |
| - new FormField({ |
100 |
| - type: 'text', |
101 |
| - label: 'Name', |
102 |
| - name: 'name', |
103 |
| - }), |
104 |
| - new FormField({ |
105 |
| - type: 'email', |
106 |
| - label: 'Email', |
107 |
| - name: 'email', |
108 |
| - }), |
109 |
| - ], |
110 |
| - }, |
111 |
| - }; |
112 |
| - }, |
113 |
| -}; |
114 |
| - |
115 |
| -export default yourAwesomeComponent; |
116 |
| -``` |
117 |
| - |
118 |
| -Each `FormField` has a set of properties to customize your input field: |
119 |
| - |
120 |
| -| Property | Type | Default | Description | |
121 |
| -| :---------- | :--------------- | :------ | :----------------------------------------------------------------------------------------------------- | |
122 |
| -| type | String | text | Define the nature of the field, can be `text|email|url|password|number|radio|checkbox|textarea|select` | |
123 |
| -| label | String | null | Defines the text in the input label | |
124 |
| -| placeholder | String | null | Defines short hint that describes the expected value of an input field | |
125 |
| -| name | String | null | Name property for the field inside of the form data | |
126 |
| -| value | any | null | Initial value of the input field | |
127 |
| -| disabled | Boolean | false | Whenever the input field is disabled or not | |
128 |
| -| customClass | String | null | Allows the user to set custom classes to the input for styling purpouses | |
129 |
| -| options | Array of Objects | [] | Options for input type `select` | |
130 |
| -| inline | Boolean | false | Specifies if options for `radio|checkbox` should be inline | |
131 |
| -| validations | Array of Objects | [] | Array of `FormValidation` objects, specify validations and messages for the input | |
132 |
| - |
133 |
| -### Events and Submit |
134 |
| - |
135 |
| -| Event | Type | Emitter | Description | |
136 |
| -| :----------- | :--- | :------------ | :----------------------------------------------------------------------------------------------------------------------------- | |
137 |
| -| `submit` | | `DynamicForm` | Emits whenever the submit button has been clicked and the form is valid (no errors), returns all the form values in an Object | |
138 |
| -| `form-error` | | `DynamicForm` | Emits whenever the submit button has been clicked and the form is invalid (with errors), returns all errors | |
139 |
| -| `change` | | `DynamicForm` | Emits every time there is a value changed on the form, returns all the form values in an Object without the need ob submitting | |
140 |
| - |
141 |
| -A small example of a form without submit button |
142 |
| - |
143 |
| -```html |
144 |
| -<template> |
145 |
| - <dynamic-form |
146 |
| - :id="testForm.id" |
147 |
| - :fields="testForm.fields" |
148 |
| - @change="valuesChanged" |
149 |
| - /> |
150 |
| -</template> |
151 |
| -``` |
152 |
| - |
153 |
| -```js |
154 |
| -... |
155 |
| -const yourAwesomeComponent = { |
156 |
| - name: 'your-awesome', |
157 |
| - methods: { |
158 |
| - valuesChanged(values) { |
159 |
| - this.formData = { |
160 |
| - ...this.formData, |
161 |
| - ...values, |
162 |
| - }; |
163 |
| - }, |
164 |
| - } |
165 |
| -} |
166 |
| -... |
167 |
| -``` |
168 |
| - |
169 |
| -### Select input |
170 |
| - |
171 |
| -```js |
172 |
| -... |
173 |
| -new FormField({ |
174 |
| - type: 'select', |
175 |
| - label: 'Category', |
176 |
| - name: 'category', |
177 |
| - options: [ |
178 |
| - { value: null, text: 'Please select an option' }, |
179 |
| - { value: 'arduino', text: 'Arduino' }, |
180 |
| - { value: 'transistors', text: 'Transistors' }, |
181 |
| - ], |
182 |
| -}), |
183 |
| -... |
184 |
| -``` |
185 |
| - |
186 |
| -### Radio and Checkboxes |
187 |
| - |
188 |
| -```js |
189 |
| -... |
190 |
| -new FormField({ |
191 |
| - type: 'checkbox', |
192 |
| - label: 'Read the conditions', |
193 |
| - name: 'conditions', |
194 |
| - inline: false, |
195 |
| -}), |
196 |
| -new FormField({ |
197 |
| - type: 'radio', |
198 |
| - label: 'Prefered Animal', |
199 |
| - name: 'animal', |
200 |
| - inline: true, |
201 |
| - options: [ |
202 |
| - { text: 'Dogs', value: 'dogs' }, |
203 |
| - { text: 'Cats', value: 'cats' }, |
204 |
| - { text: 'Others', value: 'others' }, |
205 |
| - ], |
206 |
| -}), |
207 |
| -... |
208 |
| -``` |
209 |
| - |
210 |
| -### Validation |
211 |
| - |
212 |
| -This library offers a simple validation system using the property `validations` as an array of `FormValidation`objects containing the validation function and the text in case of error. |
213 |
| - |
214 |
| -To use it you need the following code: |
215 |
| - |
216 |
| -```js |
217 |
| -import { |
218 |
| - FormField, |
219 |
| - FormValidation, |
220 |
| - required, |
221 |
| - email, |
222 |
| - pattern, |
223 |
| -} from '@asigloo/vue-dynamic-forms'; |
224 |
| - |
225 |
| -const yourAwesomeComponent = { |
226 |
| - name: 'your-awesome', |
227 |
| - data() { |
228 |
| - return { |
229 |
| - testForm: { |
230 |
| - id: 'test-form', |
231 |
| - fields: [ |
232 |
| - new FormField({ |
233 |
| - type: 'text', |
234 |
| - label: 'Name', |
235 |
| - name: 'name', |
236 |
| - }), |
237 |
| - new FormField({ |
238 |
| - type: 'email', |
239 |
| - label: 'Email', |
240 |
| - name: 'email', |
241 |
| - validations: [ |
242 |
| - new FormValidation(required, 'This field is required'), |
243 |
| - new FormValidation(email, 'Format of email is incorrect'), |
244 |
| - ], |
245 |
| - }), |
246 |
| - new FormField({ |
247 |
| - type: 'password', |
248 |
| - label: 'Password', |
249 |
| - name: 'password', |
250 |
| - validations: [ |
251 |
| - new FormValidation(required, 'This field is required'), |
252 |
| - new FormValidation( |
253 |
| - pattern( |
254 |
| - '^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[#$^+=!*()@%&]).{8,10}$', |
255 |
| - ), |
256 |
| - 'Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10', |
257 |
| - ), |
258 |
| - ], |
259 |
| - }), |
260 |
| - ], |
261 |
| - }, |
262 |
| - }; |
263 |
| - }, |
264 |
| -}; |
265 |
| - |
266 |
| -export default yourAwesomeComponent; |
267 |
| -``` |
268 |
| - |
269 |
| -### Styling themes |
270 |
| - |
271 |
| -The components are unstyled by default, so you can customize them with your own styles. If you want a more "ready to go" solution you can import on of the themes we have included in `src/styles/themes/` |
272 |
| - |
273 |
| -```scss |
274 |
| -@import '~@asigloo/vue-dynamic-forms/src/styles/themes/default.scss'; |
275 |
| -``` |
276 |
| - |
277 | 77 | ## Development
|
278 | 78 |
|
279 | 79 | ### Project setup
|
@@ -324,7 +124,7 @@ This are the features I have planned for next versions of this library
|
324 | 124 | - [ ] Form Mixins for fields manipulation (for example, change values of a field depending of other)
|
325 | 125 | - [ ] More complex input types.
|
326 | 126 | - [ ] Nuxt plugin istall
|
327 |
| -- [ ] Better docs & online examples |
| 127 | +- [x] Better docs & online examples |
328 | 128 | - [ ] Storybook
|
329 | 129 |
|
330 | 130 | ## License
|
|
0 commit comments