1- import { defineComponent , inject } from 'vue' ;
1+ import { defineComponent , inject , VNode , PropType } from 'vue' ;
22import classNames from '../_util/classNames' ;
33import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
44import FileOutlined from '@ant-design/icons-vue/FileOutlined' ;
@@ -14,6 +14,46 @@ import { cloneElement } from '../_util/vnode';
1414import { defaultConfigProvider } from '../config-provider' ;
1515
1616const TreeNode = VcTree . TreeNode ;
17+
18+ export interface TreeDataItem {
19+ key ?: string | number ;
20+ title ?: string ;
21+ isLeaf ?: boolean ;
22+ selectable ?: boolean ;
23+ children ?: TreeDataItem [ ] ;
24+ disableCheckbox ?: boolean ;
25+ disabled ?: boolean ;
26+ class ?: string ;
27+ style ?: any ;
28+ checkable ?: boolean ;
29+ icon ?: any ;
30+ slots ?: any ;
31+ switcherIcon ?: any ;
32+ }
33+
34+ interface DefaultEvent {
35+ nativeEvent : MouseEvent ;
36+ node : any ;
37+ }
38+
39+ export interface CheckEvent extends DefaultEvent {
40+ checked : boolean ;
41+ checkedNodes : VNode [ ] ;
42+ checkedNodesPositions : { node : VNode ; pos : string | number } [ ] ;
43+ event : string ;
44+ halfCheckedKeys : ( string | number ) [ ] ;
45+ }
46+
47+ export interface ExpendEvent extends DefaultEvent {
48+ expanded : boolean ;
49+ }
50+
51+ export interface SelectEvent extends DefaultEvent {
52+ event : string ;
53+ selected : boolean ;
54+ selectedNodes : VNode [ ] ;
55+ }
56+
1757function TreeProps ( ) {
1858 return {
1959 showLine : PropTypes . looseBool ,
@@ -32,30 +72,36 @@ function TreeProps() {
3272 /** 默认展开对应树节点 */
3373 defaultExpandParent : PropTypes . looseBool ,
3474 /** 默认展开指定的树节点 */
35- defaultExpandedKeys : PropTypes . array ,
75+ defaultExpandedKeys : PropTypes . arrayOf (
76+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
77+ ) ,
3678 /** (受控)展开指定的树节点 */
37- expandedKeys : PropTypes . array ,
79+ expandedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
3880 /** (受控)选中复选框的树节点 */
3981 checkedKeys : PropTypes . oneOfType ( [
40- PropTypes . array ,
82+ PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
4183 PropTypes . shape ( {
42- checked : PropTypes . array ,
43- halfChecked : PropTypes . array ,
84+ checked : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
85+ halfChecked : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
4486 } ) . loose ,
4587 ] ) ,
4688 /** 默认选中复选框的树节点 */
47- defaultCheckedKeys : PropTypes . array ,
89+ defaultCheckedKeys : PropTypes . arrayOf (
90+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
91+ ) ,
4892 /** (受控)设置选中的树节点 */
49- selectedKeys : PropTypes . array ,
93+ selectedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
5094 /** 默认选中的树节点 */
51- defaultSelectedKeys : PropTypes . array ,
95+ defaultSelectedKeys : PropTypes . arrayOf (
96+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
97+ ) ,
5298 selectable : PropTypes . looseBool ,
5399
54100 /** filter some AntTreeNodes as you need. it should return true */
55101 filterAntTreeNode : PropTypes . func ,
56102 /** 异步加载数据 */
57103 loadData : PropTypes . func ,
58- loadedKeys : PropTypes . array ,
104+ loadedKeys : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
59105 // onLoaded: (loadedKeys: string[], info: { event: 'load', node: AntTreeNode; }) => void,
60106 /** 响应右键点击 */
61107 // onRightClick: (options: AntTreeNodeMouseEvent) => void,
@@ -77,7 +123,9 @@ function TreeProps() {
77123 prefixCls : PropTypes . string ,
78124 filterTreeNode : PropTypes . func ,
79125 openAnimation : PropTypes . any ,
80- treeData : PropTypes . array ,
126+ treeData : {
127+ type : Array as PropType < TreeDataItem [ ] > ,
128+ } ,
81129 /**
82130 * @default {title,key,children}
83131 * 替换treeNode中 title,key,children字段为treeData中对应的字段
@@ -123,7 +171,7 @@ export default defineComponent({
123171 } ,
124172 TreeNode,
125173 methods : {
126- renderSwitcherIcon ( prefixCls : string , switcherIcon , { isLeaf, loading, expanded } ) {
174+ renderSwitcherIcon ( prefixCls : string , switcherIcon : VNode , { isLeaf, loading, expanded } ) {
127175 const { showLine } = this . $props ;
128176 if ( loading ) {
129177 return < LoadingOutlined class = { `${ prefixCls } -switcher-loading-icon` } /> ;
@@ -148,26 +196,19 @@ export default defineComponent({
148196 < CaretDownFilled class = { switcherCls } />
149197 ) ;
150198 } ,
151- updateTreeData ( treeData ) {
199+ updateTreeData ( treeData : TreeDataItem [ ] ) {
152200 const { $slots } = this ;
153201 const defaultFields = { children : 'children' , title : 'title' , key : 'key' } ;
154202 const replaceFields = { ...defaultFields , ...this . $props . replaceFields } ;
155203 return treeData . map ( item => {
156204 const key = item [ replaceFields . key ] ;
157205 const children = item [ replaceFields . children ] ;
158- const { slots = { } , scopedSlots = { } , class : cls , style, ...restProps } = item ;
206+ const { slots = { } , class : cls , style, ...restProps } = item ;
159207 const treeNodeProps = {
160208 ...restProps ,
161- icon : $slots [ scopedSlots . icon ] || $slots [ slots . icon ] || restProps . icon ,
162- switcherIcon :
163- $slots [ scopedSlots . switcherIcon ] ||
164- $slots [ slots . switcherIcon ] ||
165- restProps . switcherIcon ,
166- title :
167- $slots [ scopedSlots . title ] ||
168- $slots [ slots . title ] ||
169- $slots . title ||
170- restProps [ replaceFields . title ] ,
209+ icon : $slots [ slots . icon ] || restProps . icon ,
210+ switcherIcon : $slots [ slots . switcherIcon ] || restProps . switcherIcon ,
211+ title : $slots [ slots . title ] || $slots . title || restProps [ replaceFields . title ] ,
171212 dataRef : item ,
172213 key,
173214 class : cls ,
@@ -179,18 +220,18 @@ export default defineComponent({
179220 return treeNodeProps ;
180221 } ) ;
181222 } ,
182- setTreeRef ( node ) {
223+ setTreeRef ( node : VNode ) {
183224 this . tree = node ;
184225 } ,
185- handleCheck ( checkedObj , eventObj ) {
226+ handleCheck ( checkedObj : ( number | string ) [ ] , eventObj : CheckEvent ) {
186227 this . $emit ( 'update:checkedKeys' , checkedObj ) ;
187228 this . $emit ( 'check' , checkedObj , eventObj ) ;
188229 } ,
189- handleExpand ( expandedKeys , eventObj ) {
230+ handleExpand ( expandedKeys : ( number | string ) [ ] , eventObj : ExpendEvent ) {
190231 this . $emit ( 'update:expandedKeys' , expandedKeys ) ;
191232 this . $emit ( 'expand' , expandedKeys , eventObj ) ;
192233 } ,
193- handleSelect ( selectedKeys : string [ ] , eventObj ) {
234+ handleSelect ( selectedKeys : ( number | string ) [ ] , eventObj : SelectEvent ) {
194235 this . $emit ( 'update:selectedKeys' , selectedKeys ) ;
195236 this . $emit ( 'select' , selectedKeys , eventObj ) ;
196237 } ,
0 commit comments