|
1 | 1 | import type { Ref } from 'vue';
|
2 |
| -import { computed, reactive, watch, nextTick, unref } from 'vue'; |
| 2 | +import { reactive, watch, nextTick, unref, shallowRef } from 'vue'; |
3 | 3 | import cloneDeep from 'lodash-es/cloneDeep';
|
4 | 4 | import intersection from 'lodash-es/intersection';
|
5 | 5 | import isEqual from 'lodash-es/isEqual';
|
@@ -120,30 +120,7 @@ function useForm(
|
120 | 120 | const initialModel = cloneDeep(unref(modelRef));
|
121 | 121 | const validateInfos = reactive<validateInfos>({});
|
122 | 122 |
|
123 |
| - const rulesKeys = computed(() => { |
124 |
| - return rulesRef ? Object.keys(unref(rulesRef)) : []; |
125 |
| - }); |
126 |
| - |
127 |
| - watch( |
128 |
| - rulesKeys, |
129 |
| - () => { |
130 |
| - const newValidateInfos = {}; |
131 |
| - rulesKeys.value.forEach(key => { |
132 |
| - newValidateInfos[key] = validateInfos[key] || { |
133 |
| - autoLink: false, |
134 |
| - required: isRequired(unref(rulesRef)[key]), |
135 |
| - }; |
136 |
| - delete validateInfos[key]; |
137 |
| - }); |
138 |
| - for (const key in validateInfos) { |
139 |
| - if (Object.prototype.hasOwnProperty.call(validateInfos, key)) { |
140 |
| - delete validateInfos[key]; |
141 |
| - } |
142 |
| - } |
143 |
| - Object.assign(validateInfos, newValidateInfos); |
144 |
| - }, |
145 |
| - { immediate: true }, |
146 |
| - ); |
| 123 | + const rulesKeys = shallowRef([]); |
147 | 124 |
|
148 | 125 | const resetFields = (newValues: Props) => {
|
149 | 126 | Object.assign(unref(modelRef), {
|
@@ -350,22 +327,45 @@ function useForm(
|
350 | 327 |
|
351 | 328 | const debounceOptions = options?.debounce;
|
352 | 329 |
|
| 330 | + let first = true; |
353 | 331 | watch(
|
354 |
| - modelRef, |
355 |
| - debounceOptions && debounceOptions.wait |
356 |
| - ? debounce(modelFn, debounceOptions.wait, omit(debounceOptions, ['wait'])) |
357 |
| - : modelFn, |
358 |
| - { immediate: options && !!options.immediate, deep: true }, |
| 332 | + rulesRef, |
| 333 | + () => { |
| 334 | + rulesKeys.value = rulesRef ? Object.keys(unref(rulesRef)) : []; |
| 335 | + if (!first && options && options.validateOnRuleChange) { |
| 336 | + validate(); |
| 337 | + } |
| 338 | + first = false; |
| 339 | + }, |
| 340 | + { deep: true, immediate: true }, |
359 | 341 | );
|
360 | 342 |
|
361 | 343 | watch(
|
362 |
| - rulesRef, |
| 344 | + rulesKeys, |
363 | 345 | () => {
|
364 |
| - if (options && options.validateOnRuleChange) { |
365 |
| - validate(); |
| 346 | + const newValidateInfos = {}; |
| 347 | + rulesKeys.value.forEach(key => { |
| 348 | + newValidateInfos[key] = Object.assign({}, validateInfos[key], { |
| 349 | + autoLink: false, |
| 350 | + required: isRequired(unref(rulesRef)[key]), |
| 351 | + }); |
| 352 | + delete validateInfos[key]; |
| 353 | + }); |
| 354 | + for (const key in validateInfos) { |
| 355 | + if (Object.prototype.hasOwnProperty.call(validateInfos, key)) { |
| 356 | + delete validateInfos[key]; |
| 357 | + } |
366 | 358 | }
|
| 359 | + Object.assign(validateInfos, newValidateInfos); |
367 | 360 | },
|
368 |
| - { deep: true }, |
| 361 | + { immediate: true }, |
| 362 | + ); |
| 363 | + watch( |
| 364 | + modelRef, |
| 365 | + debounceOptions && debounceOptions.wait |
| 366 | + ? debounce(modelFn, debounceOptions.wait, omit(debounceOptions, ['wait'])) |
| 367 | + : modelFn, |
| 368 | + { immediate: options && !!options.immediate, deep: true }, |
369 | 369 | );
|
370 | 370 |
|
371 | 371 | return {
|
|
0 commit comments