Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit f894ca5

Browse files
authored
Merge pull request #185 from asigloo/feature/factory-functions-for-typed-form-fields-next
feat(models): Added new factory functions for fields.
2 parents c5b522e + 5643ffd commit f894ca5

File tree

8 files changed

+235
-114
lines changed

8 files changed

+235
-114
lines changed

dev/typescript/App.vue

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,18 @@
4949
import { mockAsync } from '@/core/utils/helpers';
5050
import { defineComponent, onMounted, reactive, ref } from 'vue';
5151
import {
52-
TextInput,
53-
SelectInput,
54-
EmailInput,
5552
FormValidation,
56-
PasswordInput,
57-
CheckboxInput,
58-
RadioInput,
5953
email,
6054
pattern,
61-
ColorInput,
62-
NumberInput,
63-
CustomInput,
64-
BindingObject,
55+
TextField,
56+
SelectField,
57+
EmailField,
58+
PasswordField,
59+
NumberField,
60+
CheckboxField,
61+
RadioField,
62+
CustomField,
63+
ColorField,
6564
} from '../../src';
6665
/* } from '../../dist/as-dynamic-forms.esm'; */
6766
export default defineComponent({
@@ -100,34 +99,28 @@ export default defineComponent({
10099
'readonly',
101100
],
102101
fields: {
103-
name: {
102+
name: TextField({
104103
label: 'Name',
105-
type: 'text',
106-
value: 'Alvaro',
107104
required: true,
108-
} as TextInput,
109-
email: {
105+
}),
106+
email: EmailField({
110107
label: 'Email',
111-
type: 'email',
112108
validations: [emailValidator],
113109
customClass: {
114110
active: true,
115111
'text-blue': true,
116112
},
117-
} as EmailInput,
118-
password: {
113+
}),
114+
password: PasswordField({
119115
label: 'Password',
120-
type: 'password',
121116
autocomplete: 'current-password',
122117
validations: [passwordValidator],
123-
} as PasswordInput,
124-
stock: {
118+
}),
119+
stock: NumberField({
125120
label: 'Stock',
126-
type: 'number',
127-
} as NumberInput,
128-
games: {
121+
}),
122+
games: SelectField({
129123
label: 'Games',
130-
type: 'select',
131124
customClass: 'w-1/2',
132125
value: 'the-last-of-us',
133126
options: [
@@ -144,28 +137,23 @@ export default defineComponent({
144137
value: 'Nier Automata',
145138
},
146139
],
147-
} as SelectInput,
148-
console: {
140+
}),
141+
console: SelectField({
149142
label: 'Console (Async Options)',
150-
type: 'select',
151143
customClass: 'w-1/2',
152-
options: [],
153-
} as SelectInput,
154-
steps: {
144+
}),
145+
steps: NumberField({
155146
label: 'Number',
156-
type: 'number',
157147
min: 5,
158148
max: 60,
159149
step: 5,
160150
value: 5,
161-
} as NumberInput,
162-
awesomeness: {
151+
}),
152+
awesomeness: CheckboxField({
163153
label: "Check if you're awesome",
164-
type: 'checkbox',
165-
} as CheckboxInput,
166-
character: {
154+
}),
155+
character: RadioField({
167156
label: 'Select one option',
168-
type: 'radio',
169157
options: [
170158
{
171159
key: 'mario',
@@ -185,30 +173,26 @@ export default defineComponent({
185173
disabled: true,
186174
},
187175
],
188-
} as RadioInput,
189-
customField1: {
190-
type: 'custom-field',
176+
}),
177+
customField1: CustomField({
191178
label: 'Custom Field',
192-
} as CustomInput,
193-
color: {
179+
}),
180+
color: ColorField({
194181
label: 'Color',
195-
type: 'color',
196182
value: '#4DBA87',
197-
} as ColorInput,
198-
customStyles: {
183+
}),
184+
customStyles: TextField({
199185
label: 'Custom Styles',
200-
type: 'text',
201186
required: true,
202187
customStyles: {
203188
border: '1px solid teal',
204189
},
205-
} as TextInput,
206-
readonly: {
190+
}),
191+
readonly: TextField({
207192
label: 'Readonly',
208-
type: 'text',
209193
value: 'Alvaro',
210194
readonly: true,
211-
} as TextInput,
195+
}),
212196
},
213197
});
214198

dev/vue/App.vue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<script lang="ts">
4848
import { mockAsync } from '@/core/utils/helpers';
4949
import { defineComponent, onMounted, reactive, ref } from 'vue';
50-
import { email, pattern } from '../../src';
50+
import { email, FieldTypes, pattern } from '../../src';
5151
5252
export default defineComponent({
5353
name: 'app',
@@ -86,26 +86,26 @@ export default defineComponent({
8686
fields: {
8787
name: {
8888
label: 'Name',
89-
type: 'text',
89+
type: FieldTypes.TEXT,
9090
value: 'Alvaro',
9191
},
9292
email: {
9393
label: 'Email',
94-
type: 'email',
94+
type: FieldTypes.EMAIL,
9595
validations: [emailValidator],
9696
},
9797
password: {
9898
label: 'Password',
99-
type: 'password',
99+
type: FieldTypes.PASSWORD,
100100
validations: [passwordValidator],
101101
},
102102
stock: {
103103
label: 'Stock',
104-
type: 'number',
104+
type: FieldTypes.NUMBER,
105105
},
106106
games: {
107107
label: 'Games',
108-
type: 'select',
108+
type: FieldTypes.SELECT,
109109
customClass: 'w-1/2',
110110
value: 'the-last-of-us',
111111
options: [
@@ -125,25 +125,25 @@ export default defineComponent({
125125
},
126126
console: {
127127
label: 'Console (Async Options)',
128-
type: 'select',
128+
type: FieldTypes.SELECT,
129129
customClass: 'w-1/2',
130130
options: [],
131131
},
132132
steps: {
133133
label: 'Number',
134-
type: 'number',
134+
type: FieldTypes.NUMBER,
135135
min: 5,
136136
max: 60,
137137
step: 5,
138138
value: 5,
139139
},
140140
awesomeness: {
141141
label: "Check if you're awesome",
142-
type: 'checkbox',
142+
type: FieldTypes.CHECKBOX,
143143
},
144144
character: {
145145
label: 'Select one option',
146-
type: 'radio',
146+
type: FieldTypes.RADIO,
147147
options: [
148148
{
149149
key: 'mario',
@@ -165,12 +165,12 @@ export default defineComponent({
165165
],
166166
},
167167
customField1: {
168-
type: 'custom-field',
168+
type: FieldTypes.CUSTOM,
169169
label: 'Custom Field',
170170
},
171171
color: {
172172
label: 'Color',
173-
type: 'color',
173+
type: FieldTypes.COLOR,
174174
value: '#4DBA87',
175175
},
176176
},

src/components/dynamic-form/DynamicForm.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
import { DynamicForm } from './form';
5050
import DynamicInput from '../dynamic-input/DynamicInput.vue';
5151
52-
import { FormControl, InputType } from '@/core/models';
52+
import { FieldTypes, FormControl, InputType } from '@/core/models';
5353
import { dynamicFormsSymbol } from '@/useApi';
5454
import { removeEmpty } from '@/core/utils/helpers';
5555
@@ -215,7 +215,7 @@ export default defineComponent({
215215
? controls.value.reduce((prev, curr) => {
216216
const obj = {};
217217
obj[curr.name] =
218-
curr.type === 'number'
218+
curr.type === FieldTypes.NUMBER
219219
? parseFloat(`${curr.value}`)
220220
: curr.value;
221221
return {

src/components/dynamic-input/DynamicInput.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
TextAreaInput,
2424
InputType,
2525
BindingObject,
26+
FieldTypes,
2627
} from '@/core/models';
2728
2829
import {
@@ -88,7 +89,7 @@ export default defineComponent({
8889
'dynamic-input',
8990
'form-group',
9091
{
91-
'form-group--inline': props?.control?.type === 'checkbox',
92+
'form-group--inline': props?.control?.type === FieldTypes.CHECKBOX,
9293
},
9394
{
9495
'form-group--error': showErrors.value,
@@ -185,7 +186,7 @@ export default defineComponent({
185186
186187
return () => {
187188
switch (props?.control?.type) {
188-
case 'text':
189+
case FieldTypes.TEXT:
189190
component = h(
190191
TextInputComponent,
191192
attributes.value as ControlAttribute<TextInput>,

0 commit comments

Comments
 (0)