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

Commit 2104599

Browse files
committed
feat(form): removed empty fields from change and fix type warnings
1 parent e51bcda commit 2104599

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

dev/typescript/App.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ import {
5353
EmailInput,
5454
FormValidation,
5555
PasswordInput,
56-
TextAreaInput,
5756
CheckboxInput,
5857
RadioInput,
59-
InputBase,
6058
email,
6159
pattern,
6260
ColorInput,

src/components/dynamic-form/DynamicForm.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import DynamicInput from '../dynamic-input/DynamicInput.vue';
5151
5252
import { InputBase, FormControl } from '../../core/models';
5353
import { dynamicFormsSymbol } from '../../useApi';
54+
import { removeEmpty } from '../../core/utils/helpers';
5455
5556
const props = {
5657
form: {
@@ -145,9 +146,9 @@ export default defineComponent({
145146
}
146147
});
147148
148-
function valueChange(changedValue: any) {
149+
function valueChange(changedValue) {
149150
Object.assign(formValues, changedValue);
150-
ctx.emit('changed', formValues);
151+
ctx.emit('changed', removeEmpty(formValues));
151152
}
152153
153154
function mapControls(empty?) {

src/core/models.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ export type InputType =
2525
| UrlInput
2626
| CustomInput;
2727

28+
type ValidationError = {
29+
text: string;
30+
// eslint-disable-next-line
31+
value: any;
32+
};
33+
2834
type ValidationErrors = {
29-
[key: string]: any;
35+
[key: string]: ValidationError;
3036
};
3137

3238
interface ValidatorFn {
33-
(control: FormControl<any> | undefined): ValidationErrors | null;
39+
(control: FormControl | undefined): ValidationErrors | null;
3440
}
3541

3642
export interface FormValidation {
@@ -39,7 +45,7 @@ export interface FormValidation {
3945
}
4046

4147
export interface InputBase {
42-
value: any;
48+
value: boolean | string;
4349
name: string;
4450
label: string;
4551
disabled?: boolean;
@@ -53,10 +59,6 @@ export type TextInput = InputBase & {
5359
type: 'text';
5460
};
5561

56-
/* export interface EmailInput extends InputBase<string> {
57-
type: 'email';
58-
} */
59-
6062
export type NumberInput = InputBase & {
6163
min?: number;
6264
max?: number;
@@ -65,7 +67,7 @@ export type NumberInput = InputBase & {
6567
type: 'number';
6668
};
6769

68-
export type SelectInput<T = any> = InputBase & {
70+
export type SelectInput<T = boolean | string> = InputBase & {
6971
type: 'select';
7072
value: T;
7173
options?: { key: string; value: string; disabled?: boolean }[];

src/core/utils/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable */
12
export const { assign, entries, values, keys } = Object;
23

34
export const slugify = (text: string): string =>
@@ -23,6 +24,17 @@ export const isEmpty = (entry: any) => {
2324
}
2425
};
2526

27+
export const removeEmpty = obj =>
28+
Object.keys(obj)
29+
.filter(k => obj[k] != null) // Remove undef. and null.
30+
.reduce(
31+
(newObj, k) =>
32+
typeof obj[k] === 'object'
33+
? { ...newObj, [k]: removeEmpty(obj[k]) } // Recurse.
34+
: { ...newObj, [k]: obj[k] }, // Copy value.
35+
{},
36+
);
37+
2638
export const mockAsync = (success, timeout, value) => {
2739
return new Promise((resolve, reject) => {
2840
setTimeout(() => {

0 commit comments

Comments
 (0)