-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrapper.js
124 lines (115 loc) · 3.87 KB
/
wrapper.js
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
import React, { useCallback, useRef, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import Header from './header';
import Searchbar from './searchbar';
import SearchView from './searchView';
const Wrapper = ({
backgroundColor,
animationDuration,
headerRight,
headerLeft,
title,
cancelText,
cancelTextStyle,
cancelTextMarginRight,
placeholder,
searchbarStyle,
placeholderTextColor,
onPressCancel,
onFocus,
onChangeText,
headerHeight, // Header height without searchbar
searchBarIcon,
searchScreen,
children
}) => {
const header = useRef();
const searchbar = useRef();
const searchView = useRef();
const [dimensions, setDimensions] = useState({
fullHeaderHeight: undefined,
searchbarHeight: undefined
});
const open = useCallback(() => {
onPressCancel && onPressCancel();
header.current.close();
searchbar.current.open();
searchView.current.open();
}, [header, searchbar,searchView]);
const close = useCallback(() => {
onFocus && onFocus();
header.current.open();
searchbar.current.close();
searchView.current.close();
}, [header, searchbar,searchView])
const onLayoutFullHeader = useCallback(event => {
const { fullHeaderHeight } = dimensions;
const { height } = event.nativeEvent.layout;
if (fullHeaderHeight && fullHeaderHeight >= height) return // layout was already called
setDimensions({ ...dimensions, fullHeaderHeight: height });
}, [dimensions, setDimensions])
const onLayoutSearchBar = useCallback(event => {
const { searchbarHeight } = dimensions;
const { height } = event.nativeEvent.layout;
if (searchbarHeight && searchbarHeight >= height) return // layout was already called
setDimensions({ ...dimensions, searchbarHeight: height });
}, [dimensions, setDimensions])
return (
<View style={styles.container}>
<View onLayout={onLayoutFullHeader}>
<Header
ref={header}
title={title}
duration={animationDuration}
backgroundColor={backgroundColor}
right={headerRight}
left={headerLeft}
height={headerHeight}
/>
<Searchbar
ref={searchbar}
onFocus={open}
onCancel={close}
backgroundColor={backgroundColor}
duration={animationDuration}
placeholder={placeholder}
cancelText={cancelText}
cancelTextStyle={cancelTextStyle}
searchbarStyle={searchbarStyle}
placeholderTextColor={placeholderTextColor}
marginRight={cancelTextMarginRight}
onLayout={onLayoutSearchBar}
onChangeText={onChangeText}
searchBarIcon={searchBarIcon}
/>
</View>
{
dimensions.fullHeaderHeight && dimensions.searchbarHeight &&
<SearchView
ref={searchView}
duration={animationDuration}
searchbarHeight={dimensions.searchbarHeight}
fullHeaderHeight={dimensions.fullHeaderHeight}
>
{searchScreen}
</SearchView>
}
{children}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
Wrapper.defaultProps = {
backgroundColor: '#191919',
animationDuration: 150,
title: 'Title',
placeholder: 'Search...',
cancelText: 'Cancel',
cancelTextMarginRight: 20,
headerHeight: 50
}
export default Wrapper;