Skip to content

Commit 6ede21d

Browse files
committed
🐛 fix issue #3 prop + use handler instead of setupValuesWith
1 parent 0406cbf commit 6ede21d

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/FormContainer.vue

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import { isValid, validate, validateObject } from 'valite';
3-
import { definePropertyAcessors, defineReadOnlyProperty } from './object';
3+
import { definePropertyAcessors, defineReadOnlyProperty, cloneOnlyKeys } from './object';
44
import { isType, isEveryProperty, isNotEmptyString, isObject } from './predicates';
55
66
/**
@@ -32,22 +32,27 @@
3232
data () {
3333
return {
3434
ticks: 0,
35+
isInitialized: false,
3536
errors: Object.create(null),
3637
values: Object.create(null)
3738
};
3839
},
3940
4041
watch: {
41-
/**
42-
* When schema is changed it updates values with it.
43-
* @param {object} schema
44-
*/
4542
schema: {
43+
/**
44+
* On schema changes it update `values` state. It merges only keys in
45+
* schema and outdated `values`, or `initial` if FormContainer is
46+
* not initialized.
47+
* @param {object} schema
48+
*/
49+
handler (schema) {
50+
const values = this.isInitialized ? this.values : this.initial;
51+
if (this.isInitialized)
52+
this.isInitialized = true;
53+
this.values = Object.assign(cloneOnlyKeys(schema), values);
54+
},
4655
immediate: true,
47-
handler (schema = this.schema) {
48-
const values = this.values || this.initial;
49-
this.setupValuesWith(schema, values);
50-
}
5156
}
5257
},
5358
@@ -90,19 +95,6 @@
9095
},
9196
9297
methods: {
93-
/**
94-
* Setups state using an schema and value objects.
95-
* @param {object} schema
96-
* @param {object} values
97-
*/
98-
setupValuesWith (schema, values) {
99-
const keys = Object.keys(Object.assign({}, schema, values));
100-
this.values = keys.reduce((state, key) => {
101-
state[key] = values[key] || undefined;
102-
return state;
103-
}, Object.create(null));
104-
},
105-
10698
/**
10799
* Updates a field and trigger its validation.
108100
* @param {string} field Field's name.

src/object.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* Creates a clone of object only with keys, without values.
3+
* @param {object} object
4+
* @returns {object}
5+
*/
6+
export const cloneOnlyKeys = (object) => {
7+
const clone = Object.create(null);
8+
Object.keys(object).forEach((key) => clone[key] = undefined);
9+
return clone;
10+
};
11+
112
/**
213
* Defines acessors (get and set) to key on object.
314
* @param {object} object

0 commit comments

Comments
 (0)