-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableTextInputView.js
170 lines (170 loc) · 5.57 KB
/
VariableTextInputView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { requireNativeComponent, StyleSheet, processColor, Platform } from 'react-native';
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { UIManager } from 'react-native';
import { findNodeHandle } from 'react-native';
import { deletKeyBord, getAttributedTextArr } from './Util';
const VariableTextInputView = /*#__PURE__*/forwardRef((props, ref) => {
const [currentHeight, setCurrentHeight] = useState(undefined);
const nativeRef = useRef(null);
const [mention, setMention] = useState('');
const [keyWord, setKeyWord] = useState('');
const [textValue, setTextValue] = useState('');
const _onChange = e => {
const text = e.nativeEvent.text;
setTextValue(text);
if (!!props.mentions && props.mentions.length > 0 && text.length > 0) {
const lastStr = text.slice(-1);
if (props.mentions.includes(lastStr)) {
setMention(lastStr);
props.onMention && props.onMention({
mention: lastStr,
keyWord: ''
});
}
if (!!mention) {
const result = text.split(mention).pop();
const mentionData = {
mention,
keyWord: result || ''
};
setKeyWord(result || '');
props.onMention && props.onMention(mentionData);
}
}
props.onChangeText && props.onChangeText(text);
props.onChange && props.onChange(e);
};
const clearMention = () => {
if (!!mention) {
setMention('');
setKeyWord('');
}
};
const focus = () => {
callNativeMethod('focus');
};
const blur = () => {
callNativeMethod('blur');
clearMention();
};
const callNativeMethod = (methodName, data) => {
const reactTag = findNodeHandle(nativeRef.current);
const Commands = UIManager.getViewManagerConfig('VariableTextInputView').Commands;
const commandId = Commands[methodName];
UIManager.dispatchViewManagerCommand(reactTag, commandId || 0, !!data ? data : []);
};
const insertEmoji = data => {
const sendData = {
...data,
color: processColor(data.color)
};
if (Platform.OS === 'android') {
callNativeMethod('insertEmoji', [sendData]);
} else {
callNativeMethod('insertEmoji', [{
data: [sendData]
}]);
}
};
const insertMentions = data => {
const sendData = {
...data,
color: processColor(data.color)
};
if (Platform.OS === 'ios') {
callNativeMethod('insertMentions', [{
data: sendData
}]);
} else {
callNativeMethod('insertMentions', [sendData]);
}
};
const changeAttributedText = data => {
const sendData = [];
if (data.length > 0) {
data.forEach(item => {
const newItem = {
...item,
color: processColor(item.color || '#000')
};
sendData.push(newItem);
});
}
if (Platform.OS === 'android') {
callNativeMethod('changeAttributedText', sendData);
} else {
callNativeMethod('changeAttributedText', [{
data: sendData
}]);
}
};
const onContentSizeChange = event => {
const {
style
} = props;
const styles = StyleSheet.flatten(style);
if (styles.height === undefined) {
const contentSizeHeight = event.nativeEvent.contentSize.height;
if (!!styles.maxHeight && contentSizeHeight >= styles.maxHeight) {
setCurrentHeight(parseFloat(`${styles.maxHeight}`));
return;
}
if (!!styles.minHeight && contentSizeHeight <= styles.minHeight) {
setCurrentHeight(parseFloat(`${styles.minHeight}`));
return;
}
setCurrentHeight(event.nativeEvent.contentSize.height);
}
};
const insertMentionAndDelateKeyword = data => {
const item = {
type: 2,
...data
};
const str = deletKeyBord(textValue, `${mention}${keyWord}`);
const arr = getAttributedTextArr(str, props.emojiData);
const newAttArr = [...arr, item];
changeAttributedText(newAttArr);
clearMention();
};
useImperativeHandle(ref, () => {
return {
focus: focus,
blur: blur,
insertEmoji: insertEmoji,
insertMentions: insertMentions,
changeAttributedText: changeAttributedText,
insertMentionAndDelateKeyword: insertMentionAndDelateKeyword
};
});
const _onSubmitEditing = e => {
props.onSubmitEditing && props.onSubmitEditing(e.nativeEvent.text);
};
const onAndroidSubmitEditing = e => {
props.onSubmitEditing && props.onSubmitEditing(e.nativeEvent.text);
};
const onAndroidTextInput = e => {
props.onTextInput && props.onTextInput(e);
};
const style = StyleSheet.flatten([props.style, {
height: currentHeight
}]);
return /*#__PURE__*/React.createElement(RNTVariableTextInputView, _extends({
ref: nativeRef,
onChange: _onChange,
onContentSizeChange: onContentSizeChange,
onAndroidChange: _onChange,
onAndroidContentSizeChange: onContentSizeChange
}, props, {
onSubmitEditing: _onSubmitEditing,
onAndroidSubmitEditing: onAndroidSubmitEditing,
onAndroidTextInput: onAndroidTextInput,
onAndroidBlur: props.onBlur,
onAndroidFocus: props.onFocus,
style: style
}));
});
const RNTVariableTextInputView = requireNativeComponent('VariableTextInputView');
export { VariableTextInputView };
//# sourceMappingURL=VariableTextInputView.js.map