11import type { PropType , ExtractPropTypes , InjectionKey , SetupContext , Ref } from 'vue' ;
2- const defaultFormatter = ( item : any ) => ( item ? item . label || item . toString ( ) : '' ) ;
3- const defaultValueParse = ( item : any ) => item ;
2+ export interface SourceItemObj {
3+ label : string ;
4+ disabled : boolean ;
5+ [ propName : string ] : unknown ;
6+ }
7+ const defaultFormatter = ( item : string | SourceItemObj ) => {
8+ if ( typeof item === 'string' ) {
9+ return item ;
10+ }
11+ return item !== null ? item . label || item . toString ( ) : '' ;
12+ } ;
13+ const defaultValueParse = ( item : string | SourceItemObj ) => item ;
414export type Placement =
515 | 'top'
616 | 'right'
@@ -14,122 +24,132 @@ export type Placement =
1424 | 'bottom-end'
1525 | 'left-start'
1626 | 'left-end' ;
27+
28+
29+ export type SourceType = Array < string > | Array < SourceItemObj > ;
30+
1731export const autoCompleteProps = {
1832 modelValue : {
1933 type : String ,
20- default :''
34+ default : ''
2135 } ,
22- source :{
23- type :Array ,
24- default :null
36+ source : {
37+ type : Array as PropType < SourceType > ,
38+ default : null
2539 } ,
26- allowEmptyValueSearch :{
27- type :Boolean ,
28- default :false
40+ allowEmptyValueSearch : {
41+ type : Boolean ,
42+ default : false
2943 } ,
30- position :{
44+ appendToBody :{
45+ type : Boolean ,
46+ default : false
47+ } ,
48+ position : {
3149 type : Array as PropType < Array < Placement > > ,
3250 default : [ 'bottom-end' ] ,
3351 } ,
34- disabled :{
35- type :Boolean ,
36- default :false
52+ disabled : {
53+ type : Boolean ,
54+ default : false
3755 } ,
38- delay :{
39- type :Number ,
40- default :300
56+ delay : {
57+ type : Number ,
58+ default : 300
4159 } ,
42- disabledKey :{
43- type :String ,
44- default :null
60+ disabledKey : {
61+ type : String ,
62+ default : null
4563 } ,
4664 formatter : {
47- type :Function as PropType < ( item : any ) => string > ,
48- default :defaultFormatter
65+ type : Function as PropType < ( item : string | SourceItemObj ) => string > ,
66+ default : defaultFormatter
4967 } ,
5068 isSearching : {
51- type :Boolean ,
52- default :false
69+ type : Boolean ,
70+ default : false
5371 } ,
54- sceneType :{
55- type :String ,
56- default :null
72+ sceneType : {
73+ type : String ,
74+ default : null
5775 } ,
58- searchFn :{
59- type :Function as PropType < ( term : string ) => Array < any > > ,
60- default :null
76+ searchFn : {
77+ type : Function as PropType < ( term : string ) => SourceType > ,
78+ default : null
6179 } ,
62- tipsText :{
63- type :String ,
80+ tipsText : {
81+ type : String ,
6482 default :'最近输入'
6583 } ,
66- latestSource :{
67- type :Array ,
68- default :null
84+ latestSource : {
85+ type : Array ,
86+ default : null
6987 } ,
70- valueParser :{
71- type :Function as PropType < ( item : any ) => any > ,
72- default :defaultValueParse
88+ valueParser : {
89+ type : Function as PropType < ( item : string | SourceItemObj ) => string > ,
90+ default : defaultValueParse
7391 } ,
7492 enableLazyLoad : {
75- type :Boolean ,
76- default :false
93+ type : Boolean ,
94+ default : false
7795 } ,
78- width :{
96+ width : {
7997 type : Number ,
80- default :400
98+ default : 400
8199 } ,
82- showAnimation :{
83- type :Boolean ,
84- default :true
100+ showAnimation : {
101+ type : Boolean ,
102+ default : true
85103 } ,
86- maxHeight :{
87- type :Number ,
88- default :300
104+ maxHeight : {
105+ type : Number ,
106+ default : 300
89107 } ,
90- transInputFocusEmit :{
91- type :Function as PropType < ( ) => void > ,
92- default :null
108+ transInputFocusEmit : {
109+ type : Function as PropType < ( ) => void > ,
110+ default : null
93111 } ,
94112 selectValue :{
95- type :Function as PropType < ( val : any ) => any > ,
96- default :null
113+ type : Function as PropType < ( val : string ) => string > ,
114+ default : null
97115 } ,
98- loadMore :{
99- type :Function as PropType < ( ) => void > ,
100- default :null
116+ loadMore : {
117+ type : Function as PropType < ( ) => void > ,
118+ default : null
101119 }
102120} as const ;
103121
104122export type AutoCompleteProps = ExtractPropTypes < typeof autoCompleteProps > ;
105123
106124export interface AutoCompleteRootType {
107- ctx : SetupContext < any > ;
125+ ctx : SetupContext ;
108126 props : AutoCompleteProps ;
109127}
110- export type SearchFnType = ( term : string ) => Array < any > ;
111- export type FormatterType = ( item : any ) => string ;
112- export type DefaultFuncType = ( arg ?: any ) => any ;
113- export type HandleSearch = ( term ?: string | string , enableLazyLoad ?: boolean ) => void ;
114- export type RecentlyFocus = ( latestSource : Array < any > ) => void ;
115- export type InputDebounceCb = ( ...rest : any ) => Promise < void > ;
116- export type TransInputFocusEmit = ( any ?: any ) => void ;
117- export type SelectOptionClick = ( any ?: any ) => void ;
128+ export type SearchFnType = ( term : string ) => SourceType ;
129+ export type FormatterType = ( item : string | SourceItemObj ) => string ;
130+ export type DefaultFuncType = ( ) => void ;
131+ export type HandleSearch = ( term : string , enableLazyLoad ?: boolean ) => void ;
132+ export type RecentlyFocus = ( latestSource : SourceType ) => void ;
133+ export type InputDebounceCb = ( value : string ) => void ;
134+ export type TransInputFocusEmit = ( ) => unknown ;
135+ export type SelectOptionClick = ( item : string | SourceItemObj ) => void ;
136+ export type SelectValueType = ( value : string ) => unknown ;
118137// 弹出选择框参数
119138export type DropdownProps = {
120139 props : AutoCompleteProps ;
121- searchList : Ref < any [ ] > ;
140+ searchList : Ref < SourceType > ;
122141 searchStatus ?: Ref < boolean > ;
123142 showNoResultItemTemplate : Ref < boolean > ;
124143 term ?: string ;
125144 visible : Ref < boolean > ;
126145 selectedIndex : Ref < number > ;
127- selectOptionClick : HandleSearch ;
128- dropDownRef : Ref < HTMLUListElement > ;
146+ selectOptionClick : SelectOptionClick ;
147+ dropDownRef : Ref ;
129148 showLoading : Ref < boolean > ;
130- loadMore : ( arg ?: any ) => void ;
131- latestSource : Ref < any [ ] > ;
149+ loadMore : ( ) => void ;
150+ latestSource : Ref < Array < SourceItemObj > > ;
132151 modelValue : Ref < string > ;
133152 hoverIndex : Ref < number > ;
153+ valueParser : ( ) => void ;
134154} ;
135155export const DropdownPropsKey : InjectionKey < DropdownProps > = Symbol ( 'DropdownPropsKey' ) ;
0 commit comments