-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMentionsInputWrapper.jsx
More file actions
153 lines (139 loc) · 4.77 KB
/
MentionsInputWrapper.jsx
File metadata and controls
153 lines (139 loc) · 4.77 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
import React, { forwardRef, useContext, useState } from "react";
import { ScrollView, StyleSheet } from "react-native";
import PropTypes from "prop-types";
import { MentionInput } from "react-native-controlled-mentions";
import { moderateScale } from "react-native-size-matters";
import { ThemeContext } from "styled-components/native";
import { Avatar } from "../Avatar";
import { Card } from "../Card";
import { Container } from "../Container";
import { Divider } from "../Divider";
import { Touchable } from "../Touchable";
import { Typography } from "../Typography";
export const MentionsInputWrapper = forwardRef(
({ suggestions, shouldShowSuggestions, ...rest }, ref) => {
const [visibleMentionsCount, setVisibleMentionsCount] = useState(0);
const theme = useContext(ThemeContext);
const heightOfMention = moderateScale(50);
const mentionsListHeight = visibleMentionsCount * heightOfMention;
const renderSuggestions = ({ keyword, onSuggestionPress }) => {
if (!shouldShowSuggestions) return null;
if (keyword === null) {
if (visibleMentionsCount !== 0) setVisibleMentionsCount(0);
return null;
}
const filteredSuggestions = suggestions.filter(suggestion =>
suggestion.name
.toLocaleLowerCase()
.includes(keyword?.toLocaleLowerCase())
);
setVisibleMentionsCount(
filteredSuggestions.length > 4 ? 4 : filteredSuggestions.length
);
return (
<Card elevation={4}>
<ScrollView
contentInset={{ bottom: moderateScale(20) }}
keyboardShouldPersistTaps="always"
contentContainerStyle={{
...(filteredSuggestions.length === 0
? { height: 0 }
: {
height: mentionsListHeight,
justifyContent: "flex-end",
borderRadius: moderateScale(10),
backgroundColor: theme.colors.background.grey100,
}),
}}
>
{filteredSuggestions.map((suggestion, index) => {
const { name, imageUrl } = suggestion;
return (
<Touchable
bg="background.grey100"
width="100%"
{...(index === 0 && {
borderTopLeftRadius: moderateScale(10),
borderTopRightRadius: moderateScale(10),
})}
{...(index === filteredSuggestions.length - 1 && {
borderBottomLeftRadius: moderateScale(10),
borderBottomRightRadius: moderateScale(10),
})}
height={heightOfMention}
key={index}
px={moderateScale(10)}
onPress={() => onSuggestionPress(suggestion)}
>
<Container alignItems="center" flex={1} flexDirection="row">
<Avatar
imageUrl={imageUrl}
mr={moderateScale(8)}
name={name}
variant="extra-small"
/>
<Typography
color="font.primary"
flex={1}
fontFamily="sf400"
fontSize="xs"
>
{name}
</Typography>
</Container>
{index !== filteredSuggestions.length - 1 && (
<Divider bg="border.primary" width="100%" />
)}
</Touchable>
);
})}
</ScrollView>
</Card>
);
};
return (
<MentionInput
inputRef={ref}
placeholderTextColor={theme.colors.font.grey400}
style={{ color: theme.colors.font.grey800 }}
suggestions={suggestions}
containerStyle={[
styles.mentionsTextInputStyle,
{
minHeight: moderateScale(60) + mentionsListHeight,
},
]}
partTypes={[
{
isInsertSpaceAfterMention: true,
allowedSpacesCount: 0,
trigger: "@",
renderSuggestions,
textStyle: {
fontFamily: theme.fonts.sf700,
color: theme.colors.font.base,
},
},
]}
{...rest}
/>
);
}
);
MentionsInputWrapper.displayName = "MentionsInputWrapper";
const styles = StyleSheet.create({
mentionsTextInputStyle: {
marginVertical: moderateScale(10),
flex: 1,
width: "100%",
},
});
MentionsInputWrapper.propTypes = {
shouldShowSuggestions: PropTypes.bool,
suggestions: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
imageUrl: PropTypes.string,
})
),
};