srcdev-nuxt-forms 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nmprc +2 -0
- package/.playground/composables/useApiRequest.ts +25 -0
- package/.playground/composables/useErrorMessages.ts +59 -0
- package/.playground/composables/useFormControl.ts +248 -0
- package/.playground/composables/useSleep.ts +5 -0
- package/.playground/composables/useStyleClassPassthrough.ts +30 -0
- package/.playground/composables/useZodValidation.ts +120 -0
- package/.playground/nuxt.config.ts +27 -0
- package/assets/styles/forms/themes/_error.css +1 -2
- package/assets/styles/forms/themes/_ghost.css +1 -2
- package/assets/styles/forms/themes/_primary.css +1 -1
- package/assets/styles/forms/themes/_secondary.css +1 -2
- package/assets/styles/forms/themes/_success.css +1 -2
- package/assets/styles/forms/themes/_tertiary.css +1 -2
- package/assets/styles/forms/themes/_warning.css +1 -2
- package/components/forms/input-checkbox/InputCheckboxCore.vue +1 -1
- package/components/forms/input-number/InputNumberCore.vue +1 -1
- package/components/forms/input-radio/InputRadiobuttonCore.vue +1 -1
- package/components/forms/input-range/InputRangeCore.vue +1 -1
- package/components/forms/input-text/InputTextCore.vue +5 -4
- package/components/forms/input-textarea/InputTextareaCore.vue +2 -2
- package/nuxt.config.ts +0 -27
- package/package.json +6 -6
- /package/{components → .playground/components}/scaffolding/footer/NavFooter.vue +0 -0
- /package/{components → .playground/components}/ui/content-grid/ContentGrid.vue +0 -0
- /package/{layouts → .playground/layouts}/default.vue +0 -0
- /package/{pages → .playground/pages}/forms/examples/buttons/index.vue +0 -0
- /package/{pages → .playground/pages}/forms/examples/material/cssbattle.vue +0 -0
- /package/{pages → .playground/pages}/forms/examples/material/text-fields.vue +0 -0
- /package/{pages → .playground/pages}/index.vue +0 -0
- /package/{pages → .playground/pages}/limit-text.vue +0 -0
- /package/{pages → .playground/pages}/typography.vue +0 -0
- /package/{server → .playground/server}/api/places/list.get.ts +0 -0
- /package/{server → .playground/server}/api/textFields.post.ts +0 -0
- /package/{server → .playground/server}/api/utils/index.get.ts +0 -0
- /package/{server → .playground/server}/data/places/cities.json +0 -0
- /package/{server → .playground/server}/data/places/countries.json +0 -0
- /package/{server → .playground/server}/data/utils/title.json +0 -0
- /package/{types → .playground/types}/types.forms.ts +0 -0
- /package/{types → .playground/types}/types.places.ts +0 -0
- /package/{types → .playground/types}/types.zodFormControl.ts +0 -0
package/.nmprc
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
class CustomError extends Error {
|
|
2
|
+
// name = "CustomError";
|
|
3
|
+
override name = 'CustomError';
|
|
4
|
+
extraProp = 'Error: test';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async function useApiRequest<T, E extends new (message?: string) => Error>(promise: Promise<T>, errorsToCatch?: E[]): Promise<[undefined, T] | [InstanceType<E>]> {
|
|
8
|
+
return promise
|
|
9
|
+
.then((data) => {
|
|
10
|
+
return [undefined, data] as [undefined, T];
|
|
11
|
+
})
|
|
12
|
+
.catch((error) => {
|
|
13
|
+
if (errorsToCatch == undefined) {
|
|
14
|
+
return [error];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (errorsToCatch.some((errorType) => error instanceof errorType)) {
|
|
18
|
+
return [error];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
throw error;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default useApiRequest;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { IFormData } from '@/types/types.forms';
|
|
2
|
+
|
|
3
|
+
export function useErrorMessage(name: string, formData: Ref<IFormData>) {
|
|
4
|
+
const defaultError = ref('');
|
|
5
|
+
const errorMessages = ref(formData.value.errorMessages);
|
|
6
|
+
|
|
7
|
+
const hasCustomError = () => {
|
|
8
|
+
return errorMessages.value[name] !== undefined && errorMessages.value[name].useCustomError;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const errorMessage = computed(() => {
|
|
12
|
+
console.log(`errorMessage()`);
|
|
13
|
+
if (hasCustomError()) {
|
|
14
|
+
console.log(`errorMessage() | IF`);
|
|
15
|
+
return errorMessages.value[name].message;
|
|
16
|
+
} else {
|
|
17
|
+
return defaultError.value;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const setDefaultError = (newDefaultError: string) => {
|
|
22
|
+
defaultError.value = newDefaultError;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const fieldHasError = computed(() => {
|
|
26
|
+
// console.log(`fieldHasError() | name(${name})`);
|
|
27
|
+
nextTick();
|
|
28
|
+
if (formData.value.submitDisabled) {
|
|
29
|
+
console.log(`fieldHasError() | name(${name}) | IF`);
|
|
30
|
+
if (hasCustomError()) {
|
|
31
|
+
console.log(`fieldHasError() | name(${name}) | IF | IF`);
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
} else if (Object.keys(formData.value.validityState).length > 0 && formData.value.validityState[name] !== undefined) {
|
|
35
|
+
console.log(`fieldHasError() | name(${name}) | IF | ELSE IF`);
|
|
36
|
+
|
|
37
|
+
return !formData.value.validityState[name];
|
|
38
|
+
}
|
|
39
|
+
console.log(`fieldHasError() | name(${name}) | IF | ELSE`);
|
|
40
|
+
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const removeCustomError = (valid: boolean = false) => {
|
|
46
|
+
console.log(`useErrorMessage | removeCustomError | name(${name}) | valid(${valid})`);
|
|
47
|
+
// formData.value.validityState[name] = valid;
|
|
48
|
+
// await nextTick();
|
|
49
|
+
// delete formData.value.errorMessages[name];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
hasCustomError,
|
|
54
|
+
errorMessage,
|
|
55
|
+
setDefaultError,
|
|
56
|
+
fieldHasError,
|
|
57
|
+
removeCustomError,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import type { IFormData, IFieldsInitialState, IFormFieldC12, IApiErrorMessages, ICustomErrorMessage, IErrorMessagesArr } from '@/types/types.forms';
|
|
2
|
+
import { formFieldC12 } from '@/components/forms/c12/utils';
|
|
3
|
+
|
|
4
|
+
// export function useFormControl(name: string = '') {
|
|
5
|
+
export function useFormControl(name: string = '') {
|
|
6
|
+
let savedInitialState = {};
|
|
7
|
+
|
|
8
|
+
const formData = ref<IFormData>({
|
|
9
|
+
data: {} as IFieldsInitialState,
|
|
10
|
+
validityState: {},
|
|
11
|
+
dirtyFields: {},
|
|
12
|
+
focusedField: '',
|
|
13
|
+
isPending: false,
|
|
14
|
+
errorCount: 0,
|
|
15
|
+
errorMessages: {},
|
|
16
|
+
formFieldsC12: {},
|
|
17
|
+
hasCustomErrorMessages: false,
|
|
18
|
+
formIsValid: false,
|
|
19
|
+
submitAttempted: false,
|
|
20
|
+
submitDisabled: false,
|
|
21
|
+
submitSuccess: false,
|
|
22
|
+
displayErrorMessages: false,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const initValidationState = async (fieldsInitialState: IFieldsInitialState | Ref<IFieldsInitialState | null>) => {
|
|
26
|
+
const fields = Object.keys(fieldsInitialState.value || {});
|
|
27
|
+
const state = fields.reduce((accumulatedFields, field) => {
|
|
28
|
+
accumulatedFields[field] = false;
|
|
29
|
+
return accumulatedFields;
|
|
30
|
+
}, {} as Record<string, boolean>);
|
|
31
|
+
formData.value.validityState = state;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const initFormData = async (fieldsInitialState: IFieldsInitialState | Ref<IFieldsInitialState | null>) => {
|
|
35
|
+
initValidationState(fieldsInitialState);
|
|
36
|
+
|
|
37
|
+
if (fieldsInitialState !== null) {
|
|
38
|
+
savedInitialState = toRaw(fieldsInitialState.value) as IFieldsInitialState;
|
|
39
|
+
formData.value.data = fieldsInitialState as IFieldsInitialState;
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const initFormFieldsC12 = (name: string, formFieldC12: IFormFieldC12) => {
|
|
45
|
+
formData.value.formFieldsC12[name] = formFieldC12;
|
|
46
|
+
return;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const updatePreviousValues = () => {
|
|
50
|
+
console.log(`useFormControl | updatePreviousValues`);
|
|
51
|
+
|
|
52
|
+
Object.keys(formData.value.data).forEach((key) => {
|
|
53
|
+
formData.value.formFieldsC12[key].previousValue = formData.value.data[key];
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const getErrorCount = async (updateState: boolean = false) => {
|
|
58
|
+
await nextTick();
|
|
59
|
+
|
|
60
|
+
const errorCount = Object.values(formData.value.validityState).filter((value) => !value).length;
|
|
61
|
+
formData.value.errorCount = errorCount;
|
|
62
|
+
formData.value.formIsValid = errorCount === 0;
|
|
63
|
+
|
|
64
|
+
if (updateState) {
|
|
65
|
+
formData.value.submitDisabled = true;
|
|
66
|
+
formData.value.displayErrorMessages = formData.value.errorCount > 0;
|
|
67
|
+
formData.value.submitAttempted = true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (formData.value.submitDisabled) {
|
|
71
|
+
formData.value.submitDisabled = !formData.value.formIsValid;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// update fieldHasError ref
|
|
75
|
+
// if (typeof formData.value!.formFieldsC12[name] !== 'undefined') {
|
|
76
|
+
// fieldHasError.value = formData.value!.submitAttempted && !formData.value!.formFieldsC12[name].isValid;
|
|
77
|
+
// } else {
|
|
78
|
+
// fieldHasError.value = false;
|
|
79
|
+
// }
|
|
80
|
+
|
|
81
|
+
return formData.value.errorCount;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Function to count items with "useCustomError" set to true
|
|
85
|
+
const countItemsWithCustomError = (obj: IErrorMessagesArr) => {
|
|
86
|
+
let count = 0;
|
|
87
|
+
|
|
88
|
+
for (const key in obj) {
|
|
89
|
+
if (obj.hasOwnProperty(key) && obj[key].useCustomError === true) {
|
|
90
|
+
count++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return count;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/*
|
|
98
|
+
* Useage:
|
|
99
|
+
*
|
|
100
|
+
* const { updateErrorMessages } = useFormControl();
|
|
101
|
+
*
|
|
102
|
+
* Add/Update entry
|
|
103
|
+
* const sampleCustomErrorEmail = {
|
|
104
|
+
* useCustomError: true,
|
|
105
|
+
* message: "This is a sample custom error for error EMAIL",
|
|
106
|
+
* };
|
|
107
|
+
* updateErrorMessages("email", sampleCustomErrorEmail);
|
|
108
|
+
*/
|
|
109
|
+
const updateErrorMessages = async (name: string, message: string = '', valid: boolean = false) => {
|
|
110
|
+
if (!valid) {
|
|
111
|
+
// formData.value.validityState[name] = valid;
|
|
112
|
+
// formData.value.errorMessages[name] = {
|
|
113
|
+
// useCustomError: true,
|
|
114
|
+
// message,
|
|
115
|
+
// };
|
|
116
|
+
|
|
117
|
+
formData.value.formFieldsC12[name].useCustomError = true;
|
|
118
|
+
|
|
119
|
+
// if (typeof message === 'string') {
|
|
120
|
+
// formData.value.formFieldsC12[name].customErrors = message;
|
|
121
|
+
// } else if (typeof message === 'object') {
|
|
122
|
+
// formData.value.formFieldsC12[name].customErrors = message;
|
|
123
|
+
// }
|
|
124
|
+
|
|
125
|
+
formData.value.formFieldsC12[name].customErrors = message;
|
|
126
|
+
formData.value.formFieldsC12[name].isValid = valid;
|
|
127
|
+
|
|
128
|
+
// formData.value.errorMessages[name].useCustomError = true;
|
|
129
|
+
// formData.value.errorMessages[name].message = message;
|
|
130
|
+
}
|
|
131
|
+
formData.value.hasCustomErrorMessages = countItemsWithCustomError(formData.value.errorMessages) > 0;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const useApiErrors = async (errors: IApiErrorMessages) => {
|
|
135
|
+
// Object.keys(errors).forEach((key) => {
|
|
136
|
+
// updateErrorMessages(key, errors[key]);
|
|
137
|
+
// });
|
|
138
|
+
|
|
139
|
+
for (const [key, message] of Object.entries(errors)) {
|
|
140
|
+
// console.log(`${key}: ${message}`);
|
|
141
|
+
updateErrorMessages(key, message);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// const resetForm = () => {
|
|
146
|
+
// console.log('resetForm()');
|
|
147
|
+
// formData.value.data = toRaw(fieldsInitialState.value) as IFieldsInitialState;
|
|
148
|
+
// formData.value.validityState = {};
|
|
149
|
+
// formData.value.errorCount = 0;
|
|
150
|
+
// formData.value.isPending = false;
|
|
151
|
+
// formData.value.errorMessages = {};
|
|
152
|
+
// formData.value.formIsValid = false;
|
|
153
|
+
// };
|
|
154
|
+
|
|
155
|
+
const fieldIsDirty = (name: string) => {
|
|
156
|
+
if (typeof formData.value.formFieldsC12[name] !== 'undefined') {
|
|
157
|
+
return formData.value.formFieldsC12[name].isDirty;
|
|
158
|
+
} else {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// const fieldHasError = (name: string) => {
|
|
164
|
+
// const currentValidityState = formData.value.validityState[name];
|
|
165
|
+
|
|
166
|
+
// if (formData.value.submitAttempted) {
|
|
167
|
+
// return currentValidityState;
|
|
168
|
+
// }
|
|
169
|
+
// return false;
|
|
170
|
+
// };
|
|
171
|
+
|
|
172
|
+
// const fieldHasError = computed({
|
|
173
|
+
// // getter
|
|
174
|
+
// get() {
|
|
175
|
+
// console.log(`fieldHasError getter: ${name}`);
|
|
176
|
+
// if (typeof formData.value!.formFieldsC12[name] !== 'undefined') {
|
|
177
|
+
// return !formData.value!.formFieldsC12[name].isValid;
|
|
178
|
+
// }
|
|
179
|
+
// return formData.value.validityState[name];
|
|
180
|
+
// },
|
|
181
|
+
// // setter
|
|
182
|
+
// set(newValue) {
|
|
183
|
+
// if (formData.value.submitAttempted) {
|
|
184
|
+
// return newValue;
|
|
185
|
+
// }
|
|
186
|
+
// return false;
|
|
187
|
+
// },
|
|
188
|
+
// });
|
|
189
|
+
|
|
190
|
+
// const fieldHasError = ref(false);
|
|
191
|
+
|
|
192
|
+
const formIsValid = computed(() => {
|
|
193
|
+
return formData.value.formIsValid;
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const submitDisabled = computed(() => {
|
|
197
|
+
return formData.value.submitDisabled;
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Keep an eye on this for performance issue
|
|
201
|
+
|
|
202
|
+
// const updateFieldValidity = (name: string, valid: boolean) => {
|
|
203
|
+
// console.log(`updateFieldValidity: name:${name} - valid:${valid}`);
|
|
204
|
+
// console.log(formData.value);
|
|
205
|
+
// // formData.value.formFieldsC12[name].isValid = valid;
|
|
206
|
+
// formData.value.validityState[name] = valid;
|
|
207
|
+
// };
|
|
208
|
+
|
|
209
|
+
watch(
|
|
210
|
+
() => formData.value.validityState,
|
|
211
|
+
() => {
|
|
212
|
+
getErrorCount();
|
|
213
|
+
},
|
|
214
|
+
{ deep: true }
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
watch(
|
|
218
|
+
() => formData.value.formFieldsC12,
|
|
219
|
+
() => {
|
|
220
|
+
formData.value.formFieldsC12;
|
|
221
|
+
},
|
|
222
|
+
{ deep: true }
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
watch(
|
|
226
|
+
() => formData.value.isPending,
|
|
227
|
+
(newValue, oldValue) => {
|
|
228
|
+
if (newValue) {
|
|
229
|
+
updatePreviousValues();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
formData,
|
|
236
|
+
initFormData,
|
|
237
|
+
initFormFieldsC12,
|
|
238
|
+
getErrorCount,
|
|
239
|
+
updateErrorMessages,
|
|
240
|
+
// resetForm,
|
|
241
|
+
formIsValid,
|
|
242
|
+
submitDisabled,
|
|
243
|
+
useApiErrors,
|
|
244
|
+
// fieldHasError,
|
|
245
|
+
fieldIsDirty,
|
|
246
|
+
// updateFieldValidity,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const useStyleClassPassthrough = (styleClassPassthrough: string[]) => {
|
|
2
|
+
const styleClassPassthroughRef = ref(styleClassPassthrough);
|
|
3
|
+
|
|
4
|
+
const elementClasses = computed(() => {
|
|
5
|
+
return styleClassPassthroughRef.value.join(' ');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
const updateElementClasses = (cssClass: string | string[]) => {
|
|
9
|
+
let cssClasses = [] as string[];
|
|
10
|
+
if (typeof cssClass === 'string') {
|
|
11
|
+
cssClasses = [cssClass];
|
|
12
|
+
} else if (Array.isArray(cssClass)) {
|
|
13
|
+
cssClasses = cssClass;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
cssClasses.forEach((cssClass) => {
|
|
17
|
+
if (styleClassPassthroughRef.value.includes(cssClass)) {
|
|
18
|
+
styleClassPassthroughRef.value = styleClassPassthroughRef.value.filter((className) => className !== cssClass);
|
|
19
|
+
} else {
|
|
20
|
+
styleClassPassthroughRef.value.push(cssClass);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
elementClasses,
|
|
27
|
+
updateElementClasses,
|
|
28
|
+
styleClassPassthroughRef,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import type { IFormFieldStateObj, ApiErrorResponse } from '@/types/types.forms';
|
|
3
|
+
|
|
4
|
+
const useZodValidation = (formSchema: any) => {
|
|
5
|
+
const zodFormControl = reactive({
|
|
6
|
+
errorCount: 0,
|
|
7
|
+
displayLoader: false,
|
|
8
|
+
submitDisabled: false,
|
|
9
|
+
submitAttempted: false,
|
|
10
|
+
submitSuccessful: false,
|
|
11
|
+
formIsValid: false,
|
|
12
|
+
isPending: false,
|
|
13
|
+
isDisabled: false,
|
|
14
|
+
previousState: {} as Record<string, any>,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
type formSchema = z.infer<typeof formSchema>;
|
|
18
|
+
const zodErrorObj = ref<z.ZodFormattedError<formSchema> | null>(null);
|
|
19
|
+
|
|
20
|
+
const resetPreviousValues = () => {
|
|
21
|
+
for (const [field] of Object.entries(formSchema.shape)) {
|
|
22
|
+
const previousValue = {
|
|
23
|
+
value: null,
|
|
24
|
+
message: '',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
zodFormControl.previousState[field] = previousValue;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const initZodForm = () => {
|
|
32
|
+
resetPreviousValues();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const getErrorCount = (zodErrorObj: Ref<z.ZodFormattedError<formSchema> | null>) => {
|
|
36
|
+
const zodCountErrors = zodErrorObj.value ?? [];
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
delete zodCountErrors._errors;
|
|
39
|
+
const errorCount = Object.keys(zodCountErrors ?? []).length;
|
|
40
|
+
return errorCount;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const transformErrorMessages = (errors: any) => {
|
|
44
|
+
const apiErrors = ref({}) as any;
|
|
45
|
+
for (const [key, value] of Object.entries(errors)) {
|
|
46
|
+
const fieldPath = key.split('.').map((key: string) => key.charAt(0).toLowerCase() + key.slice(1));
|
|
47
|
+
apiErrors.value[fieldPath.join('.')] = value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return apiErrors.value;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const updatePreviousValue = async (field: string, message: string, state: Record<string, any>) => {
|
|
54
|
+
const previousValue = {
|
|
55
|
+
value: state[field],
|
|
56
|
+
message: message,
|
|
57
|
+
};
|
|
58
|
+
zodFormControl.previousState[field] = previousValue;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const pushCustomErrors = async (apiErrorResponse: ApiErrorResponse, state: Record<string, any>) => {
|
|
62
|
+
const apiErrors = transformErrorMessages(apiErrorResponse.data.errors);
|
|
63
|
+
|
|
64
|
+
// 1: Create a ZodError object to hold the issues
|
|
65
|
+
const zodError = new ZodError([]);
|
|
66
|
+
|
|
67
|
+
// 2: Reset previous state values
|
|
68
|
+
resetPreviousValues();
|
|
69
|
+
|
|
70
|
+
// 3: Add issues to the ZodError object
|
|
71
|
+
for (const [path, message] of Object.entries(apiErrors)) {
|
|
72
|
+
zodError.addIssue({
|
|
73
|
+
path: path.split('.'),
|
|
74
|
+
message: message as string,
|
|
75
|
+
code: z.ZodIssueCode.custom,
|
|
76
|
+
});
|
|
77
|
+
await updatePreviousValue(path, message as string, state);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
zodErrorObj.value = zodError.format();
|
|
81
|
+
zodFormControl.errorCount = getErrorCount(zodErrorObj);
|
|
82
|
+
zodFormControl.formIsValid = zodFormControl.errorCount === 0;
|
|
83
|
+
zodFormControl.displayLoader = false;
|
|
84
|
+
zodFormControl.submitAttempted = true;
|
|
85
|
+
return zodErrorObj.value;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const doZodValidate = async (state: Record<string, any>) => {
|
|
89
|
+
const valid = formSchema.safeParse(toRaw(state));
|
|
90
|
+
if (!valid.success) {
|
|
91
|
+
zodErrorObj.value = valid.error.format();
|
|
92
|
+
} else {
|
|
93
|
+
zodErrorObj.value = null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
zodFormControl.errorCount = getErrorCount(zodErrorObj);
|
|
97
|
+
zodFormControl.formIsValid = valid.success;
|
|
98
|
+
|
|
99
|
+
return valid.success;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const fieldMaxLength = (name: string) => {
|
|
103
|
+
const fieldSchema = formSchema.shape[name];
|
|
104
|
+
if (fieldSchema instanceof z.ZodString) {
|
|
105
|
+
return fieldSchema._def.checks.find((check) => check.kind === 'max')?.value || null;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
initZodForm,
|
|
112
|
+
zodFormControl,
|
|
113
|
+
zodErrorObj,
|
|
114
|
+
pushCustomErrors,
|
|
115
|
+
doZodValidate,
|
|
116
|
+
fieldMaxLength,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default useZodValidation;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default defineNuxtConfig({
|
|
2
|
+
devtools: { enabled: true },
|
|
3
|
+
extends: ['..'],
|
|
4
|
+
app: {
|
|
5
|
+
head: {
|
|
6
|
+
htmlAttrs: {
|
|
7
|
+
lang: 'en',
|
|
8
|
+
},
|
|
9
|
+
titleTemplate: '%s - Website name',
|
|
10
|
+
meta: [{ charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }],
|
|
11
|
+
},
|
|
12
|
+
pageTransition: {
|
|
13
|
+
name: 'page',
|
|
14
|
+
mode: 'out-in',
|
|
15
|
+
},
|
|
16
|
+
layoutTransition: {
|
|
17
|
+
name: 'layout',
|
|
18
|
+
mode: 'out-in',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
components: [
|
|
22
|
+
{
|
|
23
|
+
path: './components',
|
|
24
|
+
pathPrefix: false,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
});
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--red-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--red-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--red-12);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--gray-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--gray-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--gray-1);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--blue-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--blue-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
11
11
|
|
|
12
12
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px light-dark(var(--blue-12), var(--blue-2));
|
|
13
13
|
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--gray-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--gray-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--gray-12);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--green-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--green-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--green-12);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--gray-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--gray-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--gray-12);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
--theme-form-input-outline: white;
|
|
8
8
|
--theme-form-input-outline-active: var(--orange-6);
|
|
9
9
|
--theme-form-input-outline-focus: var(--orange-6);
|
|
10
|
-
--theme-form-input-text: light-dark(var(--gray-
|
|
11
|
-
|
|
10
|
+
--theme-form-input-text: light-dark(var(--gray-10), var(--gray-11));
|
|
12
11
|
--theme-form-focus-box-shadow: 1px 1px 8px 1px var(--orange-12);
|
|
13
12
|
|
|
14
13
|
--theme-form-checkbox-bg: light-dark(var(--gray-1), var(--gray-4));
|
|
@@ -110,7 +110,7 @@ const isChecked = computed(() => {
|
|
|
110
110
|
--_checkbox-size: initial;
|
|
111
111
|
--_checkbox-border-radius: 4px;
|
|
112
112
|
--_outline-width: var(--input-outline-width-thin);
|
|
113
|
-
--_border-width: var(--input-border-width-
|
|
113
|
+
--_border-width: var(--input-border-width-default); /* --input-border-width-default / 2px */
|
|
114
114
|
|
|
115
115
|
display: grid;
|
|
116
116
|
grid-template-areas: 'element-stack';
|
|
@@ -110,7 +110,7 @@ onMounted(() => {
|
|
|
110
110
|
<style lang="css">
|
|
111
111
|
.input-number-wrapper {
|
|
112
112
|
--_gutter: 12px;
|
|
113
|
-
--_border-width: var(--input-border-width-
|
|
113
|
+
--_border-width: var(--input-border-width-default);
|
|
114
114
|
--_min-width: v-bind(minLength);
|
|
115
115
|
|
|
116
116
|
display: flex;
|
|
@@ -111,7 +111,7 @@ const isChecked = computed(() => {
|
|
|
111
111
|
.input-radiobutton-wrapper {
|
|
112
112
|
--_checkbox-size: initial;
|
|
113
113
|
--_outline-width: var(--input-outline-width-thin);
|
|
114
|
-
--_border-width: var(--input-border-width-
|
|
114
|
+
--_border-width: var(--input-border-width-default);
|
|
115
115
|
|
|
116
116
|
display: grid;
|
|
117
117
|
grid-template-areas: 'element-stack';
|
|
@@ -107,7 +107,7 @@ const modelValue = defineModel<number | readonly number[]>();
|
|
|
107
107
|
<style lang="css">
|
|
108
108
|
.input-range-wrapper {
|
|
109
109
|
--_gutter: 12px;
|
|
110
|
-
--_border-width: var(--input-border-width-
|
|
110
|
+
--_border-width: var(--input-border-width-default);
|
|
111
111
|
--_outline-width: var(--input-outline-width-thin);
|
|
112
112
|
|
|
113
113
|
--_input-range-height: 24px;
|
|
@@ -137,8 +137,8 @@ onMounted(() => {
|
|
|
137
137
|
<style lang="css">
|
|
138
138
|
.input-text-wrapper {
|
|
139
139
|
--_gutter: 12px;
|
|
140
|
-
--_border-width: var(--input-border-width-
|
|
141
|
-
--_outline-width: var(--input-border-width-
|
|
140
|
+
--_border-width: var(--input-border-width-default);
|
|
141
|
+
--_outline-width: var(--input-border-width-default);
|
|
142
142
|
|
|
143
143
|
display: flex;
|
|
144
144
|
align-items: center;
|
|
@@ -200,14 +200,15 @@ onMounted(() => {
|
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
input:autofill,
|
|
203
|
+
input:-internal-autofill-selected,
|
|
203
204
|
input:-webkit-autofill-strong-password,
|
|
204
205
|
input:-webkit-autofill-strong-password-viewable,
|
|
205
206
|
input:-webkit-autofill-and-obscured {
|
|
206
207
|
background-color: var(--theme-form-input-bg) !important;
|
|
207
208
|
background-image: none !important;
|
|
208
|
-
color: var(--
|
|
209
|
+
color: var(--theme-form-input-text) !important;
|
|
209
210
|
-webkit-box-shadow: 0 0 0px 1000px var(--theme-form-input-bg) inset;
|
|
210
|
-
|
|
211
|
+
-webkit-text-fill-color: var(--theme-form-input-text);
|
|
211
212
|
transition: background-color 5000s ease-in-out 0s;
|
|
212
213
|
}
|
|
213
214
|
</style>
|
|
@@ -97,8 +97,8 @@ onMounted(() => {
|
|
|
97
97
|
<style lang="css">
|
|
98
98
|
.input-textarea-wrapper {
|
|
99
99
|
--_gutter: 12px;
|
|
100
|
-
--_border-width: var(--input-border-width-
|
|
101
|
-
--_outline-width: var(--input-border-width-
|
|
100
|
+
--_border-width: var(--input-border-width-default);
|
|
101
|
+
--_outline-width: var(--input-border-width-default);
|
|
102
102
|
|
|
103
103
|
display: flex;
|
|
104
104
|
align-items: center;
|
package/nuxt.config.ts
CHANGED
|
@@ -4,40 +4,13 @@ const { resolve } = createResolver(import.meta.url);
|
|
|
4
4
|
|
|
5
5
|
export default defineNuxtConfig({
|
|
6
6
|
devtools: { enabled: true },
|
|
7
|
-
|
|
8
|
-
app: {
|
|
9
|
-
head: {
|
|
10
|
-
htmlAttrs: {
|
|
11
|
-
lang: 'en',
|
|
12
|
-
},
|
|
13
|
-
titleTemplate: '%s - Website name',
|
|
14
|
-
meta: [{ charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }],
|
|
15
|
-
},
|
|
16
|
-
pageTransition: {
|
|
17
|
-
name: 'page',
|
|
18
|
-
mode: 'out-in',
|
|
19
|
-
},
|
|
20
|
-
layoutTransition: {
|
|
21
|
-
name: 'layout',
|
|
22
|
-
mode: 'out-in',
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
7
|
css: [resolve('./assets/styles/main.css')],
|
|
26
|
-
|
|
27
|
-
runtimeConfig: {
|
|
28
|
-
public: {
|
|
29
|
-
validatorLocale: 'en-GB',
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
|
|
33
8
|
modules: ['@nuxt/icon'],
|
|
34
|
-
|
|
35
9
|
components: [
|
|
36
10
|
{
|
|
37
11
|
path: './components',
|
|
38
12
|
pathPrefix: false,
|
|
39
13
|
},
|
|
40
14
|
],
|
|
41
|
-
|
|
42
15
|
compatibilityDate: '2024-07-13',
|
|
43
16
|
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "srcdev-nuxt-forms",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"main": "./nuxt.config.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"reinstall": "rm -rf node_modules && npm install",
|
|
8
|
-
"dev": "nuxi dev",
|
|
9
|
-
"build": "nuxt build",
|
|
10
|
-
"generate": "nuxt generate",
|
|
11
|
-
"preview": "nuxt preview",
|
|
8
|
+
"dev": "nuxi dev .playground",
|
|
9
|
+
"build": "nuxt build .playground",
|
|
10
|
+
"generate": "nuxt generate .playground",
|
|
11
|
+
"preview": "nuxt preview .playground",
|
|
12
12
|
"lint": "eslint .",
|
|
13
|
-
"postinstall": "nuxt prepare",
|
|
13
|
+
"postinstall": "nuxt prepare .playground",
|
|
14
14
|
"release": "release-it"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|