1
1
import type { App , PropType , VNodeTypes , Plugin , ExtractPropTypes } from 'vue' ;
2
- import { defineComponent , inject , nextTick } from 'vue' ;
2
+ import { ref , onMounted } from 'vue' ;
3
+ import { defineComponent , nextTick } from 'vue' ;
3
4
import classNames from '../_util/classNames' ;
4
5
import omit from 'omit.js' ;
5
6
import PropTypes from '../_util/vue-types' ;
6
7
import VcMentions from '../vc-mentions' ;
7
8
import { mentionsProps as baseMentionsProps } from '../vc-mentions/src/mentionsProps' ;
8
9
import Spin from '../spin' ;
9
- import BaseMixin from '../_util/BaseMixin' ;
10
- import { defaultConfigProvider } from '../config-provider' ;
11
- import { getOptionProps , getComponent , getSlot } from '../_util/props-util' ;
12
10
import type { RenderEmptyHandler } from '../config-provider/renderEmpty' ;
11
+ import useConfigInject from '../_util/hooks/useConfigInject' ;
13
12
14
13
const { Option } = VcMentions ;
15
14
@@ -79,65 +78,46 @@ export type MentionsProps = Partial<ExtractPropTypes<typeof mentionsProps>>;
79
78
80
79
const Mentions = defineComponent ( {
81
80
name : 'AMentions' ,
82
- mixins : [ BaseMixin ] ,
83
81
inheritAttrs : false ,
84
- Option : { ...Option , name : 'AMentionsOption' } ,
85
- getMentions,
86
82
props : mentionsProps ,
83
+ getMentions,
87
84
emits : [ 'update:value' , 'change' , 'focus' , 'blur' , 'select' ] ,
88
- setup ( ) {
89
- return {
90
- configProvider : inject ( 'configProvider' , defaultConfigProvider ) ,
85
+ setup ( props , { slots, emit, attrs, expose } ) {
86
+ const { prefixCls, renderEmpty, direction } = useConfigInject ( 'mentions' , props ) ;
87
+ const focused = ref ( false ) ;
88
+ const vcMentions = ref ( null ) ;
89
+
90
+ const handleFocus = ( e : FocusEvent ) => {
91
+ focused . value = true ;
92
+ emit ( 'focus' , e ) ;
91
93
} ;
92
- } ,
93
- data ( ) {
94
- return {
95
- focused : false ,
94
+
95
+ const handleBlur = ( e : FocusEvent ) => {
96
+ focused . value = false ;
97
+ emit ( 'blur' , e ) ;
96
98
} ;
97
- } ,
98
- mounted ( ) {
99
- nextTick ( ( ) => {
100
- if ( process . env . NODE_ENV === 'test' ) {
101
- if ( this . autofocus ) {
102
- this . focus ( ) ;
103
- }
104
- }
105
- } ) ;
106
- } ,
107
- methods : {
108
- handleFocus ( e : FocusEvent ) {
109
- this . $emit ( 'focus' , e ) ;
110
- this . setState ( {
111
- focused : true ,
112
- } ) ;
113
- } ,
114
- handleBlur ( e : FocusEvent ) {
115
- this . $emit ( 'blur' , e ) ;
116
- this . setState ( {
117
- focused : false ,
118
- } ) ;
119
- } ,
120
- handleSelect ( ...args : [ MentionsOptionProps , string ] ) {
121
- this . $emit ( 'select' , ...args ) ;
122
- this . setState ( {
123
- focused : true ,
124
- } ) ;
125
- } ,
126
- handleChange ( val : string ) {
127
- this . $emit ( 'update:value' , val ) ;
128
- this . $emit ( 'change' , val ) ;
129
- } ,
130
- getNotFoundContent ( renderEmpty : RenderEmptyHandler ) {
131
- const notFoundContent = getComponent ( this , 'notFoundContent' ) ;
99
+
100
+ const handleSelect = ( ...args : [ MentionsOptionProps , string ] ) => {
101
+ emit ( 'select' , ...args ) ;
102
+ focused . value = true ;
103
+ } ;
104
+
105
+ const handleChange = ( val : string ) => {
106
+ emit ( 'update:value' , val ) ;
107
+ emit ( 'change' , val ) ;
108
+ } ;
109
+
110
+ const getNotFoundContent = ( renderEmpty : RenderEmptyHandler ) => {
111
+ const notFoundContent = props . notFoundContent ;
132
112
if ( notFoundContent !== undefined ) {
133
113
return notFoundContent ;
134
114
}
135
115
136
116
return renderEmpty ( 'Select' ) ;
137
- } ,
138
- getOptions ( ) {
139
- const { loading } = this . $props ;
140
- const children = getSlot ( this ) ;
117
+ } ;
118
+
119
+ const getOptions = ( ) => {
120
+ const { loading } = props ;
141
121
142
122
if ( loading ) {
143
123
return (
@@ -146,71 +126,82 @@ const Mentions = defineComponent({
146
126
</ Option >
147
127
) ;
148
128
}
149
- return children ;
150
- } ,
151
- getFilterOption ( ) {
152
- const { filterOption, loading } = this . $props ;
129
+ return slots . default ?.( ) ;
130
+ } ;
131
+
132
+ const getFilterOption = ( ) => {
133
+ const { filterOption, loading } = props ;
153
134
if ( loading ) {
154
135
return loadingFilterOption ;
155
136
}
156
137
return filterOption ;
157
- } ,
158
- focus ( ) {
159
- ( this . $refs . vcMentions as HTMLTextAreaElement ) . focus ( ) ;
160
- } ,
161
- blur ( ) {
162
- ( this . $refs . vcMentions as HTMLTextAreaElement ) . blur ( ) ;
163
- } ,
164
- } ,
165
- render ( ) {
166
- const { focused } = this . $data ;
167
- const { getPrefixCls, renderEmpty } = this . configProvider ;
168
- const {
169
- prefixCls : customizePrefixCls ,
170
- disabled,
171
- getPopupContainer,
172
- ...restProps
173
- } = getOptionProps ( this ) as any ;
174
- const { class : className , ...otherAttrs } = this . $attrs ;
175
- const prefixCls = getPrefixCls ( 'mentions' , customizePrefixCls ) ;
176
- const otherProps = omit ( restProps , [ 'loading' , 'onUpdate:value' ] ) ;
177
-
178
- const mergedClassName = classNames ( className , {
179
- [ `${ prefixCls } -disabled` ] : disabled ,
180
- [ `${ prefixCls } -focused` ] : focused ,
181
- } ) ;
138
+ } ;
182
139
183
- const mentionsProps = {
184
- prefixCls,
185
- notFoundContent : this . getNotFoundContent ( renderEmpty ) ,
186
- ...otherProps ,
187
- disabled,
188
- filterOption : this . getFilterOption ( ) ,
189
- getPopupContainer,
190
- children : this . getOptions ( ) ,
191
- class : mergedClassName ,
192
- rows : 1 ,
193
- ...otherAttrs ,
194
- onChange : this . handleChange ,
195
- onSelect : this . handleSelect ,
196
- onFocus : this . handleFocus ,
197
- onBlur : this . handleBlur ,
198
- ref : 'vcMentions' ,
140
+ const focus = ( ) => {
141
+ ( vcMentions . value as HTMLTextAreaElement ) . focus ( ) ;
199
142
} ;
143
+ const blur = ( ) => {
144
+ ( vcMentions . value as HTMLTextAreaElement ) . blur ( ) ;
145
+ } ;
146
+
147
+ expose ( { focus, blur } ) ;
148
+
149
+ onMounted ( ( ) => {
150
+ nextTick ( ( ) => {
151
+ if ( process . env . NODE_ENV === 'test' ) {
152
+ if ( props . autofocus ) {
153
+ focus ( ) ;
154
+ }
155
+ }
156
+ } ) ;
157
+ } ) ;
158
+
159
+ return ( ) => {
160
+ const { disabled, getPopupContainer, ...restProps } = props ;
161
+ const { class : className , ...otherAttrs } = attrs ;
162
+ const otherProps = omit ( restProps , [ 'loading' , 'onUpdate:value' , 'prefixCls' ] ) ;
163
+
164
+ const mergedClassName = classNames ( className , {
165
+ [ `${ prefixCls . value } -disabled` ] : disabled ,
166
+ [ `${ prefixCls . value } -focused` ] : focused . value ,
167
+ [ `${ prefixCls } -rtl` ] : direction . value === 'rtl' ,
168
+ } ) ;
200
169
201
- return < VcMentions { ...mentionsProps } /> ;
170
+ const mentionsProps = {
171
+ prefixCls : prefixCls . value ,
172
+ notFoundContent : getNotFoundContent ( renderEmpty . value ) ,
173
+ ...otherProps ,
174
+ disabled,
175
+ direction : direction . value ,
176
+ filterOption : getFilterOption ( ) ,
177
+ getPopupContainer,
178
+ children : getOptions ( ) ,
179
+ class : mergedClassName ,
180
+ rows : 1 ,
181
+ ...otherAttrs ,
182
+ onChange : handleChange ,
183
+ onSelect : handleSelect ,
184
+ onFocus : handleFocus ,
185
+ onBlur : handleBlur ,
186
+ ref : vcMentions ,
187
+ } ;
188
+ return < VcMentions { ...mentionsProps } /> ;
189
+ } ;
202
190
} ,
203
191
} ) ;
204
192
193
+ export const MentionsOption = {
194
+ ...Option ,
195
+ name : 'AMentionsOption' ,
196
+ } ;
197
+
205
198
/* istanbul ignore next */
206
199
Mentions . install = function ( app : App ) {
207
200
app . component ( Mentions . name , Mentions ) ;
208
- app . component ( Mentions . Option . name , Mentions . Option ) ;
201
+ app . component ( MentionsOption . name , MentionsOption ) ;
209
202
return app ;
210
203
} ;
211
204
212
- export const MentionsOption = Mentions . Option ;
213
-
214
205
export default Mentions as typeof Mentions &
215
206
Plugin & {
216
207
readonly Option : typeof Option ;
0 commit comments