@@ -29,7 +29,14 @@ export default class TreeSelect extends SuperComponent {
29
29
scrollIntoView : null ,
30
30
} ;
31
31
32
- properties = props ;
32
+ properties = {
33
+ ...props ,
34
+ customValue : {
35
+ // 用于自定义选中值,优先级高于value,用于弥补value为[]场景
36
+ type : null ,
37
+ value : null ,
38
+ } ,
39
+ } ;
33
40
34
41
controlledProps = [
35
42
{
@@ -39,7 +46,7 @@ export default class TreeSelect extends SuperComponent {
39
46
] ;
40
47
41
48
observers = {
42
- 'value, options, keys, multiple' ( ) {
49
+ 'value, customValue, options, keys, multiple' ( ) {
43
50
this . buildTreeOptions ( ) ;
44
51
} ,
45
52
} ;
@@ -52,9 +59,8 @@ export default class TreeSelect extends SuperComponent {
52
59
53
60
methods = {
54
61
buildTreeOptions ( ) {
55
- const { options, value, multiple, keys } = this . data ;
62
+ const { options, value, defaultValue , customValue , multiple, keys } = this . data ;
56
63
const treeOptions = [ ] ;
57
- const innerValue = [ ] ;
58
64
59
65
let level = - 1 ;
60
66
let node = { children : options } ;
@@ -68,7 +74,7 @@ export default class TreeSelect extends SuperComponent {
68
74
value : item [ keys ?. value || 'value' ] ,
69
75
children : item . children ,
70
76
} ) ) ;
71
- const thisValue = value ?. [ level ] ;
77
+ const thisValue = customValue ?. [ level ] || value ?. [ level ] ;
72
78
73
79
treeOptions . push ( [ ...list ] ) ;
74
80
@@ -83,33 +89,34 @@ export default class TreeSelect extends SuperComponent {
83
89
84
90
const leafLevel = Math . max ( 0 , level ) ;
85
91
86
- treeOptions ?. forEach ( ( ele , idx ) => {
87
- const v = idx === treeOptions . length - 1 && multiple ? [ ele [ 0 ] . value ] : ele [ 0 ] . value ;
88
- innerValue . push ( value ?. [ idx ] || v ) ;
89
- } ) ;
90
-
91
92
if ( multiple ) {
92
- const finalValue = this . data . value || this . data . defaultValue ;
93
+ const finalValue = customValue || value || defaultValue ;
93
94
if ( finalValue [ leafLevel ] != null && ! Array . isArray ( finalValue [ leafLevel ] ) ) {
94
95
throw TypeError ( '应传入数组类型的 value' ) ;
95
96
}
96
97
}
97
98
98
99
this . setData ( {
99
- innerValue,
100
+ innerValue :
101
+ customValue ||
102
+ treeOptions ?. map ( ( ele , idx ) => {
103
+ const v = idx === treeOptions . length - 1 && multiple ? [ ele [ 0 ] . value ] : ele [ 0 ] . value ;
104
+ return value ?. [ idx ] || v ;
105
+ } ) ,
100
106
leafLevel,
101
107
treeOptions,
102
108
} ) ;
103
109
} ,
104
110
105
111
getScrollIntoView ( status : string ) {
106
- const { value, scrollIntoView } = this . data ;
112
+ const { value, customValue , scrollIntoView } = this . data ;
107
113
if ( status === 'init' ) {
108
- const scrollIntoView = Array . isArray ( value )
109
- ? value . map ( ( item ) => {
114
+ const _value = customValue || value ;
115
+ const scrollIntoView = Array . isArray ( _value )
116
+ ? _value . map ( ( item ) => {
110
117
return Array . isArray ( item ) ? item [ 0 ] : item ;
111
118
} )
112
- : [ value ] ;
119
+ : [ _value ] ;
113
120
this . setData ( {
114
121
scrollIntoView,
115
122
} ) ;
0 commit comments