|
@@ -0,0 +1,203 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import React, { Component } from 'react';
|
|
4
|
+import { Text } from 'react-native';
|
|
5
|
+
|
|
6
|
+import { Icon } from '../../../font-icons';
|
|
7
|
+import { Avatar } from '../../../participants';
|
|
8
|
+import { StyleType } from '../../../styles';
|
|
9
|
+
|
|
10
|
+import { type Item } from '../../Types';
|
|
11
|
+
|
|
12
|
+import Container from './Container';
|
|
13
|
+import styles, { AVATAR_SIZE, UNDERLAY_COLOR } from './styles';
|
|
14
|
+
|
|
15
|
+type Props = {
|
|
16
|
+
|
|
17
|
+ /**
|
|
18
|
+ * Preferred size of the avatar.
|
|
19
|
+ */
|
|
20
|
+ avatarSize?: number,
|
|
21
|
+
|
|
22
|
+ /**
|
|
23
|
+ * External style to be applied to the avatar (icon).
|
|
24
|
+ */
|
|
25
|
+ avatarStyle?: StyleType,
|
|
26
|
+
|
|
27
|
+ /**
|
|
28
|
+ * External style to be applied to the avatar (text).
|
|
29
|
+ */
|
|
30
|
+ avatarTextStyle?: StyleType,
|
|
31
|
+
|
|
32
|
+ /**
|
|
33
|
+ * Children of the component.
|
|
34
|
+ */
|
|
35
|
+ children?: ?React$Element<*>,
|
|
36
|
+
|
|
37
|
+ /**
|
|
38
|
+ * item containing data to be rendered
|
|
39
|
+ */
|
|
40
|
+ item: Item,
|
|
41
|
+
|
|
42
|
+ /**
|
|
43
|
+ * External style prop to be applied to the extra lines.
|
|
44
|
+ */
|
|
45
|
+ linesStyle?: StyleType,
|
|
46
|
+
|
|
47
|
+ /**
|
|
48
|
+ * Function to invoke on press.
|
|
49
|
+ */
|
|
50
|
+ onPress: ?Function,
|
|
51
|
+
|
|
52
|
+ /**
|
|
53
|
+ * External style prop to be applied to the title.
|
|
54
|
+ */
|
|
55
|
+ titleStyle?: StyleType
|
|
56
|
+};
|
|
57
|
+
|
|
58
|
+/**
|
|
59
|
+ * Implements a list item with an avatar rendered for it.
|
|
60
|
+ */
|
|
61
|
+export default class AvatarListItem extends Component<Props> {
|
|
62
|
+ /**
|
|
63
|
+ * Constructor of the component.
|
|
64
|
+ *
|
|
65
|
+ * @inheritdoc
|
|
66
|
+ */
|
|
67
|
+ constructor(props: Props) {
|
|
68
|
+ super(props);
|
|
69
|
+
|
|
70
|
+ this._renderItemLine = this._renderItemLine.bind(this);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ /**
|
|
74
|
+ * Implements {@code Component#render}.
|
|
75
|
+ *
|
|
76
|
+ * @inheritdoc
|
|
77
|
+ */
|
|
78
|
+ render() {
|
|
79
|
+ const {
|
|
80
|
+ avatarSize = AVATAR_SIZE,
|
|
81
|
+ avatarStyle,
|
|
82
|
+ avatarTextStyle
|
|
83
|
+ } = this.props;
|
|
84
|
+ const { avatar, colorBase, lines, title } = this.props.item;
|
|
85
|
+ const avatarStyles = {
|
|
86
|
+ ...styles.avatar,
|
|
87
|
+ ...this._getAvatarColor(colorBase),
|
|
88
|
+ ...avatarStyle,
|
|
89
|
+ borderRadius: avatarSize / 2,
|
|
90
|
+ height: avatarSize,
|
|
91
|
+ width: avatarSize
|
|
92
|
+ };
|
|
93
|
+
|
|
94
|
+ const isAvatarURL = Boolean(avatar && avatar.match(/^http[s]*:\/\//i));
|
|
95
|
+
|
|
96
|
+ return (
|
|
97
|
+ <Container
|
|
98
|
+ onClick = { this.props.onPress }
|
|
99
|
+ style = { styles.listItem }
|
|
100
|
+ underlayColor = { UNDERLAY_COLOR }>
|
|
101
|
+ <Container style = { styles.avatarContainer }>
|
|
102
|
+ <Container style = { avatarStyles }>
|
|
103
|
+ {
|
|
104
|
+ isAvatarURL && <Avatar
|
|
105
|
+ size = { avatarSize }
|
|
106
|
+ uri = { avatar } />
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ {
|
|
110
|
+ Boolean(avatar && !isAvatarURL) && <Icon
|
|
111
|
+ name = { avatar } />
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ {
|
|
115
|
+ !avatar && <Text
|
|
116
|
+ style = { [
|
|
117
|
+ styles.avatarContent,
|
|
118
|
+ avatarTextStyle
|
|
119
|
+ ] }>
|
|
120
|
+ { title.substr(0, 1).toUpperCase() }
|
|
121
|
+ </Text>
|
|
122
|
+ }
|
|
123
|
+ </Container>
|
|
124
|
+ </Container>
|
|
125
|
+ <Container style = { styles.listItemDetails }>
|
|
126
|
+ <Text
|
|
127
|
+ numberOfLines = { 1 }
|
|
128
|
+ style = { [
|
|
129
|
+ styles.listItemText,
|
|
130
|
+ styles.listItemTitle,
|
|
131
|
+ this.props.titleStyle
|
|
132
|
+ ] }>
|
|
133
|
+ { title }
|
|
134
|
+ </Text>
|
|
135
|
+ {this._renderItemLines(lines)}
|
|
136
|
+ </Container>
|
|
137
|
+ { this.props.children }
|
|
138
|
+ </Container>
|
|
139
|
+ );
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ /**
|
|
143
|
+ * Returns a style (color) based on the string that determines the color of
|
|
144
|
+ * the avatar.
|
|
145
|
+ *
|
|
146
|
+ * @param {string} colorBase - The string that is the base of the color.
|
|
147
|
+ * @private
|
|
148
|
+ * @returns {Object}
|
|
149
|
+ */
|
|
150
|
+ _getAvatarColor(colorBase) {
|
|
151
|
+ if (!colorBase) {
|
|
152
|
+ return null;
|
|
153
|
+ }
|
|
154
|
+ let nameHash = 0;
|
|
155
|
+
|
|
156
|
+ for (let i = 0; i < colorBase.length; i++) {
|
|
157
|
+ nameHash += colorBase.codePointAt(i);
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ return styles[`avatarColor${(nameHash % 5) + 1}`];
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ _renderItemLine: (string, number) => React$Node;
|
|
164
|
+
|
|
165
|
+ /**
|
|
166
|
+ * Renders a single line from the additional lines.
|
|
167
|
+ *
|
|
168
|
+ * @param {string} line - The line text.
|
|
169
|
+ * @param {number} index - The index of the line.
|
|
170
|
+ * @private
|
|
171
|
+ * @returns {React$Node}
|
|
172
|
+ */
|
|
173
|
+ _renderItemLine(line, index) {
|
|
174
|
+ if (!line) {
|
|
175
|
+ return null;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ return (
|
|
179
|
+ <Text
|
|
180
|
+ key = { index }
|
|
181
|
+ numberOfLines = { 1 }
|
|
182
|
+ style = { [
|
|
183
|
+ styles.listItemText,
|
|
184
|
+ this.props.linesStyle
|
|
185
|
+ ] }>
|
|
186
|
+ {line}
|
|
187
|
+ </Text>
|
|
188
|
+ );
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ _renderItemLines: Array<string> => Array<React$Node>;
|
|
192
|
+
|
|
193
|
+ /**
|
|
194
|
+ * Renders the additional item lines, if any.
|
|
195
|
+ *
|
|
196
|
+ * @param {Array<string>} lines - The lines to render.
|
|
197
|
+ * @private
|
|
198
|
+ * @returns {Array<React$Node>}
|
|
199
|
+ */
|
|
200
|
+ _renderItemLines(lines) {
|
|
201
|
+ return lines && lines.length ? lines.map(this._renderItemLine) : null;
|
|
202
|
+ }
|
|
203
|
+}
|