-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBottomSheet.jsx
More file actions
500 lines (479 loc) · 13.5 KB
/
BottomSheet.jsx
File metadata and controls
500 lines (479 loc) · 13.5 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import React, { useCallback, useEffect, useMemo, useState } from "react";
import {
StyleSheet,
FlatList,
Keyboard,
InteractionManager,
} from "react-native";
import PropTypes from "prop-types";
import Modal from "react-native-modal";
import { SafeAreaView } from "react-native-safe-area-context";
import { moderateScale } from "react-native-size-matters";
import { Container } from "./Container";
import { Divider } from "./Divider";
import { Loader } from "./Loader";
import { SearchBar } from "./SearchBar";
import { Touchable } from "./Touchable";
import { Typography } from "./Typography";
import { theme } from "../theme";
const Title = ({
title,
bg,
isVisible,
hide,
titleContainerStyle,
titleTextStyle,
contentType,
canSearch,
setSearchText,
onDonePress,
searchBarProps,
}) => {
let touchY;
useEffect(() => {
setSearchText("");
}, [isVisible, setSearchText]);
return (
<Container
bg={bg}
borderRadius={moderateScale(20)}
pb={moderateScale(12)}
onTouchStart={e => (touchY = e.nativeEvent.pageY)}
onTouchEnd={e => {
// Swipe Down Detection
if (e.nativeEvent.pageY - touchY > 20) hide();
}}
{...titleContainerStyle}
>
<Container flexDirection="row" justifyContent="space-between">
{title && (
<Typography textStyle="modalHeader" {...titleTextStyle}>
{title}
</Typography>
)}
{contentType && (
<Touchable
onPress={() => {
hide();
onDonePress();
}}
>
<Typography color="font.purple500" textStyle="modalHeader">
Done
</Typography>
</Touchable>
)}
</Container>
{canSearch && (
<SearchBar
containerProps={{ mt: moderateScale(16) }}
debounceDelay={200}
placeholder="Search"
showCancelButton={false}
onChangeText={setSearchText}
onCancel={() => {
setSearchText("");
}}
{...searchBarProps}
/>
)}
</Container>
);
};
Title.propTypes = {
title: PropTypes.string,
isVisible: PropTypes.bool,
hide: PropTypes.func,
bg: PropTypes.string,
titleContainerStyle: PropTypes.object,
titleTextStyle: PropTypes.object,
contentType: PropTypes.oneOf(["checkbox", null]),
canSearch: PropTypes.bool,
setSearchText: PropTypes.func,
onDonePress: PropTypes.func,
searchBarProps: PropTypes.object,
};
/**
* BottomSheet can be used to select an item from list of items. Like a DropDown
*
* <div class="screenshots">
* <img src="screenshots/bottomsheet/bottomsheet.png" />
* </div>
*
* ## Usage
* ```js
* import React, { useState } from 'react';
* import { Container, BottomSheet } from '@bigbinary/neetoui-rn';
*
* export default function Main() {
* const [isBottomSheetVisible, setBottomSheetVisibility] = useState(false);
* const [selectedItem, setSelectedItem] = useState(null);
* const data = ["neeto-ui-rn", "neeto-desk-rn", "neeto-hq"];
*
* return (
* <Container>
* <BottomSheet
* maxHeight={moderateScale(200)}
* isVisible={isBottomSheetVisible}
* hide={() => {
* setBottomSheetVisibility(false);
* }}
* onItemPress={({item}) => {
* setSelectedItem(item);
* }}
* title="PROJECT"
* data={data}
* selectedItem={selectedItem}
* ContentRow={() => <CustomComponent />}
* />
* </Container>
* );
* }
* ```
*
* @extends StyledSystems props /styled-system
*/
export const BottomSheet = ({
showCreateOption,
CreateItemComponent,
showCreateOptionLoader,
createSearchedOptionLabelStyle,
createOptionLabel,
onPressCreateOption,
createSearchedOptionContainerStyle,
data = [],
title,
hide,
isVisible,
onItemPress,
bg,
children,
titleContainerStyle,
titleTextStyle,
modalParams,
HeaderComponent,
ContentRow,
contentType,
canSearch,
onDonePress,
disabled,
noResultsLabelContainerStyle,
noResultsLabelStyle,
noResultsLabel,
NoResultsComponent,
labelExtractor,
valueExtractor,
selectedItem,
onBackdropPress,
searchBarProps,
shouldHideKeyboardOnScrollBegin = true,
shouldShowItemSeparator = true,
flatListRef,
...rest
}) => {
const [searchText, setSearchText] = useState("");
const [isReady, setIsReady] = useState(false);
useEffect(() => {
if (isVisible) {
InteractionManager.runAfterInteractions(() => {
setIsReady(true);
});
} else {
setIsReady(false);
}
}, [isVisible]);
const getLabel = useCallback(
(item, index) => item?.label || labelExtractor(item, index) || item,
[labelExtractor]
);
const generatedData = useMemo(() => {
if (searchText.length) {
return data.filter(item =>
getLabel(item).toLowerCase().includes(searchText.toLowerCase())
);
}
return data;
}, [data, getLabel, searchText]);
const shouldHideCreateComponent = useMemo(() => {
const filteredItemCount = generatedData.filter(
data =>
getLabel(data).toLocaleLowerCase() === searchText.toLocaleLowerCase()
).length;
return filteredItemCount > 0;
}, [generatedData, getLabel, searchText]);
const checkIsSelected = item => {
const itemValue = item?.value || valueExtractor(item) || item;
const selectedItemValue =
selectedItem?.value || valueExtractor(selectedItem) || selectedItem;
return itemValue === selectedItemValue;
};
return (
<Modal
avoidKeyboard
hideModalContentWhileAnimating
useNativeDriver
isVisible={isVisible && isReady}
style={styles.modalStyle}
onRequestClose={hide}
onBackdropPress={() => {
onBackdropPress();
hide();
}}
{...modalParams}
>
<SafeAreaView style={styles.safeAreaView}>
<Container
bg={bg}
borderTopLeftRadius={moderateScale(20)}
borderTopRightRadius={moderateScale(20)}
flexShrink={1}
p={moderateScale(16)}
{...rest}
>
{title && (
<Title
HeaderComponent={HeaderComponent}
bg={bg}
canSearch={canSearch}
contentType={contentType}
hide={hide}
isVisible={isVisible}
searchBarProps={searchBarProps}
searchText={searchText}
setSearchText={setSearchText}
title={title}
titleContainerStyle={titleContainerStyle}
titleTextStyle={titleTextStyle}
onDonePress={onDonePress}
/>
)}
{data && (
<FlatList
data={generatedData}
initialNumToRender={data.length}
keyExtractor={(_item, index) => index}
keyboardShouldPersistTaps="handled"
ref={flatListRef}
showsVerticalScrollIndicator={false}
ItemSeparatorComponent={
shouldShowItemSeparator ? (
<Divider bg="background.grey200" />
) : null
}
ListFooterComponent={
<Container>
{children}
{!!searchText &&
!generatedData.length &&
!showCreateOption &&
(NoResultsComponent ? (
<NoResultsComponent />
) : (
<Container
alignItems="center"
mb={moderateScale(2)}
py={moderateScale(2)}
{...noResultsLabelContainerStyle}
>
<Typography
color="font.grey"
fontFamily="sf600"
fontSize="s"
{...noResultsLabelStyle}
>
{noResultsLabel || "No options found"}
</Typography>
</Container>
))}
{!disabled &&
!shouldHideCreateComponent &&
(showCreateOptionLoader ? (
<Loader color={theme.colors.background.base} />
) : (
!!searchText &&
showCreateOption && (
<Container>
{CreateItemComponent ? (
<CreateItemComponent searchText={searchText} />
) : (
<Touchable
alignItems="center"
height={moderateScale(40)}
justifyContent="center"
onPress={() => onPressCreateOption(searchText)}
{...createSearchedOptionContainerStyle}
>
<Typography
color="font.grey"
fontFamily="sf400"
fontSize="s"
{...createSearchedOptionLabelStyle}
>
{createOptionLabel ||
`Create ${searchText} option`}
</Typography>
</Touchable>
)}
</Container>
)
))}
</Container>
}
renderItem={({ item, index }) => (
<ContentRow
index={index}
isSelected={checkIsSelected(item)}
item={item}
key={index}
onPress={() => {
!contentType && hide();
onItemPress({ index, item });
}}
/>
)}
onScrollBeginDrag={
shouldHideKeyboardOnScrollBegin && Keyboard.dismiss
}
/>
)}
</Container>
</SafeAreaView>
</Modal>
);
};
BottomSheet.defaultProps = {
bg: "background.primary",
showCreateOption: false,
showCreateOptionLoader: false,
createOptionLabel: null,
onPressCreateOption: () => {},
CreateItemComponent: null,
onDonePress: () => {},
valueExtractor: () => {},
labelExtractor: () => {},
onBackdropPress: () => {},
searchBarProps: {},
};
BottomSheet.propTypes = {
/**
* List of items.
*/
data: PropTypes.array,
/**
* Title to be displayed in the bottom sheet header.
*/
title: PropTypes.string,
/**
* If true bottom sheet will be shown.
*/
isVisible: PropTypes.bool,
/**
* Callback that will be called on while hiding the bottom sheet.
*/
hide: PropTypes.func,
/**
* Callback which returns the index of selected item.
*/
onItemPress: PropTypes.func,
/**
* Callback that will be called on Done button press
*/
onDonePress: PropTypes.func,
/**
* Background color.
*/
bg: PropTypes.string,
/**
* To customize title container styles.
*/
titleContainerStyle: PropTypes.object,
/**
* To customize title text styles.
*/
titleTextStyle: PropTypes.object,
/**
* Show option to create the searched label if not present in the options list
*/
showCreateOption: PropTypes.bool,
/**
* Show loader while creating a searched option not present in the options list
*/
showCreateOptionLoader: PropTypes.bool,
/**
* Custom label for creating searched option not present in the options list
*/
createOptionLabel: PropTypes.string,
/**
* Callback when create searched option is pressed
*/
onPressCreateOption: PropTypes.func,
/**
* Component that renders when searched item doesn't exists
*/
CreateItemComponent: PropTypes.func,
/**
* To customize empty options placeholder text style.
*/
createSearchedOptionLabelStyle: PropTypes.object,
/**
* Flag will disable create item feature
*/
disabled: PropTypes.bool,
/**
* To customize empty options placeholder container style.
*/
createSearchedOptionContainerStyle: PropTypes.object,
noResultsLabelContainerStyle: PropTypes.object,
noResultsLabelStyle: PropTypes.object,
noResultsLabel: PropTypes.string,
NoResultsComponent: PropTypes.node,
/**
* Use custom key as label.
*/
labelExtractor: PropTypes.func,
/**
* Use custom key as value.
*/
valueExtractor: PropTypes.func,
/**
* Function to customize back drop press
*/
onBackdropPress: PropTypes.func,
/**
* Currently selected item
*/
selectedItem: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
/**
* Object to update the searchbar component
*/
searchBarProps: PropTypes.object,
/**
* To support more Modal params.
*/
modalParams: PropTypes.object,
children: PropTypes.node,
HeaderComponent: PropTypes.elementType,
ContentRow: PropTypes.elementType,
contentType: PropTypes.oneOf(["checkbox", null]),
canSearch: PropTypes.bool,
shouldHideKeyboardOnScrollBegin: PropTypes.bool,
shouldShowItemSeparator: PropTypes.bool,
flatListRef: PropTypes.object,
};
const styles = StyleSheet.create({
safeAreaView: {
maxHeight: "70%",
backgroundColor: theme.colors.background.primary,
borderTopRightRadius: moderateScale(20),
borderTopLeftRadius: moderateScale(20),
},
modalStyle: {
margin: 0,
justifyContent: "flex-end",
flex: 1,
},
inputContainerStyle: {
backgroundColor: "white",
borderRadius: moderateScale(10),
},
});