Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit 1b4c32b

Browse files
committed
Filter mentions typeahead results based on current text.
1 parent 400ca41 commit 1b4c32b

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

examples/mentions/mentions.html

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,11 @@
1919
const { TypeaheadEditor, normalizeSelectedIndex } = DraftTypeahead;
2020

2121
const people = [
22-
{
23-
name: 'Justin Vaillancourt',
24-
img: ''
25-
},
26-
{
27-
name: 'Ellie Pritts',
28-
img: ''
29-
},
30-
{
31-
name: 'Maxime Santerre',
32-
img: ''
33-
},
34-
{
35-
name: 'Melody Ma',
36-
img: ''
37-
},
38-
{
39-
name: 'Kris Hartvigsen',
40-
img: ''
41-
}
22+
'Justin Vaillancourt',
23+
'Ellie Pritts',
24+
'Maxime Santerre',
25+
'Melody Ma',
26+
'Kris Hartvigsen'
4227
];
4328

4429
const Mentions = ({ left, top, selectedIndex, text }) => {
@@ -47,16 +32,27 @@
4732
left,
4833
top
4934
});
50-
const normalizedIndex = normalizeSelectedIndex(selectedIndex, people.length);
35+
36+
const relevantPeople = people.filter(person => {
37+
const query = text.toLowerCase().replace(/^@/, '');
38+
return person.toLowerCase().startsWith(query);
39+
});
40+
if (!relevantPeople) {
41+
return null;
42+
}
43+
44+
const normalizedIndex = normalizeSelectedIndex(
45+
selectedIndex, relevantPeople.length
46+
);
5147
return (
5248
<ul style={typeaheadStyle}>
53-
{people.map((person, index) => {
49+
{relevantPeople.map((person, index) => {
5450
const style =
5551
index === normalizedIndex ?
5652
styles.selectedPerson : styles.person;
5753
return (
5854
<li style={style}>
59-
{person.name}
55+
{person}
6056
</li>
6157
);
6258
})}

0 commit comments

Comments
 (0)