|
6 | 6 | reactive, |
7 | 7 | computed, |
8 | 8 | onMounted, |
9 | | - watchEffect, |
10 | 9 | onBeforeUnmount, |
11 | 10 | unref, |
12 | 11 | WatchStopHandle, |
@@ -215,28 +214,38 @@ export function useField<TValue = any>( |
215 | 214 | } |
216 | 215 |
|
217 | 216 | return Object.keys(rulesVal).reduce((acc: string[], rule: string) => { |
218 | | - const deps = extractLocators((normalizedRules as Ref<Record<string, any>>).value[rule]).map( |
219 | | - (dep: any) => dep.__locatorRef |
220 | | - ); |
| 217 | + const deps = extractLocators(rulesVal[rule]).map((dep: any) => dep.__locatorRef); |
| 218 | + |
221 | 219 | acc.push(...deps); |
222 | 220 |
|
223 | 221 | return acc; |
224 | 222 | }, []); |
225 | 223 | }); |
226 | 224 |
|
| 225 | + const dependenciesValues = computed(() => { |
| 226 | + return dependencies.value.reduce((acc, depName) => { |
| 227 | + if (depName in form.values) { |
| 228 | + acc[depName] = form.values[depName]; |
| 229 | + } |
| 230 | + |
| 231 | + return acc; |
| 232 | + }, {} as Record<string, unknown>); |
| 233 | + }); |
| 234 | + |
227 | 235 | // Adds a watcher that runs the validation whenever field dependencies change |
228 | | - watchEffect(() => { |
229 | | - // Skip if no dependencies |
230 | | - if (!dependencies.value.length) { |
| 236 | + watch(dependenciesValues, (deps, oldDeps) => { |
| 237 | + // Skip if no dependencies or if the field wasn't manipulated |
| 238 | + if (!Object.keys(deps).length || !meta.dirty) { |
231 | 239 | return; |
232 | 240 | } |
233 | 241 |
|
234 | | - // For each dependent field, validate it if it was validated before |
235 | | - dependencies.value.forEach(dep => { |
236 | | - if (dep in form.values && meta.dirty) { |
237 | | - return validate(); |
238 | | - } |
| 242 | + const shouldValidate = Object.keys(deps).some(depName => { |
| 243 | + return deps[depName] !== oldDeps[depName]; |
239 | 244 | }); |
| 245 | + |
| 246 | + if (shouldValidate) { |
| 247 | + validate(); |
| 248 | + } |
240 | 249 | }); |
241 | 250 |
|
242 | 251 | return field; |
|
0 commit comments