Skip to content

Commit e42b936

Browse files
committed
feat: update name to checkDependenceForChangedStatus. add new test for it.
1 parent a1d2fcd commit e42b936

File tree

7 files changed

+75
-25
lines changed

7 files changed

+75
-25
lines changed

plugin/classes/Form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default class Form extends EventEmitter implements FormDependence{
130130
/**
131131
* @description Method check name for including in changes.
132132
* */
133-
checkNameForChangedStatus(dependenceName: string) {
133+
checkDependenceForChangedStatus(dependenceName: string) {
134134
return checkNameInObject(this.changes, dependenceName);
135135
}
136136

plugin/classes/Input.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ export default class Input extends EventEmitter {
3838
* @description Boolean value, true if parent form mark current field like changed.
3939
* */
4040
get changed() {
41-
if (!this.name) return false;
42-
if (!this.parentForm) return false;
43-
return this.parentForm.checkNameForChangedStatus(this.name);
41+
if (!this.name) {
42+
return false;
43+
}
44+
if (!this.parentForm) {
45+
return false;
46+
}
47+
return this.parentForm.checkDependenceForChangedStatus(this.name);
4448
}
4549

4650
/**

plugin/hooks/use-input-state.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {onUnmounted, reactive} from "vue";
22
import Input from "../classes/Input";
3+
import debug from "../debug/debug";
34

45
export default function useInputState(name: string, validation: any[] = []) {
56

@@ -26,14 +27,24 @@ function useInputController(input: Input) {
2627
errors: [],
2728
changed: input.changed
2829
})
29-
30+
31+
/**
32+
* @description setTimeout used for wait short time after values will be marked like changed inside Form.
33+
* */
34+
function updateChanged() {
35+
setTimeout(() => {
36+
state.changed = input.changed;
37+
}, 0)
38+
}
39+
3040
const controls = {
3141
change: (v:any) => {
3242
state.value = v;
33-
state.changed = true
43+
updateChanged();
3444
},
3545
setValues(v: any) {
3646
state.value = v;
47+
updateChanged();
3748
},
3849
disable: () => {
3950
state.disabled = true;
@@ -67,6 +78,7 @@ function useInputController(input: Input) {
6778
function updateName(name: string) {
6879
off?.();
6980
input.name = name;
81+
if (!input.parentForm) debug.msg(`input:${name}`, `Can't found parent form.`)
7082
off = input.parentForm?.dependInput(name, controls);
7183
}
7284

plugin/widgets/input-field.vue

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<template>
2-
<component
3-
:is="componentItem"
4-
:name="name"
5-
6-
:modelValue="props.name ? state.value : modelValue"
7-
@update:modelValue="handleInput"
8-
9-
:label="props.label"
10-
:disabled="state.disabled"
11-
:errors="state.errors"
12-
:options="parseOptions(options)"
13-
:autofocus="autofocus"
14-
/>
2+
<component
3+
:is="componentItem"
4+
:name="name"
5+
6+
:modelValue="props.name ? state.value : modelValue"
7+
@update:modelValue="handleInput"
8+
9+
:label="props.label"
10+
:disabled="state.disabled"
11+
:errors="state.errors"
12+
:options="parseOptions(options)"
13+
:autofocus="autofocus"
14+
:changed="state.changed"
15+
/>
1516
</template>
1617

1718
<script setup lang="ts">
@@ -90,6 +91,9 @@ function parseOptions(v: typeof props.options) {
9091
9192
function handleInput(v: any) {
9293
input.change(v);
94+
/**
95+
* Events for user interface. You can use both variant.
96+
* */
9397
emits('update:modelValue', v)
9498
emits('input', v)
9599
}

src/pages/test/App.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010
<input-field type = "text" name = "sun" label = "Username" prefix = "MMM" :pretty = "splitPoint"/>
1111
<input-field type = "text" name = "sun" label = "numeric" prefix = "MMM" :modify = "[]" numeric />
1212

13-
13+
<widget-child/>
1414
</div>
1515
</template>
1616

1717
<script setup lang='ts'>
1818
import InputField from "../../../plugin/widgets/input-field.vue";
19-
import {Form} from "../../../plugin";
19+
import {Form, useInputState} from "../../../plugin";
2020
import {onMounted, ref} from "vue";
21+
import WidgetChild from "@/pages/test/widget-child.vue";
2122
2223
const form = new Form();
2324
2425
2526
onMounted(() => setTimeout(() => form.validate(), 1000))
2627
28+
2729
function m1(a: string) {
2830
return a.replace(/\D/,'')
2931
}

src/pages/test/widget-child.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<template>
2-
<p>{{label}}</p>
2+
<p>{{state.state}}</p>
33
</template>
44

55
<script setup lang="ts">
6-
defineProps<{
7-
label: string
8-
}>()
6+
7+
import {useInputState} from "../../../plugin";
8+
9+
const state = useInputState('sun')
10+
11+
window.sun = state.input;
12+
13+
914
</script>
1015

1116
<style scoped>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Form} from "../../plugin";
2+
import Input from "../../plugin/classes/Input";
3+
4+
describe("Check name for changed status", () => {
5+
6+
test("After execute change status of changed input must be true", () => {
7+
const form = new Form();
8+
9+
const input_1 = new Input({name: 'name'});
10+
const input_2 = new Input({name: 'login'});
11+
12+
form.subscribe(input_1);
13+
form.subscribe(input_2);
14+
15+
form.change({
16+
name: "Jenesius"
17+
})
18+
19+
expect(form.checkDependenceForChangedStatus(input_1.name)).toBe(true)
20+
expect(form.checkDependenceForChangedStatus(input_2.name)).toBe(false)
21+
})
22+
23+
})

0 commit comments

Comments
 (0)