-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathMentionOnlyEditor.tsx
More file actions
164 lines (151 loc) · 4.18 KB
/
Copy pathMentionOnlyEditor.tsx
File metadata and controls
164 lines (151 loc) · 4.18 KB
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
import { EnrichedTextInput } from 'react-native-enriched-html';
import type {
EnrichedTextInputInstance,
HtmlStyle,
OnChangeMentionEvent,
} from 'react-native-enriched-html';
import { useMemo, useRef, useState } from 'react';
import { View, StyleSheet, Pressable, Text, Platform } from 'react-native';
type Suggestion = { id: string; name: string };
// On web, a tap outside the editor blurs it and ends the active mention before
// `onPress` fires - so `setMention` would do nothing. Preventing the default
// mousedown keeps focus in the editor. It's a no-op on native, where tapping a
// Pressable never steals focus.
const keepEditorFocused: { onMouseDown: (e: React.MouseEvent) => void } | null =
Platform.OS === 'web'
? { onMouseDown: (e: { preventDefault(): void }) => e.preventDefault() }
: null;
const USERS: Suggestion[] = [
{ id: 'u1', name: 'John Doe' },
{ id: 'u2', name: 'Jane Smith' },
{ id: 'u3', name: 'Alice Johnson' },
{ id: 'u4', name: 'Bob Brown' },
];
const CHANNELS: Suggestion[] = [
{ id: 'c1', name: 'general' },
{ id: 'c2', name: 'engineering' },
{ id: 'c3', name: 'random' },
{ id: 'c4', name: 'announcements' },
];
export default function App() {
const ref = useRef<EnrichedTextInputInstance>(null);
const [indicator, setIndicator] = useState<string | null>(null);
const [query, setQuery] = useState('');
const suggestions = useMemo(() => {
if (indicator === null) return [];
const source = indicator === '#' ? CHANNELS : USERS;
return source.filter(item =>
item.name.toLowerCase().startsWith(query.toLowerCase())
);
}, [indicator, query]);
const openPicker = (nextIndicator: string) => {
setIndicator(nextIndicator);
setQuery('');
};
const closePicker = () => {
setIndicator(null);
setQuery('');
};
const pick = (item: Suggestion) => {
if (indicator === null) return;
// Replaces the "@jo" / "#gen" the user typed with a finished mention.
ref.current?.setMention(indicator, `${indicator}${item.name}`, {
id: item.id,
});
closePicker();
};
const updateQuery = ({ indicator: ind, text }: OnChangeMentionEvent) => {
setIndicator(ind);
setQuery(text);
};
return (
<View>
<EnrichedTextInput
ref={ref}
style={styles.input}
htmlStyle={htmlStyle}
placeholder="Type '@' for people or '#' for channels..."
mentionIndicators={['@', '#']}
onStartMention={openPicker}
onChangeMention={updateQuery}
onEndMention={closePicker}
/>
{suggestions.length > 0 && (
<View style={styles.picker}>
{suggestions.map(item => (
<Pressable
key={item.id}
{...keepEditorFocused}
style={styles.row}
onPress={() => pick(item)}>
<View style={styles.badge}>
<Text style={styles.badgeText}>{indicator}</Text>
</View>
<Text style={styles.rowText}>{item.name}</Text>
</Pressable>
))}
</View>
)}
</View>
);
}
// Each mention kind is styled by its indicator.
const htmlStyle: HtmlStyle = {
mention: {
'@': {
color: '#2b7a4b',
backgroundColor: '#d8f3e3',
textDecorationLine: 'none',
},
'#': {
color: '#2b5f9e',
backgroundColor: '#d8e6f9',
textDecorationLine: 'none',
},
},
};
const styles = StyleSheet.create({
input: {
fontSize: 18,
color: '#232736',
padding: 12,
marginBottom: 192,
borderRadius: 12,
minHeight: 96,
backgroundColor: '#eef0ff',
},
picker: {
position: 'absolute',
top: '100%',
marginTop: -176,
width: '100%',
borderWidth: 1,
borderColor: '#dfe3f5',
borderRadius: 12,
backgroundColor: '#ffffff',
overflow: 'hidden',
},
row: {
flexDirection: 'row',
alignItems: 'center',
gap: 10,
paddingVertical: 8,
paddingHorizontal: 12,
},
badge: {
width: 28,
height: 28,
borderRadius: 14,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#eef0ff',
},
badgeText: {
color: '#5b6bb0',
fontWeight: '600',
},
rowText: {
fontSize: 16,
color: '#232736',
},
});