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

Commit 43c29d7

Browse files
committed
feature(next): added custom-class and value changed event
1 parent 6389778 commit 43c29d7

File tree

7 files changed

+78
-18
lines changed

7 files changed

+78
-18
lines changed

dev/vue/App.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
<script lang="ts">
1818
import { defineComponent, reactive, ref, onMounted, Ref } from 'vue';
19-
import { TextInput, SelectInput, DynamicForm } from '../../src/index';
19+
import {
20+
TextInput,
21+
SelectInput,
22+
DynamicForm,
23+
EmailInput,
24+
} from '../../src/index';
2025
2126
export default defineComponent({
2227
name: 'app',
@@ -29,8 +34,12 @@ export default defineComponent({
2934
label: 'Name',
3035
value: 'Awiwi',
3136
}),
37+
new EmailInput({
38+
label: 'Email',
39+
}),
3240
new SelectInput<string>({
3341
label: 'Games',
42+
customClass: 'w-1/2',
3443
options: [
3544
{
3645
key: 'the-last-of-us',
@@ -52,8 +61,6 @@ export default defineComponent({
5261
setTimeout(() => {
5362
form.fields[0].label = 'RockNRoll';
5463
form.fields[0].value = 'James Bond';
55-
form.fields[0].disabled = true;
56-
form.id = 'awwiwiwiiwiwiwiw';
5764
}, 2000),
5865
);
5966
return {

src/components/dynamic-form/DynamicForm.vue

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
v-for="control in controls"
55
:key="control?.name"
66
:control="control"
7+
@changed="valueChanged"
78
/>
8-
<pre>{{ form }}</pre>
9+
<pre>{{ formValues }}</pre>
910
</form>
1011
</template>
1112

@@ -18,6 +19,7 @@ import {
1819
watchEffect,
1920
ref,
2021
Ref,
22+
computed,
2123
} from 'vue';
2224
import { DynamicForm } from './form';
2325
import DynamicInput from '@/components/dynamic-input/DynamicInput.vue';
@@ -35,16 +37,48 @@ export default defineComponent({
3537
name: 'asDynamicForm',
3638
props,
3739
components,
38-
setup(props) {
40+
setup(props, { emit }) {
3941
const controls: Ref<FormControl<any>[] | undefined> = ref([]);
42+
const formValues = reactive({});
4043
watchEffect(() => {
4144
controls.value =
4245
props.form?.fields?.map(
4346
(field: InputBase<any>) => new FormControl({ ...field }),
4447
) || [];
48+
Object.assign(
49+
formValues,
50+
controls.value
51+
? controls.value.reduce((prev, curr) => {
52+
const obj = {};
53+
obj[curr.name] =
54+
curr.type === 'number' ? parseFloat(curr.value) : curr.value;
55+
return {
56+
...prev,
57+
...obj,
58+
};
59+
}, {})
60+
: {},
61+
);
4562
});
4663
47-
return { controls, form: props.form };
64+
function valueChanged(changedValue: any) {
65+
Object.assign(formValues, changedValue);
66+
}
67+
/* const formValues = computed(() => {
68+
return controls.value
69+
? controls.value.reduce((prev, curr) => {
70+
const obj = {};
71+
obj[curr.name] =
72+
curr.type === 'number' ? parseFloat(curr.value) : curr.value;
73+
return {
74+
...prev,
75+
...obj,
76+
};
77+
}, {})
78+
: {};
79+
}); */
80+
81+
return { controls, form: props.form, valueChanged, formValues };
4882
},
4983
});
5084
</script>

src/components/dynamic-input/DynamicInput.vue

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { defineComponent, h, PropType, ref, toRefs, reactive } from 'vue';
2+
import { defineComponent, h, PropType, computed } from 'vue';
33
import TextInput from '@/components/text-input/TextInput.vue';
44
import SelectInput from '@/components/select-input/SelectInput.vue';
55
@@ -17,22 +17,29 @@ export default defineComponent({
1717
name: 'asDynamicInput',
1818
components,
1919
props,
20-
setup(props) {
20+
setup(props, { emit }) {
2121
let component;
22+
const attributes = computed(() => {
23+
return {
24+
control: props.control,
25+
onChange: function ($event) {
26+
const value = {};
27+
value[$event.target.name] = $event.target.value;
28+
emit('changed', value);
29+
},
30+
};
31+
});
32+
2233
return () => {
2334
switch (props?.control?.type) {
2435
case 'text':
2536
case 'email':
2637
case 'password':
2738
case 'url':
28-
component = h(TextInput, {
29-
control: props.control,
30-
});
39+
component = h(TextInput, attributes.value);
3140
break;
3241
case 'select':
33-
component = h(SelectInput, {
34-
control: props.control,
35-
});
42+
component = h(SelectInput, attributes.value);
3643
break;
3744
3845
default:
@@ -41,7 +48,7 @@ export default defineComponent({
4148
return h(
4249
'div',
4350
{
44-
class: ['dynamic-input', 'form-group'],
51+
class: ['dynamic-input', 'form-group', props?.control?.customClass],
4552
},
4653
[
4754
component,

src/components_old/dynamic-form/DynamicForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const computed = {
125125
return Object.keys(this.$scopedSlots);
126126
},
127127
normalizedControls() {
128-
let controls = {};
128+
const controls = {};
129129
this.controls.forEach(element => {
130130
controls[element.name] = element;
131131
});

src/composables/input-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function useInputEvents(control: FormControl<any> | undefined, emit) {
55
if (control) {
66
control.value = $event.target.value;
77
}
8-
emit('change', control?.value);
8+
emit('changed', $event.target.value);
99
}
1010
function onFocus() {
1111
emit('focus');

src/core/models.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class InputBase<T> {
2525
disabled?: boolean;
2626
order?: number;
2727
type: string;
28+
customClass?: string;
2829
placeholder?: string;
2930
validations?: FormValidation[];
3031
options?: { key: string; value: string; disabled?: boolean }[];
@@ -37,6 +38,7 @@ export class InputBase<T> {
3738
disabled?: boolean;
3839
order?: number;
3940
type?: string;
41+
customClass?: string;
4042
placeholder?: string;
4143
validations?: FormValidation[];
4244
options?: { key: string; value: string; disabled?: boolean }[];
@@ -48,6 +50,7 @@ export class InputBase<T> {
4850
this.disabled = !!options.disabled;
4951
this.order = options.order === undefined ? 1 : options.order;
5052
this.type = options.type || '';
53+
this.customClass = options.customClass;
5154
this.placeholder = options.placeholder;
5255
this.validations = options.validations;
5356
this.options = options.options;
@@ -58,6 +61,14 @@ export class TextInput extends InputBase<string> {
5861
type = 'text';
5962
}
6063

64+
export class EmailInput extends InputBase<string> {
65+
type = 'email';
66+
}
67+
68+
export class PasswordInput extends InputBase<string> {
69+
type = 'password';
70+
}
71+
6172
export class SelectInput<T> extends InputBase<T> {
6273
type = 'select';
6374
}
@@ -76,6 +87,7 @@ export class FormControl<T> extends InputBase<T> {
7687
disabled?: boolean;
7788
order?: number;
7889
type?: string;
90+
customClass?: string;
7991
options?: { key: string; value: string; disabled?: boolean }[];
8092
} = {},
8193
) {
@@ -86,6 +98,7 @@ export class FormControl<T> extends InputBase<T> {
8698
type: options.type,
8799
disabled: !!options.disabled,
88100
order: options.order,
101+
customClass: options.customClass,
89102
});
90103
}
91104
}

src/styles/themes/default.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ $input-error-color: #dc3545 !default;
5252
.form-group {
5353
display: flex;
5454
flex-direction: column;
55-
width: 100%;
5655
margin-bottom: 1rem;
5756

5857
& > div {

0 commit comments

Comments
 (0)