1
- import { computed , defineComponent , PropType , Slot , watch } from " vue" ;
1
+ import { computed , defineComponent , PropType , Slot , watch , onMounted } from ' vue' ;
2
2
import { NodeKey , TreeNodeOptions } from "./types" ;
3
3
import VirtualCheckbox from '../VirtualCheckbox' ;
4
4
import RenderNode from './render' ;
@@ -8,19 +8,19 @@ export default defineComponent({
8
8
name : 'VirTreeNode' ,
9
9
props : {
10
10
selectedNodes : {
11
- type : Object as PropType < SelectionModel < Required < TreeNodeOptions > > > ,
11
+ type : Array as PropType < Required < TreeNodeOptions > [ ] > ,
12
12
required : true
13
13
} ,
14
- checkedNodes : {
15
- type : Object as PropType < SelectionModel < NodeKey > > ,
14
+ checkedNodeKeys : {
15
+ type : Array as PropType < NodeKey [ ] > ,
16
16
required : true
17
17
} ,
18
18
expandedKeys : {
19
- type : Object as PropType < SelectionModel < NodeKey > > ,
19
+ type : Array as PropType < NodeKey [ ] > ,
20
20
required : true
21
21
} ,
22
22
disabledKeys : {
23
- type : Object as PropType < SelectionModel < NodeKey > > ,
23
+ type : Array as PropType < NodeKey [ ] > ,
24
24
required : true
25
25
} ,
26
26
node : {
@@ -40,45 +40,69 @@ export default defineComponent({
40
40
} ,
41
41
emits : [ 'selectChange' , 'toggleExpand' , 'checkChange' ] ,
42
42
setup ( props , { emit, expose } ) {
43
- /*watch(() => props.checkedNodes, newVal => {
44
- console.log('wat checkedNodes', newVal.selected);
45
- }, {
46
- deep: true,
47
- immediate: true
48
- });*/
49
- const halfChecked = computed ( ( ) => {
50
- let result = false ;
43
+ const getCheckedChildrenSize = ( ) : number => {
44
+ let result = 0 ;
51
45
if ( ! props . checkStrictly && props . node . hasChildren ) {
52
46
const { children } = props . node ;
53
- const checkedChildren = ( children as Required < TreeNodeOptions > [ ] ) ! . filter ( item => props . checkedNodes . isSelected ( item . nodeKey ) ) ;
54
- result = checkedChildren . length > 0 && checkedChildren . length < children ! . length ;
47
+ const checkedChildren = ( children as Required < TreeNodeOptions > [ ] ) ! . filter ( item => props . checkedNodeKeys . includes ( item . nodeKey ) ) ;
48
+ result = checkedChildren . length ;
55
49
}
56
50
return result ;
57
- } ) ;
58
- const textCls = computed ( ( ) => {
59
- let result = 'node-title' ;
60
- if ( props . selectedNodes . isSelected ( props . node ) ) {
61
- result += ' selected' ;
62
- }
63
- if ( props . disabledKeys . isSelected ( props . node . nodeKey ) ) {
64
- result += ' disabled' ;
51
+ }
52
+
53
+ const setCheckedStatus = ( ) => {
54
+ const checkedChildrenSize = getCheckedChildrenSize ( ) ;
55
+ const shouldChecked = checkedChildrenSize > 0 && checkedChildrenSize === props . node . children ! . length ;
56
+ if ( shouldChecked && ! props . checkedNodeKeys . includes ( props . node . nodeKey ) ) {
57
+ handleCheckChange ( shouldChecked ) ;
65
58
}
66
- return result ;
67
- } ) ;
59
+ }
60
+
68
61
const handleSelect = ( event : MouseEvent ) => {
69
62
event . stopPropagation ( ) ;
70
- if ( ! props . disabledKeys . isSelected ( props . node . nodeKey ) ) {
63
+ if ( ! props . disabledKeys . includes ( props . node . nodeKey ) ) {
71
64
emit ( 'selectChange' , props . node ) ;
72
65
}
73
66
}
74
67
const handleExpand = ( ) => {
75
68
emit ( 'toggleExpand' , props . node ) ;
76
69
}
77
70
const handleCheckChange = ( checked : boolean ) => {
78
- emit ( 'checkChange' , [ checked , props . node ] )
71
+ emit ( 'checkChange' , [ checked , props . node ] ) ;
79
72
}
73
+
74
+ watch ( ( ) => props . node , ( ) => {
75
+ setCheckedStatus ( ) ;
76
+ } ) ;
77
+
78
+ watch ( ( ) => props . checkedNodeKeys , newVal => {
79
+ setCheckedStatus ( ) ;
80
+ } ) ;
81
+
82
+ onMounted ( ( ) => {
83
+ setCheckedStatus ( ) ;
84
+ } ) ;
85
+
86
+ const halfChecked = computed ( ( ) => {
87
+ let result = false ;
88
+ const checkedChildrenSize = getCheckedChildrenSize ( ) ;
89
+ result = checkedChildrenSize > 0 && checkedChildrenSize < props . node . children ! . length ;
90
+ return result ;
91
+ } ) ;
92
+
93
+ const textCls = computed ( ( ) => {
94
+ let result = 'node-title' ;
95
+ if ( props . selectedNodes [ 0 ] . nodeKey === props . node . nodeKey ) {
96
+ result += ' selected' ;
97
+ }
98
+ if ( props . disabledKeys . includes ( props . node . nodeKey ) ) {
99
+ result += ' disabled' ;
100
+ }
101
+ return result ;
102
+ } ) ;
103
+
80
104
const renderArrow = ( ) : JSX . Element | null => {
81
- return < div class = { [ 'node-arrow' , props . expandedKeys . isSelected ( props . node . nodeKey ) ? 'expanded' : '' ] } >
105
+ return < div class = { [ 'node-arrow' , props . expandedKeys . includes ( props . node . nodeKey ) ? 'expanded' : '' ] } >
82
106
{
83
107
props . node . hasChildren
84
108
? props . iconSlot ? props . iconSlot ( props . node . loading ) : props . node . loading
@@ -90,12 +114,11 @@ export default defineComponent({
90
114
}
91
115
92
116
const renderContent = ( ) => {
93
- // console.log('checkedNodes>>>>>>', props.checkedNodes);
94
117
if ( props . showCheckbox ) {
95
118
return < VirtualCheckbox
96
119
class = "node-content node-check-box"
97
- disabled = { props . disabledKeys . isSelected ( props . node . nodeKey ) }
98
- modelValue = { props . checkedNodes . isSelected ( props . node . nodeKey ) }
120
+ disabled = { props . disabledKeys . includes ( props . node . nodeKey ) }
121
+ modelValue = { props . checkedNodeKeys . includes ( props . node . nodeKey ) }
99
122
halfChecked = { halfChecked . value }
100
123
// @ts -ignore
101
124
onChange = { handleCheckChange } >
0 commit comments