|
1 | 1 | <template> |
2 | 2 | <div id="app"> |
3 | | - <div class="container"> |
4 | | - <h1 class="title text-center">Dynamic Forms Example</h1> |
5 | | - <div class="row mt-5"> |
6 | | - <div class="col-6"> |
7 | | - <dynamic-form |
8 | | - :id="testForm.id" |
9 | | - :fields="testForm.fields" |
10 | | - :options="testForm.options" |
11 | | - @change="valuesChanged" |
12 | | - > |
13 | | - <template |
14 | | - slot="custom-field-1" |
15 | | - slot-scope="{ control, valueChange, onFocus, onBlur }" |
16 | | - > |
17 | | - <div class="avocado-field"> |
18 | | - <input |
19 | | - v-if="control" |
20 | | - class="form-control" |
21 | | - v-model="control.value" |
22 | | - :type="control.type" |
23 | | - :name="control.name" |
24 | | - @change="valueChange()" |
25 | | - @focus="onFocus()" |
26 | | - @blur="onBlur()" |
27 | | - /> |
28 | | - <i>🥑</i> |
29 | | - </div> |
30 | | - </template> |
31 | | - <template slot="third-party" slot-scope="props"> |
32 | | - <div class="third-party"> |
33 | | - <v-select |
34 | | - v-model="props.control.value" |
35 | | - :options="props.control.options" |
36 | | - :name="props.control.name" |
37 | | - @input="props.valueChange()" |
38 | | - @search:focus="props.onFocus()" |
39 | | - @search:blur="props.onBlur()" |
40 | | - ></v-select> |
41 | | - </div> |
42 | | - </template> |
43 | | - </dynamic-form> |
44 | | - <div class="row d-flex justify-content-end p-4"> |
45 | | - <button submit="true" :form="testForm.id" class="btn btn-primary"> |
46 | | - Submit |
47 | | - </button> |
48 | | - </div> |
49 | | - </div> |
50 | | - <div class="col-6"> |
51 | | - <pre>{{ formData }}</pre> |
52 | | - <pre>{{ testForm }}</pre> |
53 | | - </div> |
54 | | - </div> |
55 | | - </div> |
| 3 | + <dynamic-form :form="form" /> |
| 4 | + <pre>{{ form }}</pre> |
56 | 5 | </div> |
57 | 6 | </template> |
58 | 7 |
|
59 | | -<script> |
60 | | -import { |
61 | | - FormField, |
62 | | - FormValidation, |
63 | | - required, |
64 | | - email, |
65 | | - pattern, |
66 | | - maxLength, |
67 | | - url, |
68 | | - /* } from '../dist/as-dynamic-forms.common'; */ |
69 | | -} from '@/index'; |
| 8 | +<script lang="ts"> |
| 9 | +import { defineComponent, reactive } from 'vue'; |
| 10 | +import { TextInput, SelectInput, DynamicForm } from '../../src/index'; |
70 | 11 |
|
71 | | -const data = () => ({ |
72 | | - formData: {}, |
73 | | - testForm: { |
74 | | - id: 'test-form', |
75 | | - fields: [ |
76 | | - new FormField({ |
77 | | - type: 'text', |
78 | | - label: 'Name', |
79 | | - name: 'name', |
80 | | - customClass: 'col-12', |
81 | | - }), |
82 | | - new FormField({ |
83 | | - type: 'email', |
84 | | - label: 'Email', |
85 | | - name: 'email', |
86 | | - customClass: 'col-12', |
87 | | - validations: [ |
88 | | - new FormValidation(required, 'This field is required'), |
89 | | - new FormValidation(email, 'Format of email is incorrect'), |
90 | | - ], |
91 | | - }), |
92 | | - new FormField({ |
93 | | - type: 'password', |
94 | | - label: 'Password', |
95 | | - name: 'password', |
96 | | - customClass: 'col-12', |
97 | | - validations: [ |
98 | | - new FormValidation(required, 'This field is required'), |
99 | | - new FormValidation( |
100 | | - pattern( |
101 | | - '^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{8,10}$', |
102 | | - ), |
103 | | - 'Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10', |
104 | | - ), |
105 | | - ], |
106 | | -
|
107 | | - value: 'sdsdsd', |
108 | | - }), |
109 | | - new FormField({ |
110 | | - type: 'textarea', |
111 | | - label: 'Bio', |
112 | | - name: 'bio', |
113 | | - cols: 30, |
114 | | - rows: 10, |
115 | | - customClass: 'col-12', |
116 | | - value: |
117 | | - 'Prism whatever occupy, stumptown polaroid butcher activated charcoal seitan cornhole direct trade coloring book offal sriracha. 8-bit pitchfork kitsch crucifix chartreuse, tumblr adaptogen brunch stumptown. Drinking vinegar yuccie echo park lo-fi, poutine unicorn raclette adaptogen kale chips chia. Deep v austin fam organic kickstarter hexagon hell of wolf pour-over YOLO. 8-bit glossier lyft authentic, selfies aesthetic kinfolk prism tattooed irony quinoa distillery pug slow-carb post-ironic.', |
118 | | - validations: [ |
119 | | - new FormValidation( |
120 | | - maxLength(100), |
121 | | - `This field should be less than 100 characters`, |
122 | | - ), |
123 | | - ], |
124 | | - }), |
125 | | - new FormField({ |
126 | | - type: 'url', |
127 | | - label: 'Website', |
128 | | - name: 'website', |
129 | | - customClass: 'col-12', |
130 | | - helper: 'Pstt... psst, this is a hint', |
131 | | - validations: [new FormValidation(url, `Format of Url is incorrect`)], |
132 | | - }), |
133 | | - new FormField({ |
134 | | - type: 'select', |
135 | | - label: 'Category', |
136 | | - name: 'category', |
137 | | - customClass: 'col-12', |
138 | | - options: [ |
139 | | - { value: null, text: 'Please select an option' }, |
140 | | - { value: 'arduino', text: 'Arduino' }, |
141 | | - { value: 'transistors', text: 'Transistors' }, |
142 | | - { value: 'resistors', text: 'Resistors', disabled: true }, |
143 | | - ], |
144 | | - }), |
145 | | - new FormField({ |
146 | | - type: 'checkbox', |
147 | | - label: 'Read the conditions', |
148 | | - name: 'conditions', |
149 | | - inline: false, |
150 | | - customClass: 'col-12', |
151 | | - }), |
152 | | - new FormField({ |
153 | | - type: 'checkbox', |
154 | | - label: 'Disabled', |
155 | | - name: 'disabled', |
156 | | - value: true, |
157 | | - disabled: true, |
158 | | - customClass: 'col-12', |
159 | | - }), |
160 | | - new FormField({ |
161 | | - type: 'radio', |
162 | | - label: 'Prefered Animal', |
163 | | - name: 'animal', |
164 | | - inline: true, |
165 | | - customClass: 'col-12', |
166 | | - options: [ |
167 | | - { text: 'Dogs', value: 'dogs' }, |
168 | | - { text: 'Cats', value: 'cats' }, |
169 | | - { text: 'Others', value: 'others' }, |
170 | | - ], |
171 | | - }), |
172 | | - new FormField({ |
173 | | - type: 'custom-field', |
174 | | - label: 'Custom Field 1', |
175 | | - name: 'custom-field-1', |
176 | | - customClass: 'col-12', |
177 | | - }), |
178 | | - new FormField({ |
179 | | - id: 'number-custom-id', |
180 | | - type: 'number', |
181 | | - label: 'Number', |
182 | | - name: 'number', |
183 | | - value: 0, |
184 | | - customClass: 'col-12 col-md-6', |
185 | | - }), |
186 | | - new FormField({ |
187 | | - type: 'number', |
188 | | - label: 'Number 2', |
189 | | - name: 'number2', |
190 | | - value: 50, |
191 | | - customClass: 'col-12 col-md-6', |
192 | | - }), |
193 | | - new FormField({ |
194 | | - type: 'custom-field', |
195 | | - label: 'V-Select', |
196 | | - name: 'third-party', |
197 | | - customClass: 'col-6', |
198 | | - options: ['Arduino', 'Pinguino'], |
199 | | - }), |
200 | | - ], |
201 | | - options: { |
202 | | - autoValidate: true, |
203 | | - customClass: 'row', |
204 | | - netlify: true, |
205 | | - }, |
206 | | - }, |
207 | | -}); |
208 | | -
|
209 | | -const methods = { |
210 | | - valuesChanged(values) { |
211 | | - this.$forceUpdate(); // this is only to refresh the fields on the <pre> tags, not necessary for other purpouses |
212 | | - this.formData = { |
213 | | - ...this.formData, |
214 | | - ...values, |
215 | | - }; |
216 | | - }, |
217 | | -}; |
218 | | -
|
219 | | -export default { |
| 12 | +export default defineComponent({ |
220 | 13 | name: 'app', |
221 | | - data, |
222 | | - methods, |
223 | | - mounted() { |
224 | | - console.log(this.$formUtils); |
| 14 | + setup() { |
| 15 | + const form = reactive<DynamicForm>({ |
| 16 | + id: 'form', |
| 17 | + fields: [ |
| 18 | + new TextInput({ |
| 19 | + label: 'Name', |
| 20 | + }), |
| 21 | + new SelectInput<string>({ |
| 22 | + label: 'Games', |
| 23 | + options: [ |
| 24 | + { |
| 25 | + key: 'the-last-of-us', |
| 26 | + value: 'The Last of Us II', |
| 27 | + }, |
| 28 | + { |
| 29 | + key: 'death-stranding', |
| 30 | + value: 'Death Stranding', |
| 31 | + }, |
| 32 | + { |
| 33 | + key: 'nier-automata', |
| 34 | + value: 'Nier Automata', |
| 35 | + }, |
| 36 | + ], |
| 37 | + }), |
| 38 | + ], |
| 39 | + }); |
| 40 | + return { |
| 41 | + form, |
| 42 | + }; |
225 | 43 | }, |
226 | | -}; |
| 44 | +}); |
227 | 45 | </script> |
228 | | -<style lang="scss"> |
229 | | -.avocado-field { |
230 | | - position: relative; |
231 | | -
|
232 | | - .form-control { |
233 | | - border-color: #aec64c; |
234 | | - background-color: #e2eb5d52; |
235 | | - border-radius: 16px; |
236 | | - } |
237 | | -
|
238 | | - i { |
239 | | - position: absolute; |
240 | | - top: 5px; |
241 | | - right: 5px; |
242 | | - } |
243 | | -} |
244 | | -</style> |
| 46 | +<style lang="scss"></style> |
0 commit comments