@@ -6,10 +6,8 @@ import {
6
6
NativeSyntheticEvent ,
7
7
TextInputChangeEventData ,
8
8
NativeModules ,
9
- ImageResolvedAssetSource ,
10
9
StyleSheet ,
11
10
processColor ,
12
- ProcessedColorValue ,
13
11
TextInputContentSizeChangeEventData ,
14
12
Platform ,
15
13
KeyboardTypeOptions ,
@@ -22,44 +20,18 @@ import React, {
22
20
} from 'react' ;
23
21
import { UIManager } from 'react-native' ;
24
22
import { findNodeHandle } from 'react-native' ;
23
+ import type {
24
+ IATTextViewBase ,
25
+ IEmojiData ,
26
+ IInserTextAttachmentItem ,
27
+ IVTTextInputData ,
28
+ IonMentionData ,
29
+ MentionData ,
30
+ PrivateItemData ,
31
+ } from './exTypes' ;
32
+ import { deletKeyBord , getAttributedTextArr } from './Util' ;
25
33
const VariableTextInputViewManager = NativeModules . VariableTextInputViewManager ;
26
- export interface IVTTextInputData {
27
- nativeEvent : {
28
- text : string ;
29
- } ;
30
- }
31
- export enum ITextType {
32
- emoji = 1 ,
33
- normal = 0 ,
34
- tagText = 2 ,
35
- }
36
- interface PrivateItemData {
37
- type : ITextType ;
38
- text ?: string ;
39
- color ?: ProcessedColorValue | null | undefined ;
40
- tag ?: '@' | '#' ;
41
- name ?: string ;
42
- id ?: string ;
43
- img ?: ImageResolvedAssetSource ; //emoji图片
44
- emojiTag ?: string ; //[微笑] //emojitag
45
- }
46
- export interface IInserTextAttachmentItem {
47
- type : ITextType ;
48
- text ?: string ;
49
- color ?: ColorValue ;
50
- tag ?: '@' | '#' ;
51
- name ?: string ;
52
- id ?: string ;
53
- img ?: ImageResolvedAssetSource ; //emoji图片
54
- emojiTag ?: string ; //[微笑] //emojitag
55
- emojiUri ?: string ;
56
- }
57
- export interface IOnTagsType {
58
- tag : string ;
59
- keyWord : string ;
60
- }
61
- interface IProps {
62
- onMention ?: ( ) => void ;
34
+ interface INativeProps {
63
35
style ?: StyleProp < TextStyle > | undefined ;
64
36
placeholder ?: string ;
65
37
placeholderTextColor ?: ColorValue ;
@@ -85,14 +57,28 @@ interface IProps {
85
57
onAndroidSubmitEditing ?: ( text : string ) => void ;
86
58
submitBehavior ?: 'submit' ;
87
59
}
88
- export type IATTextViewBase = {
89
- focus : ( ) => void ;
90
- blur : ( ) => void ;
91
- insertEmoji : ( img : IInserTextAttachmentItem ) => void ;
92
- insertMentions : ( data : IInserTextAttachmentItem ) => void ;
93
- changeAttributedText : ( data : IInserTextAttachmentItem [ ] ) => void ;
94
- dismissTag : ( ) => void ;
95
- } ;
60
+ interface IProps {
61
+ style ?: StyleProp < TextStyle > | undefined ;
62
+ placeholder ?: string ;
63
+ placeholderTextColor ?: ColorValue ;
64
+ keyboardAppearance ?: 'default' | 'light' | 'dark' ; //ios only
65
+ onChange ?: ( e : NativeSyntheticEvent < TextInputChangeEventData > ) => void ;
66
+ onChangeText ?: ( text : string ) => void ;
67
+ maxTextLength ?: number ;
68
+ text ?: string ;
69
+ blurOnSubmit ?: boolean ;
70
+ onTextInput ?: ( event : IVTTextInputData ) => void ;
71
+ onContentSizeChange ?: (
72
+ e : NativeSyntheticEvent < TextInputContentSizeChangeEventData >
73
+ ) => void ;
74
+ underlineColorAndroid ?: ColorValue ;
75
+ keyboardType ?: KeyboardTypeOptions | undefined ;
76
+ onSubmitEditing ?: ( text : string ) => void ;
77
+ submitBehavior ?: 'submit' ;
78
+ emojiData ?: IEmojiData [ ] ;
79
+ mentions ?: string [ ] ; //'@','#'
80
+ onMention ?: ( data : IonMentionData ) => void ;
81
+ }
96
82
export type IATTextViewRef = React . ForwardedRef < IATTextViewBase > ;
97
83
98
84
const VariableTextInputView = forwardRef (
@@ -101,9 +87,45 @@ const VariableTextInputView = forwardRef(
101
87
undefined
102
88
) ;
103
89
const nativeRef = useRef ( null ) ;
90
+ const [ mention , setMention ] = useState < string > ( '' ) ;
91
+ const [ keyWord , setKeyWord ] = useState < string > ( '' ) ;
92
+ const [ textValue , setTextValue ] = useState < string > ( '' ) ;
104
93
const _onChange = ( e : NativeSyntheticEvent < TextInputChangeEventData > ) => {
105
- props . onChangeText && props . onChangeText ( e . nativeEvent . text ) ;
106
- props . onChange && props . onChange ( e ) ;
94
+ const text = e . nativeEvent . text ;
95
+ setTextValue ( text ) ;
96
+ if ( ! ! props . mentions && props . mentions . length > 0 && text . length > 0 ) {
97
+ const lastStr = text . slice ( - 1 ) ;
98
+ if ( props . mentions . includes ( lastStr ) ) {
99
+ setMention ( lastStr ) ;
100
+ props . onMention && props . onMention ( { mention : lastStr , keyWord : '' } ) ;
101
+ }
102
+ if ( ! ! mention ) {
103
+ const result = text . split ( mention ) . pop ( ) ;
104
+ const mentionData : IonMentionData = {
105
+ mention,
106
+ keyWord : result || '' ,
107
+ } ;
108
+ setKeyWord ( result || '' ) ;
109
+ props . onMention && props . onMention ( mentionData ) ;
110
+ }
111
+ props . onChangeText && props . onChangeText ( text ) ;
112
+ props . onChange && props . onChange ( e ) ;
113
+ }
114
+ } ;
115
+ // useEffect(() => {
116
+ // if (!!props.text) {
117
+ // const attStrArr: IInserTextAttachmentItem[] = getAttributedTextArr(
118
+ // props.text,
119
+ // props.emojiData
120
+ // );
121
+ // changeAttributedText(attStrArr);
122
+ // }
123
+ // }, [props.text]);
124
+ const clearMention = ( ) => {
125
+ if ( ! ! mention ) {
126
+ setMention ( '' ) ;
127
+ setKeyWord ( '' ) ;
128
+ }
107
129
} ;
108
130
const focus = ( ) => {
109
131
if ( Platform . OS === 'android' ) {
@@ -118,6 +140,7 @@ const VariableTextInputView = forwardRef(
118
140
} else {
119
141
VariableTextInputViewManager . blur ( ) ;
120
142
}
143
+ clearMention ( ) ;
121
144
} ;
122
145
const callNativeMethod = ( methodName : string , data ?: any ) => {
123
146
const reactTag = findNodeHandle ( nativeRef . current ) ;
@@ -186,28 +209,16 @@ const VariableTextInputView = forwardRef(
186
209
setCurrentHeight ( event . nativeEvent . contentSize . height ) ;
187
210
}
188
211
} ;
189
- const onAndroidContentSizeChange = ( event : any ) => {
190
- const { style } = props ;
191
- const styles = StyleSheet . flatten ( style ) ;
192
- if ( styles . height === undefined ) {
193
- const contentSizeHeight = event . nativeEvent . contentSize . height ;
194
- if ( ! ! styles . maxHeight && contentSizeHeight > styles . maxHeight ) {
195
- setCurrentHeight ( parseFloat ( `${ styles . maxHeight } ` ) ) ;
196
- return ;
197
- }
198
- if ( ! ! styles . minHeight && contentSizeHeight < styles . minHeight ) {
199
- setCurrentHeight ( parseFloat ( `${ styles . minHeight } ` ) ) ;
200
- return ;
201
- }
202
- setCurrentHeight ( event . nativeEvent . contentSize . height ) ;
203
- }
204
- } ;
205
- const dismissTag = ( ) => {
206
- if ( Platform . OS === 'android' ) {
207
- callNativeMethod ( 'dismissTag' ) ;
208
- } else {
209
- VariableTextInputViewManager . dismissTag ( ) ;
210
- }
212
+ const insertMentionAndDelateKeyword = ( data : MentionData ) => {
213
+ const item : IInserTextAttachmentItem = {
214
+ type : 2 ,
215
+ ...data ,
216
+ } ;
217
+ const str = deletKeyBord ( textValue , `${ mention } ${ keyWord } ` ) ;
218
+ const arr = getAttributedTextArr ( str , props . emojiData ) ;
219
+ const newAttArr = [ ...arr , item ] ;
220
+ changeAttributedText ( newAttArr ) ;
221
+ clearMention ( ) ;
211
222
} ;
212
223
useImperativeHandle ( ref , ( ) => {
213
224
return {
@@ -216,15 +227,9 @@ const VariableTextInputView = forwardRef(
216
227
insertEmoji : insertEmoji ,
217
228
insertMentions : insertMentions ,
218
229
changeAttributedText : changeAttributedText ,
219
- dismissTag : dismissTag ,
230
+ insertMentionAndDelateKeyword : insertMentionAndDelateKeyword ,
220
231
} ;
221
232
} ) ;
222
- const onAndroidChange = (
223
- e : NativeSyntheticEvent < TextInputChangeEventData >
224
- ) => {
225
- props . onChangeText && props . onChangeText ( e . nativeEvent . text ) ;
226
- props . onChange && props . onChange ( e ) ;
227
- } ;
228
233
const onAndroidSubmitEditing = ( ) => { } ;
229
234
const onAndroidTextInput = ( e : IVTTextInputData ) => {
230
235
props . onTextInput && props . onTextInput ( e ) ;
@@ -235,8 +240,8 @@ const VariableTextInputView = forwardRef(
235
240
ref = { nativeRef }
236
241
onChange = { _onChange }
237
242
onContentSizeChange = { onContentSizeChange }
238
- onAndroidChange = { onAndroidChange }
239
- onAndroidContentSizeChange = { onAndroidContentSizeChange }
243
+ onAndroidChange = { _onChange }
244
+ onAndroidContentSizeChange = { onContentSizeChange }
240
245
{ ...props }
241
246
onAndroidSubmitEditing = { onAndroidSubmitEditing }
242
247
onAndroidTextInput = { onAndroidTextInput }
@@ -245,7 +250,7 @@ const VariableTextInputView = forwardRef(
245
250
) ;
246
251
}
247
252
) ;
248
- const RNTVariableTextInputView = requireNativeComponent < IProps > (
253
+ const RNTVariableTextInputView = requireNativeComponent < INativeProps > (
249
254
'VariableTextInputView'
250
255
) ;
251
256
export { VariableTextInputView } ;
0 commit comments