|
1 | 1 | <script> |
2 | 2 | import { isValid, validate, validateObject } from 'valite'; |
3 | | - import { definePropertyAcessors, defineReadOnlyProperty } from './object'; |
| 3 | + import { definePropertyAcessors, defineReadOnlyProperty, cloneOnlyKeys } from './object'; |
4 | 4 | import { isType, isEveryProperty, isNotEmptyString, isObject } from './predicates'; |
5 | 5 |
|
6 | 6 | /** |
|
32 | 32 | data () { |
33 | 33 | return { |
34 | 34 | ticks: 0, |
| 35 | + isInitialized: false, |
35 | 36 | errors: Object.create(null), |
36 | 37 | values: Object.create(null) |
37 | 38 | }; |
38 | 39 | }, |
39 | 40 |
|
40 | 41 | watch: { |
41 | | - /** |
42 | | - * When schema is changed it updates values with it. |
43 | | - * @param {object} schema |
44 | | - */ |
45 | 42 | 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 | + }, |
46 | 55 | immediate: true, |
47 | | - handler (schema = this.schema) { |
48 | | - const values = this.values || this.initial; |
49 | | - this.setupValuesWith(schema, values); |
50 | | - } |
51 | 56 | } |
52 | 57 | }, |
53 | 58 |
|
|
90 | 95 | }, |
91 | 96 |
|
92 | 97 | 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 | | -
|
106 | 98 | /** |
107 | 99 | * Updates a field and trigger its validation. |
108 | 100 | * @param {string} field Field's name. |
|
0 commit comments