|
1 | 1 | 
|
2 | 2 |
|
3 |
| -## Software version |
| 3 | +# Vue Dynamic Forms |
4 | 4 |
|
5 |
| -- `node -v` : v12.4.0 |
6 |
| -- `npm -v` : v6.4.1 |
7 |
| -- `yarn -v`: 1.9.4 |
8 |
| -- `vue --version`:3.0.3 |
9 |
| -- `webpack -v` : v4.1.1 |
| 5 | + [](http://opensource.org/licenses/MIT) |
10 | 6 |
|
11 |
| -# vue-dynamic-forms |
| 7 | +Easy way to dynamically create reactive forms in vue based on varying business object model |
12 | 8 |
|
13 |
| -[](http://opensource.org/licenses/MIT) |
| 9 | +## Documentation |
14 | 10 |
|
15 |
| -Easy way to dynamically create reactive forms in vue based on varying business object model |
| 11 | +Complete documentation and examples available at |
| 12 | + |
| 13 | +- **[API Documentation]()** (soon) |
| 14 | +- **[Sandbox Demo]()** (soon) |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +$ npm install @asigloo/vue-dynamic-forms |
| 20 | +``` |
| 21 | + |
| 22 | +or if you prefer yarn |
| 23 | + |
| 24 | +```bash |
| 25 | +$ yarn add @asigloo/vue-dynamic-forms |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Global |
| 31 | + |
| 32 | +Register the component globally in your `main.js`: |
| 33 | + |
| 34 | +```js |
| 35 | +import Vue from 'vue'; |
| 36 | +import VueDynamicForms from '@asigloo/vue-dynamic-forms'; |
| 37 | + |
| 38 | +Vue.use(VueDynamicForms); |
| 39 | +``` |
| 40 | + |
| 41 | +### Local |
| 42 | + |
| 43 | +You can include the dynamic form directly to your component. |
| 44 | + |
| 45 | +```js |
| 46 | +import Vue from 'vue'; |
| 47 | +import { DynamicForm } from '@asigloo/vue-dynamic-forms'; |
| 48 | + |
| 49 | +const components = { DynamicForm }; |
| 50 | + |
| 51 | +export { |
| 52 | + ... |
| 53 | + components, |
| 54 | + ... |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Form Composition |
| 59 | + |
| 60 | +The dynamic form component is really easy to use, you will only need to add it to your template like this: |
| 61 | + |
| 62 | +```html |
| 63 | +<template> |
| 64 | + <dynamic-form :id="testForm.id" :fields="testForm.fields" /> |
| 65 | + <button type="submit" form="testForm.id">Submit</button> |
| 66 | +</template> |
| 67 | +``` |
| 68 | + |
| 69 | +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: |
| 70 | + |
| 71 | +```js |
| 72 | +import { FormField } from '@asigloo/vue-dynamic-forms'; |
| 73 | + |
| 74 | +const yourAwesomeComponent = { |
| 75 | + name: 'your-awesome', |
| 76 | + data() { |
| 77 | + return { |
| 78 | + testForm: { |
| 79 | + id: 'test-form', |
| 80 | + fields: [ |
| 81 | + new FormField({ |
| 82 | + type: 'text', |
| 83 | + label: 'Name', |
| 84 | + name: 'name', |
| 85 | + }), |
| 86 | + new FormField({ |
| 87 | + type: 'email', |
| 88 | + label: 'Email', |
| 89 | + name: 'email', |
| 90 | + }), |
| 91 | + ], |
| 92 | + }, |
| 93 | + }; |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +export default yourAwesomeComponent; |
| 98 | +``` |
| 99 | + |
| 100 | +Each `FormField` has a set of properties to customize your input field: |
16 | 101 |
|
17 |
| -## Project setup |
| 102 | +| Property | Type | Default | Description | |
| 103 | +| :---------- | :--------------- | :------ | :----------------------------------------------------------------------------------------------------- | |
| 104 | +| type | String | text | Define the nature of the field, can be `text|email|url|password|number|radio|checkbox|textarea|select` | |
| 105 | +| label | String | null | Defines the text in the input label | |
| 106 | +| placeholder | String | null | Defines short hint that describes the expected value of an input field | |
| 107 | +| name | String | null | Name property for the field inside of the form data | |
| 108 | +| value | any | null | Initial value of the input field | |
| 109 | +| disabled | Boolean | false | Whenever the input field is disabled or not | |
| 110 | +| customClass | String | null | Allows the user to set custom classes to the input for styling purpouses | |
| 111 | +| options | Array of Objects | [] | Options for input type `select` | |
| 112 | +| inline | Boolean | false | Specifies if options for `radio|checkbox` should be inline | |
| 113 | +| validations | Array of Objects | [] | Array of `FormValidation` objects, specify validations and messages for the input | |
| 114 | + |
| 115 | +### Events and Submit |
| 116 | + |
| 117 | +| Event | Type | Emitter | Description | |
| 118 | +| :----------- | :--- | :------------ | :----------------------------------------------------------------------------------------------------------------------------- | |
| 119 | +| `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 | |
| 120 | +| `form-error` | | `DynamicForm` | Emits whenever the submit button has been clicked and the form is invalid (with errors), returns all errors | |
| 121 | +| `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 | |
| 122 | + |
| 123 | +A small example of a form without submit button |
| 124 | + |
| 125 | +```html |
| 126 | +<template> |
| 127 | + <dynamic-form |
| 128 | + :id="testForm.id" |
| 129 | + :fields="testForm.fields" |
| 130 | + @change="valuesChanged" |
| 131 | + /> |
| 132 | +</template> |
| 133 | +``` |
| 134 | + |
| 135 | +```js |
| 136 | +... |
| 137 | +const yourAwesomeComponent = { |
| 138 | + name: 'your-awesome', |
| 139 | + methods: { |
| 140 | + valuesChanged(values) { |
| 141 | + this.formData = { |
| 142 | + ...this.formData, |
| 143 | + ...values, |
| 144 | + }; |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | +... |
| 149 | +``` |
| 150 | + |
| 151 | +### Select input |
| 152 | + |
| 153 | +```js |
| 154 | +... |
| 155 | +new FormField({ |
| 156 | + type: 'select', |
| 157 | + label: 'Category', |
| 158 | + name: 'category', |
| 159 | + options: [ |
| 160 | + { value: null, text: 'Please select an option' }, |
| 161 | + { value: 'arduino', text: 'Arduino' }, |
| 162 | + { value: 'transistors', text: 'Transistors' }, |
| 163 | + ], |
| 164 | +}), |
| 165 | +... |
| 166 | +``` |
| 167 | + |
| 168 | +### Radio and Checkboxes |
| 169 | + |
| 170 | +```js |
| 171 | +... |
| 172 | +new FormField({ |
| 173 | + type: 'checkbox', |
| 174 | + label: 'Read the conditions', |
| 175 | + name: 'conditions', |
| 176 | + inline: false, |
| 177 | +}), |
| 178 | +new FormField({ |
| 179 | + type: 'radio', |
| 180 | + label: 'Prefered Animal', |
| 181 | + name: 'animal', |
| 182 | + inline: true, |
| 183 | + options: [ |
| 184 | + { text: 'Dogs', value: 'dogs' }, |
| 185 | + { text: 'Cats', value: 'cats' }, |
| 186 | + { text: 'Others', value: 'others' }, |
| 187 | + ], |
| 188 | +}), |
| 189 | +... |
| 190 | +``` |
| 191 | + |
| 192 | +### Validation |
| 193 | + |
| 194 | +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. |
| 195 | + |
| 196 | +To use it you need the following code: |
| 197 | + |
| 198 | +```js |
| 199 | +import { |
| 200 | + FormField, |
| 201 | + FormValidation, |
| 202 | + required, |
| 203 | + email, |
| 204 | + pattern, |
| 205 | +} from '@asigloo/vue-dynamic-forms'; |
| 206 | + |
| 207 | +const yourAwesomeComponent = { |
| 208 | + name: 'your-awesome', |
| 209 | + data() { |
| 210 | + return { |
| 211 | + testForm: { |
| 212 | + id: 'test-form', |
| 213 | + fields: [ |
| 214 | + new FormField({ |
| 215 | + type: 'text', |
| 216 | + label: 'Name', |
| 217 | + name: 'name', |
| 218 | + }), |
| 219 | + new FormField({ |
| 220 | + type: 'email', |
| 221 | + label: 'Email', |
| 222 | + name: 'email', |
| 223 | + validations: [ |
| 224 | + new FormValidation(required, 'This field is required'), |
| 225 | + new FormValidation(email, 'Format of email is incorrect'), |
| 226 | + ], |
| 227 | + }), |
| 228 | + new FormField({ |
| 229 | + type: 'password', |
| 230 | + label: 'Password', |
| 231 | + name: 'password', |
| 232 | + validations: [ |
| 233 | + new FormValidation(required, 'This field is required'), |
| 234 | + new FormValidation( |
| 235 | + pattern( |
| 236 | + '^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[#$^+=!*()@%&]).{8,10}$', |
| 237 | + ), |
| 238 | + 'Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10', |
| 239 | + ), |
| 240 | + ], |
| 241 | + }), |
| 242 | + ], |
| 243 | + }, |
| 244 | + }; |
| 245 | + }, |
| 246 | +}; |
| 247 | + |
| 248 | +export default yourAwesomeComponent; |
| 249 | +``` |
| 250 | + |
| 251 | +### Styling themes |
| 252 | + |
| 253 | +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/` |
| 254 | + |
| 255 | +## Development |
| 256 | + |
| 257 | +### Project setup |
18 | 258 |
|
19 | 259 | ```
|
20 | 260 | yarn install
|
21 | 261 | ```
|
22 | 262 |
|
23 |
| -### Compiles and hot-reloads for development |
| 263 | +### Compiles and hot-reloads |
24 | 264 |
|
25 | 265 | ```
|
26 | 266 | yarn run serve
|
@@ -49,3 +289,22 @@ yarn run lint
|
49 | 289 | ```
|
50 | 290 | yarn run test:unit
|
51 | 291 | ```
|
| 292 | + |
| 293 | +## Contributing |
| 294 | + |
| 295 | +If you find this library useful and you want to help improve it, maintain it or just want a new feature, feel free to contact me, or feel free to do a PR 😁. |
| 296 | + |
| 297 | +## Todolist |
| 298 | + |
| 299 | +This are the features I have planned for next versions of this library |
| 300 | + |
| 301 | +- [ ] Material theme |
| 302 | +- [ ] Form Mixins for fields manipulation (for example, change values of a field depending of other) |
| 303 | +- [ ] More complex input types. |
| 304 | +- [ ] Nuxt plugin istall |
| 305 | +- [ ] Better docs & online examples |
| 306 | +- [ ] Storybook |
| 307 | + |
| 308 | +## License |
| 309 | + |
| 310 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
0 commit comments