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

Commit 6389778

Browse files
committed
feature(next): achieved reactivity on control mapping
1 parent 3e37f28 commit 6389778

File tree

7 files changed

+77
-22
lines changed

7 files changed

+77
-22
lines changed

dev/vue/App.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</template>
1616

1717
<script lang="ts">
18-
import { defineComponent, reactive, ref } from 'vue';
18+
import { defineComponent, reactive, ref, onMounted, Ref } from 'vue';
1919
import { TextInput, SelectInput, DynamicForm } from '../../src/index';
2020
2121
export default defineComponent({
@@ -27,6 +27,7 @@ export default defineComponent({
2727
fields: [
2828
new TextInput({
2929
label: 'Name',
30+
value: 'Awiwi',
3031
}),
3132
new SelectInput<string>({
3233
label: 'Games',
@@ -47,6 +48,14 @@ export default defineComponent({
4748
}),
4849
],
4950
});
51+
onMounted(() =>
52+
setTimeout(() => {
53+
form.fields[0].label = 'RockNRoll';
54+
form.fields[0].value = 'James Bond';
55+
form.fields[0].disabled = true;
56+
form.id = 'awwiwiwiiwiwiwiw';
57+
}, 2000),
58+
);
5059
return {
5160
title,
5261
form,

src/components/dynamic-form/DynamicForm.vue

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
<template>
2-
<form class="dynamic-form" novalidate>
3-
<!-- <li v-for="control in this.form.fields" :key="control.name">
4-
{{ control.label }}
5-
</li> -->
2+
<form class="dynamic-form" novalidate :id="form.id">
63
<dynamic-input
74
v-for="control in controls"
85
:key="control?.name"
96
:control="control"
107
/>
11-
<pre>{{ controls }}</pre>
8+
<pre>{{ form }}</pre>
129
</form>
1310
</template>
1411

1512
<script lang="ts">
16-
import { defineComponent, PropType } from 'vue';
13+
import {
14+
defineComponent,
15+
PropType,
16+
toRef,
17+
reactive,
18+
watchEffect,
19+
ref,
20+
Ref,
21+
} from 'vue';
1722
import { DynamicForm } from './form';
1823
import DynamicInput from '@/components/dynamic-input/DynamicInput.vue';
1924
import { InputBase, FormControl } from '@/core/models';
@@ -31,12 +36,15 @@ export default defineComponent({
3136
props,
3237
components,
3338
setup(props) {
34-
const controls: FormControl<any>[] =
35-
props.form?.fields?.map(
36-
(field: InputBase<any>) => new FormControl({ ...field }),
37-
) || [];
39+
const controls: Ref<FormControl<any>[] | undefined> = ref([]);
40+
watchEffect(() => {
41+
controls.value =
42+
props.form?.fields?.map(
43+
(field: InputBase<any>) => new FormControl({ ...field }),
44+
) || [];
45+
});
3846
39-
return { controls };
47+
return { controls, form: props.form };
4048
},
4149
});
4250
</script>

src/components/dynamic-input/DynamicInput.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { defineComponent, h, PropType } from 'vue';
2+
import { defineComponent, h, PropType, ref, toRefs, reactive } from 'vue';
33
import TextInput from '@/components/text-input/TextInput.vue';
44
import SelectInput from '@/components/select-input/SelectInput.vue';
55
@@ -43,7 +43,17 @@ export default defineComponent({
4343
{
4444
class: ['dynamic-input', 'form-group'],
4545
},
46-
component,
46+
[
47+
component,
48+
h(
49+
'label',
50+
{
51+
class: 'form-label',
52+
for: props?.control?.label,
53+
},
54+
props?.control?.label,
55+
),
56+
],
4757
);
4858
};
4959
},

src/components/select-input/SelectInput.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const props = {
99
export default defineComponent({
1010
name: 'asSelectInput',
1111
props,
12-
setup(props) {
12+
setup(props, { emit }) {
1313
return () => {
1414
const options = props?.control?.options?.map(({ key, value, disabled }) =>
1515
h('option', { key, value, disabled }, value),
@@ -18,7 +18,6 @@ export default defineComponent({
1818
'select',
1919
{
2020
name: props?.control?.name || '',
21-
type: props?.control?.type,
2221
value: props?.control?.value,
2322
disabled: props?.control?.disabled,
2423
placeholder: props?.control?.placeholder,

src/components/text-input/TextInput.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
<script lang="ts">
22
import { defineComponent, h, PropType } from 'vue';
33
import { FormControl } from '@/core/models';
4-
4+
import { useInputEvents } from '@/composables/input-events';
55
const props = {
66
control: Object as PropType<FormControl<any>>,
77
};
88
99
export default defineComponent({
1010
name: 'asTextInput',
1111
props,
12-
setup(props) {
12+
setup(props, { emit }) {
13+
const { onChange, onFocus, onBlur } = useInputEvents(props?.control, emit);
14+
1315
return () =>
1416
h('input', {
1517
name: props?.control?.name || '',
1618
type: props?.control?.type,
17-
value: props?.control?.value,
1819
disabled: props?.control?.disabled,
1920
placeholder: props?.control?.placeholder,
2021
class: ['form-control'],
22+
value: props?.control?.value,
23+
onFocus,
24+
onBlur,
25+
onChange,
2126
});
2227
},
2328
});

src/composables/input-events.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { FormControl } from '@/core/models';
2+
3+
export function useInputEvents(control: FormControl<any> | undefined, emit) {
4+
function onChange($event) {
5+
if (control) {
6+
control.value = $event.target.value;
7+
}
8+
emit('change', control?.value);
9+
}
10+
function onFocus() {
11+
emit('focus');
12+
}
13+
function onBlur() {
14+
emit('blur');
15+
if (control) {
16+
control.touched = true;
17+
}
18+
}
19+
return {
20+
onFocus,
21+
onChange,
22+
onBlur,
23+
};
24+
}

src/core/models.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class InputBase<T> {
2727
type: string;
2828
placeholder?: string;
2929
validations?: FormValidation[];
30-
options?: { key: string; value: string; disabled: boolean }[];
30+
options?: { key: string; value: string; disabled?: boolean }[];
3131

3232
constructor(
3333
options: {
@@ -39,7 +39,7 @@ export class InputBase<T> {
3939
type?: string;
4040
placeholder?: string;
4141
validations?: FormValidation[];
42-
options?: { key: string; value: string; disabled: boolean }[];
42+
options?: { key: string; value: string; disabled?: boolean }[];
4343
} = {},
4444
) {
4545
this.value = options.value;
@@ -76,7 +76,7 @@ export class FormControl<T> extends InputBase<T> {
7676
disabled?: boolean;
7777
order?: number;
7878
type?: string;
79-
options?: { key: string; value: string; disabled: boolean }[];
79+
options?: { key: string; value: string; disabled?: boolean }[];
8080
} = {},
8181
) {
8282
super({

0 commit comments

Comments
 (0)