-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Section.tsx
277 lines (259 loc) · 6.75 KB
/
Section.tsx
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React, { useState } from 'react';
import {
Platform,
SafeAreaView,
StyleSheet,
Text,
View,
TextStyle,
ViewStyle,
} from 'react-native';
import Separator from './Separator';
export interface SectionInterface {
allowFontScaling?: boolean;
children?: React.ReactNode;
footerComponent?: React.ReactNode;
headerComponent?: React.ReactNode;
footer?: string;
footerTextColor?: TextStyle['color'];
footerTextStyle?: TextStyle;
header?: string;
headerTextColor?: TextStyle['color'];
headerTextStyle?: TextStyle;
hideSeparator?: boolean;
hideSurroundingSeparators?: boolean;
roundedCorners?: boolean;
sectionPaddingBottom?: ViewStyle['paddingBottom'];
sectionPaddingTop?: ViewStyle['paddingTop'];
sectionTintColor?: ViewStyle['backgroundColor'];
separatorInsetLeft?: ViewStyle['marginLeft'];
separatorInsetRight?: ViewStyle['marginRight'];
separatorTintColor?: ViewStyle['backgroundColor'];
withSafeAreaView?: boolean;
}
const Section: React.FC<SectionInterface> = ({
allowFontScaling = true,
children,
footerComponent,
headerComponent,
footer,
footerTextColor = '#6d6d72',
footerTextStyle = {},
header,
headerTextColor = '#6D6D72',
headerTextStyle = {},
hideSeparator = false,
hideSurroundingSeparators = false,
roundedCorners = false,
sectionPaddingBottom = 15,
sectionPaddingTop = 15,
sectionTintColor = '#EFEFF4',
separatorInsetLeft = 15,
separatorInsetRight = 0,
separatorTintColor = '#C8C7CC',
withSafeAreaView = Platform.OS === 'ios'
? parseInt(`${Platform.Version}`, 10) <= 10
? false
: true
: true,
}: SectionInterface) => {
const [highlightedRowIndex, setHighlightedRowIndex] = useState<number>();
const highlightRow = (index: number): void => {
if (highlightedRowIndex) return;
setHighlightedRowIndex(index);
};
const unhighlightRow = (): void => {
if (highlightedRowIndex) return;
setHighlightedRowIndex(undefined);
};
/**
* Merge styles with props
*/
const _styles = {
...styles,
section: [
styles.section,
{
backgroundColor: sectionTintColor,
paddingBottom: sectionPaddingBottom,
paddingTop: sectionPaddingTop,
},
],
sectionChildren: [
styles.sectionChildren,
roundedCorners && styles.sectionChildrenRoundedCorners,
],
sectionheaderText: [
styles.sectionheaderText,
{ color: headerTextColor },
headerTextStyle,
],
sectionfooterText: [
styles.sectionfooterText,
{ color: footerTextColor },
footerTextStyle,
],
};
// Need until .count is fixed: https://github.com/facebook/react/issues/7685
const childrenWithoutEmpty = React.Children.toArray(children);
const sumOfChildren = childrenWithoutEmpty.length;
/**
* Render Cell and add Border
*/
const renderChild = (
child: string | number | {} | React.ReactNode,
index: number,
): React.ReactNode => {
if (!child) {
return null;
}
if (!React.isValidElement(child)) return null; // Breaking?
const propsToAdd = {
onHighlightRow: (): void => highlightRow(index),
onUnHighlightRow: unhighlightRow,
sectionRoundedCorners: roundedCorners,
isFirstChild: index === 0,
isLastChild: index === sumOfChildren - 1,
};
// Skip rendering of separator
if (
hideSeparator ||
!Array.isArray(children) ||
sumOfChildren === 1 ||
index === sumOfChildren - 1 ||
child?.props?.hideSeparator
) {
return React.cloneElement(child, propsToAdd);
}
const invisibleSeparator =
highlightedRowIndex === index || highlightedRowIndex === index + 1;
// Add margin, if Image is provided
let separatorInsetLeftSupportImage = separatorInsetLeft;
// only update if separatorInsetLeft is default
if (child.props.image && separatorInsetLeft === 15) {
separatorInsetLeftSupportImage = 60;
}
return (
<View key={child?.key ?? undefined}>
{React.cloneElement(child, propsToAdd)}
<Separator
isHidden={invisibleSeparator}
backgroundColor={child.props.backgroundColor}
tintColor={separatorTintColor}
insetLeft={separatorInsetLeftSupportImage}
insetRight={separatorInsetRight}
/>
</View>
);
};
/**
* Render header if defined
*/
const renderHeader = (): React.ReactNode | null => {
if (header && withSafeAreaView) {
return (
<View style={styles.sectionheader}>
<SafeAreaView>
<Text
allowFontScaling={allowFontScaling}
style={_styles.sectionheaderText}>
{header}
</Text>
</SafeAreaView>
</View>
);
}
if (header) {
return (
<View style={styles.sectionheader}>
<Text
allowFontScaling={allowFontScaling}
style={_styles.sectionheaderText}>
{header}
</Text>
</View>
);
}
return null;
};
/**
* Render footer if defined
*/
const renderFooter = (): React.ReactNode | null => {
if (footer && withSafeAreaView) {
return (
<View style={styles.sectionfooter}>
<SafeAreaView>
<Text
allowFontScaling={allowFontScaling}
style={_styles.sectionfooterText}>
{footer}
</Text>
</SafeAreaView>
</View>
);
}
if (footer) {
return (
<View style={styles.sectionfooter}>
<Text
allowFontScaling={allowFontScaling}
style={_styles.sectionfooterText}>
{footer}
</Text>
</View>
);
}
return null;
};
return (
<View style={_styles.section}>
{headerComponent || renderHeader()}
{hideSurroundingSeparators || (
<Separator
insetLeft={0}
tintColor={separatorTintColor}
withSafeAreaView={false}
/>
)}
<View style={_styles.sectionChildren}>
{childrenWithoutEmpty.map(renderChild)}
</View>
{hideSurroundingSeparators || (
<Separator
insetLeft={0}
tintColor={separatorTintColor}
withSafeAreaView={false}
/>
)}
{footerComponent || renderFooter()}
</View>
);
};
const styles = StyleSheet.create({
section: {},
sectionChildren: {},
sectionChildrenRoundedCorners: {
borderRadius: 10,
overflow: 'hidden',
},
sectionheader: {
paddingLeft: 15,
paddingRight: 15,
paddingBottom: 5,
},
sectionheaderText: {
fontSize: 13,
letterSpacing: -0.078,
},
sectionfooter: {
paddingLeft: 15,
paddingRight: 15,
paddingTop: 10,
},
sectionfooterText: {
fontSize: 13,
letterSpacing: -0.078,
},
});
export default Section;