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

Commit 1b3a962

Browse files
committed
feat(models): Added new factory functions for fields.
1 parent c5b522e commit 1b3a962

File tree

2 files changed

+171
-56
lines changed

2 files changed

+171
-56
lines changed

dev/typescript/App.vue

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ import {
6262
NumberInput,
6363
CustomInput,
6464
BindingObject,
65+
TextField,
66+
SelectField,
67+
EmailField,
68+
PasswordField,
69+
NumberField,
70+
CheckboxField,
71+
RadioField,
72+
CustomField,
73+
ColorField,
6574
} from '../../src';
6675
/* } from '../../dist/as-dynamic-forms.esm'; */
6776
export default defineComponent({
@@ -100,34 +109,28 @@ export default defineComponent({
100109
'readonly',
101110
],
102111
fields: {
103-
name: {
112+
name: TextField({
104113
label: 'Name',
105-
type: 'text',
106-
value: 'Alvaro',
107114
required: true,
108-
} as TextInput,
109-
email: {
115+
}),
116+
email: EmailField({
110117
label: 'Email',
111-
type: 'email',
112118
validations: [emailValidator],
113119
customClass: {
114120
active: true,
115121
'text-blue': true,
116122
},
117-
} as EmailInput,
118-
password: {
123+
}),
124+
password: PasswordField({
119125
label: 'Password',
120-
type: 'password',
121126
autocomplete: 'current-password',
122127
validations: [passwordValidator],
123-
} as PasswordInput,
124-
stock: {
128+
}),
129+
stock: NumberField({
125130
label: 'Stock',
126-
type: 'number',
127-
} as NumberInput,
128-
games: {
131+
}),
132+
games: SelectField({
129133
label: 'Games',
130-
type: 'select',
131134
customClass: 'w-1/2',
132135
value: 'the-last-of-us',
133136
options: [
@@ -144,28 +147,23 @@ export default defineComponent({
144147
value: 'Nier Automata',
145148
},
146149
],
147-
} as SelectInput,
148-
console: {
150+
}),
151+
console: SelectField({
149152
label: 'Console (Async Options)',
150-
type: 'select',
151153
customClass: 'w-1/2',
152-
options: [],
153-
} as SelectInput,
154-
steps: {
154+
}),
155+
steps: NumberField({
155156
label: 'Number',
156-
type: 'number',
157157
min: 5,
158158
max: 60,
159159
step: 5,
160160
value: 5,
161-
} as NumberInput,
162-
awesomeness: {
161+
}),
162+
awesomeness: CheckboxField({
163163
label: "Check if you're awesome",
164-
type: 'checkbox',
165-
} as CheckboxInput,
166-
character: {
164+
}),
165+
character: RadioField({
167166
label: 'Select one option',
168-
type: 'radio',
169167
options: [
170168
{
171169
key: 'mario',
@@ -185,30 +183,26 @@ export default defineComponent({
185183
disabled: true,
186184
},
187185
],
188-
} as RadioInput,
189-
customField1: {
190-
type: 'custom-field',
186+
}),
187+
customField1: CustomField({
191188
label: 'Custom Field',
192-
} as CustomInput,
193-
color: {
189+
}),
190+
color: ColorField({
194191
label: 'Color',
195-
type: 'color',
196192
value: '#4DBA87',
197-
} as ColorInput,
198-
customStyles: {
193+
}),
194+
customStyles: TextField({
199195
label: 'Custom Styles',
200-
type: 'text',
201196
required: true,
202197
customStyles: {
203198
border: '1px solid teal',
204199
},
205-
} as TextInput,
206-
readonly: {
200+
}),
201+
readonly: TextField({
207202
label: 'Readonly',
208-
type: 'text',
209203
value: 'Alvaro',
210204
readonly: true,
211-
} as TextInput,
205+
}),
212206
},
213207
});
214208

src/core/models.ts

Lines changed: 136 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
12
export type InputTypeKeys =
23
| 'text'
34
| 'email'
@@ -59,63 +60,63 @@ export interface InputBase {
5960
}
6061

6162
export type TextInput = InputBase & {
62-
type: 'text';
63-
value: string;
63+
type: string;
64+
value?: string;
6465
};
6566

6667
export type NumberInput = InputBase & {
67-
type: 'number';
68+
type: string;
6869
value: number;
69-
min?: number;
70-
max?: number;
71-
step?: number;
70+
min: number;
71+
max: number;
72+
step: number;
7273
};
7374

7475
export type SelectInput<T = boolean | string> = InputBase & {
75-
type: 'select';
76+
type: string;
7677
value: T;
7778
options?: { key: string; value: string; disabled?: boolean }[];
7879
};
7980

8081
export type TextAreaInput = InputBase & {
81-
type: 'textarea';
82+
type: string;
8283
value: string;
8384
cols?: number;
8485
rows?: number;
8586
};
8687

8788
export type CheckboxInput = InputBase & {
88-
type: 'checkbox';
89+
type: string;
8990
value: boolean;
9091
};
9192

9293
export type CustomInput = InputBase & {
93-
type: 'custom-field';
94+
type: string;
9495
value: boolean | string | number;
9596
};
9697

9798
export type EmailInput = InputBase & {
98-
type: 'email';
99+
type: string;
99100
value: string;
100101
};
101102

102103
export type PasswordInput = InputBase & {
103-
type: 'password';
104+
type: string;
104105
value: string;
105106
};
106107

107108
export type ColorInput = InputBase & {
108-
type: 'color';
109+
type: string;
109110
value: string;
110111
};
111112

112113
export type UrlInput = InputBase & {
113-
type: 'url';
114+
type: string;
114115
value: string;
115116
};
116117

117118
export type RadioInput = InputBase & {
118-
type: 'radio';
119+
type: string;
119120
value: string;
120121
options?: { key: string; value: string; disabled?: boolean }[];
121122
};
@@ -136,3 +137,123 @@ export interface FormOptions {
136137
netlifyHoneypot?: string;
137138
autocomplete?: boolean;
138139
}
140+
141+
export const fieldTypes = {
142+
TEXT: 'text',
143+
TEXTAREA: 'textarea',
144+
SELECT: 'select',
145+
NUMBER: 'number',
146+
EMAIL: 'email',
147+
URL: 'url',
148+
PASSWORD: 'password',
149+
CHECKBOX: 'checkbox',
150+
RADIO: 'radio',
151+
CUSTOM: 'custom-field',
152+
COLOR: 'color',
153+
};
154+
155+
// Factory Functions
156+
157+
export const FieldBase = ({
158+
value = null,
159+
validations = [],
160+
label = null,
161+
ariaLabel = null,
162+
ariaLabelledBy = null,
163+
customClass = null,
164+
customStyles = null,
165+
disabled = false,
166+
placeholder = null,
167+
inline = false,
168+
required = false,
169+
autocomplete = null,
170+
readonly = false,
171+
} = {}): InputBase =>
172+
({
173+
value,
174+
validations,
175+
label,
176+
ariaLabel,
177+
ariaLabelledBy,
178+
customClass,
179+
customStyles,
180+
disabled,
181+
placeholder,
182+
inline,
183+
required,
184+
autocomplete,
185+
readonly,
186+
} as InputBase);
187+
188+
export const TextField = ({ value = null, ...rest }): TextInput => ({
189+
...FieldBase(rest),
190+
value,
191+
type: fieldTypes.TEXT,
192+
});
193+
194+
export const EmailField = ({ value = null, ...rest }): EmailInput => ({
195+
...FieldBase(rest),
196+
value,
197+
type: fieldTypes.EMAIL,
198+
});
199+
200+
export const PasswordField = ({ value = null, ...rest }): PasswordInput => ({
201+
...FieldBase(rest),
202+
value,
203+
type: fieldTypes.PASSWORD,
204+
});
205+
206+
export const CheckboxField = ({ value = null, ...rest }): CheckboxInput => ({
207+
...FieldBase(rest),
208+
value,
209+
type: fieldTypes.CHECKBOX,
210+
});
211+
212+
export const ColorField = ({ value = null, ...rest }): ColorInput => ({
213+
...FieldBase(rest),
214+
value,
215+
type: fieldTypes.COLOR,
216+
});
217+
218+
export const RadioField = ({
219+
options = [],
220+
value = null,
221+
...rest
222+
}): RadioInput => ({
223+
...FieldBase(rest),
224+
value,
225+
options,
226+
type: fieldTypes.RADIO,
227+
});
228+
229+
export const NumberField = ({
230+
value = null,
231+
min = 0,
232+
max = 100,
233+
step = 1,
234+
...rest
235+
}): NumberInput => ({
236+
...FieldBase(rest),
237+
value,
238+
min,
239+
max,
240+
step,
241+
type: fieldTypes.NUMBER,
242+
});
243+
244+
export const SelectField = ({
245+
options = [],
246+
value = null,
247+
...rest
248+
}): SelectInput => ({
249+
...FieldBase(rest),
250+
value,
251+
options,
252+
type: fieldTypes.SELECT,
253+
});
254+
255+
export const CustomField = ({ value = null, ...rest }): CustomInput => ({
256+
...FieldBase(rest),
257+
value,
258+
type: fieldTypes.CUSTOM,
259+
});

0 commit comments

Comments
 (0)