9
9
*/
10
10
11
11
import * as React from 'react' ;
12
+ import { useRef } from 'react' ;
12
13
import KeyCode from 'rc-util/lib/KeyCode' ;
13
14
import MultipleSelector from './MultipleSelector' ;
14
15
import SingleSelector from './SingleSelector' ;
@@ -72,6 +73,9 @@ export interface SelectorProps {
72
73
maxTagPlaceholder ?: React . ReactNode | ( ( omittedValues : LabelValueType [ ] ) => React . ReactNode ) ;
73
74
tagRender ?: ( props : CustomTagProps ) => React . ReactElement ;
74
75
76
+ /** Check if `tokenSeparators` contains `\n` */
77
+ tokenWithEnter ?: boolean ;
78
+
75
79
// Motion
76
80
choiceTransitionName ?: string ;
77
81
@@ -90,15 +94,16 @@ export interface SelectorProps {
90
94
}
91
95
92
96
const Selector : React . RefForwardingComponent < RefSelectorProps , SelectorProps > = ( props , ref ) => {
93
- const inputRef = React . useRef < HTMLInputElement > ( null ) ;
94
- const compositionStatusRef = React . useRef < boolean > ( false ) ;
97
+ const inputRef = useRef < HTMLInputElement > ( null ) ;
98
+ const compositionStatusRef = useRef < boolean > ( false ) ;
95
99
96
100
const {
97
101
prefixCls,
98
102
multiple,
99
103
open,
100
104
mode,
101
105
showSearch,
106
+ tokenWithEnter,
102
107
103
108
onSearch,
104
109
onSearchSubmit,
@@ -152,7 +157,7 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
152
157
} ;
153
158
154
159
// When paste come, ignore next onChange
155
- const pasteClearRef = React . useRef ( false ) ;
160
+ const pastedTextRef = useRef < string > ( null ) ;
156
161
157
162
const triggerOnSearch = ( value : string ) => {
158
163
if ( onSearch ( value , true , compositionStatusRef . current ) !== false ) {
@@ -168,30 +173,27 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
168
173
compositionStatusRef . current = false ;
169
174
} ;
170
175
171
- const onInputChange = ( { target : { value } } ) => {
172
- if ( pasteClearRef . current ) {
173
- pasteClearRef . current = false ;
174
- return ;
176
+ const onInputChange : React . ChangeEventHandler < HTMLInputElement > = event => {
177
+ let {
178
+ target : { value } ,
179
+ } = event ;
180
+
181
+ // Pasted text should replace back to origin content
182
+ if ( tokenWithEnter && pastedTextRef . current && / [ \r \n ] / . test ( pastedTextRef . current ) ) {
183
+ const replacedText = pastedTextRef . current . replace ( / [ \r \n ] / g, ' ' ) ;
184
+ value = value . replace ( replacedText , pastedTextRef . current ) ;
175
185
}
176
186
187
+ pastedTextRef . current = null ;
188
+
177
189
triggerOnSearch ( value ) ;
178
190
} ;
179
191
180
192
const onInputPaste : React . ClipboardEventHandler = e => {
181
- // github.com/ant-design/ant-design/issues/24461
182
- if ( ( e . target as HTMLInputElement ) . value ) {
183
- return ;
184
- }
185
193
const { clipboardData } = e ;
186
194
const value = clipboardData . getData ( 'text' ) ;
187
195
188
- // Block next onChange
189
- pasteClearRef . current = true ;
190
- setTimeout ( ( ) => {
191
- pasteClearRef . current = false ;
192
- } ) ;
193
-
194
- triggerOnSearch ( value ) ;
196
+ pastedTextRef . current = value ;
195
197
} ;
196
198
197
199
// ====================== Focus ======================
0 commit comments