1
- import { getPropByPath , isObject , isPromise } from '@/packages/utils/util' ;
2
- import {
3
- computed ,
4
- isVNode ,
5
- PropType ,
6
- provide ,
7
- reactive ,
8
- watch ,
9
- getCurrentInstance ,
10
- type VNodeNormalizedChildren
11
- } from 'vue' ;
1
+ import { getPropByPath , isPromise } from '@/packages/utils/util' ;
2
+ import { computed , PropType , provide , reactive , watch } from 'vue' ;
12
3
import { FormItemRule } from '../formitem/types' ;
13
4
import { ErrorMessage , FormRule , FormRules } from './types' ;
14
5
@@ -28,6 +19,55 @@ export const component = (components: any) => {
28
19
emits : [ 'validate' ] ,
29
20
30
21
setup ( props : any , { emit } : any ) {
22
+ const useChildren = ( ) => {
23
+ const publicChildren : any [ ] = reactive ( [ ] ) ;
24
+ const internalChildren : any [ ] = reactive ( [ ] ) ;
25
+
26
+ const linkChildren = ( value ?: any ) => {
27
+ const link = ( child : any ) => {
28
+ if ( child . proxy ) {
29
+ internalChildren . push ( child ) ;
30
+ publicChildren . push ( child . proxy as any ) ;
31
+ }
32
+ } ;
33
+
34
+ const removeLink = ( child : any ) => {
35
+ if ( child . proxy ) {
36
+ let internalIndex = internalChildren . indexOf ( child ) ;
37
+ if ( internalIndex > - 1 ) {
38
+ internalChildren . splice ( internalIndex , 1 ) ;
39
+ }
40
+
41
+ let publicIndex = publicChildren . indexOf ( child . proxy ) ;
42
+ if ( internalIndex > - 1 ) {
43
+ publicChildren . splice ( publicIndex , 1 ) ;
44
+ }
45
+ }
46
+ } ;
47
+
48
+ provide (
49
+ 'NutFormParent' ,
50
+ Object . assign (
51
+ {
52
+ removeLink,
53
+ link,
54
+ children : publicChildren ,
55
+ internalChildren
56
+ } ,
57
+ value
58
+ )
59
+ ) ;
60
+ } ;
61
+
62
+ return {
63
+ children : publicChildren ,
64
+ linkChildren
65
+ } ;
66
+ } ;
67
+
68
+ const { children, linkChildren } = useChildren ( ) ;
69
+ linkChildren ( { props } ) ;
70
+
31
71
const formErrorTip = computed ( ( ) => reactive < any > ( { } ) ) ;
32
72
provide ( 'formErrorTip' , formErrorTip ) ;
33
73
const clearErrorTips = ( ) => {
@@ -48,39 +88,14 @@ export const component = (components: any) => {
48
88
{ immediate : true }
49
89
) ;
50
90
51
- const findFormItem = ( vNodeChildren : VNodeNormalizedChildren ) => {
91
+ const getTaskFromChildren = ( ) => {
52
92
const task : FormRule [ ] = [ ] ;
53
-
54
- const search = ( vNode : VNodeNormalizedChildren ) => {
55
- if ( isVNode ( vNode ) ) {
56
- const type = ( vNode ?. type as any ) ?. name || vNode ?. type ;
57
- if ( type == 'nut-form-item' || type ?. toString ( ) . endsWith ( 'form-item' ) ) {
58
- task . push ( {
59
- prop : vNode . props ?. [ 'prop' ] ,
60
- rules : vNode . props ?. [ 'rules' ] || [ ]
61
- } ) ;
62
- } else if ( vNode . component ?. subTree ) {
63
- const childSubTree = vNode . component ?. subTree ;
64
- search ( childSubTree . children ) ;
65
- if ( isObject ( childSubTree . children ) && Object . keys ( childSubTree . children ) ) {
66
- // 异步节点获取
67
- if ( ( childSubTree . children as any ) ?. default ) {
68
- search ( ( childSubTree . children as any ) . default ( ) ) ;
69
- }
70
- } else {
71
- search ( childSubTree . children ) ;
72
- }
73
- } else if ( vNode . children ) {
74
- search ( vNode . children ) ;
75
- }
76
- } else if ( Array . isArray ( vNode ) ) {
77
- vNode . forEach ( ( v : any ) => {
78
- search ( v ) ;
79
- } ) ;
80
- }
81
- } ;
82
-
83
- search ( vNodeChildren ) ;
93
+ children . forEach ( ( item ) => {
94
+ task . push ( {
95
+ prop : item ?. [ 'prop' ] ,
96
+ rules : item ?. [ 'rules' ] || [ ]
97
+ } ) ;
98
+ } ) ;
84
99
return task ;
85
100
} ;
86
101
@@ -150,9 +165,6 @@ export const component = (components: any) => {
150
165
return Promise . resolve ( true ) ;
151
166
} ;
152
167
153
- // only setup get
154
- const instance = getCurrentInstance ( ) ! ;
155
-
156
168
/**
157
169
* 校验
158
170
* @param customProp 指定校验,用于用户自定义场景时触发,例如 blur、change 事件
@@ -161,8 +173,7 @@ export const component = (components: any) => {
161
173
const validate = ( customProp = '' ) => {
162
174
return new Promise ( ( resolve , reject ) => {
163
175
try {
164
- // 改用当前组件树subtree
165
- const task = findFormItem ( instance . subTree . children ) ;
176
+ const task = getTaskFromChildren ( ) ;
166
177
167
178
const errors = task . map ( ( item ) => {
168
179
if ( customProp && customProp !== item . prop ) {
0 commit comments