|
1 | 1 | import _ from 'lodash';
|
2 | 2 | import React, {Component} from 'react';
|
3 |
| -import {StyleSheet, Alert, ScrollView} from 'react-native'; |
| 3 | +import {StyleSheet, ScrollView, LayoutAnimation} from 'react-native'; |
4 | 4 | import {Colors, Typography, View, Drawer, Text, Button, ListItem, Avatar, AvatarHelper} from 'react-native-ui-lib'; //eslint-disable-line
|
5 | 5 | import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
|
6 | 6 | import conversations from '../../data/conversations';
|
7 | 7 |
|
| 8 | +import {renderBooleanOption, renderSliderOption, renderColorOption} from '../ExampleScreenPresenter'; |
8 | 9 |
|
9 |
| -const collectionsIcon = require('../../assets/icons/collections.png'); |
10 |
| -const starIcon = require('../../assets/icons/star.png'); |
11 |
| -const shareIcon = require('../../assets/icons/share.png'); |
12 |
| -const videoIcon = require('../../assets/icons/video.png'); |
13 |
| -const tagsIcon = require('../../assets/icons/tags.png'); |
| 10 | +const ITEMS = { |
| 11 | + read: {icon: require('../../assets/icons/mail.png'), text: 'Read', background: Colors.green30}, |
| 12 | + archive: {icon: require('../../assets/icons/archive.png'), text: 'Archive', background: Colors.blue30}, |
| 13 | + delete: {icon: require('../../assets/icons/delete.png'), text: 'Delete', background: Colors.red30} |
| 14 | +}; |
14 | 15 |
|
15 | 16 | class DrawerScreen extends Component {
|
16 | 17 | constructor(props) {
|
17 | 18 | super(props);
|
18 | 19 |
|
19 | 20 | this.state = {
|
20 |
| - itemsTintColor: undefined, |
21 |
| - leftItem: {icon: collectionsIcon, text: 'Archive', onPress: this.onLeftItemPressed}, |
22 |
| - rightItems: [ |
23 |
| - {icon: starIcon, text: 'Accessories', onPress: this.onItemPress, background: Colors.violet10}, |
24 |
| - {icon: shareIcon, text: 'Share', onPress: this.onItemPress, background: Colors.violet30}, |
25 |
| - {icon: videoIcon, text: 'Video', onPress: this.onItemPress, background: Colors.violet40}, |
26 |
| - // {icon: tagsIcon, text: 'Video', background: Colors.red30}, |
27 |
| - ], |
| 21 | + hideItem: false, |
| 22 | + showRightItems: true, |
| 23 | + showLeftItem: true, |
| 24 | + fullSwipeLeft: true |
28 | 25 | };
|
29 | 26 | }
|
30 | 27 |
|
31 |
| - onItemPress = () => { |
32 |
| - Alert.alert('Right drawer item pressed'); |
33 |
| - |
34 |
| - this.toggleDynamicItem(); |
35 |
| - this.firstDrawer.closeDrawer(); |
36 |
| - }; |
37 |
| - onItemPress2 = () => { |
38 |
| - const {itemsTintColor} = this.state; |
39 |
| - const color = itemsTintColor === undefined ? Colors.blue30 : undefined; |
40 |
| - |
41 |
| - this.setState({itemsTintColor: color}); |
42 |
| - }; |
43 |
| - onLeftItemPressed = () => { |
44 |
| - Alert.alert('Left drawer item pressed'); |
45 |
| - }; |
46 |
| - onContentPress(id) { |
47 |
| - Alert.alert(`List item #${id + 1} pressed`); |
48 |
| - } |
49 |
| - onButtonPress(id) { |
50 |
| - Alert.alert(`Button '${id}' pressed`); |
51 |
| - } |
52 |
| - |
53 |
| - async setItemWidth(item) { |
54 |
| - if (item && !item.width && !_.isEmpty(item.text)) { |
55 |
| - const horizontalPadding = 12; |
56 |
| - const typography = Typography.text70; |
57 |
| - const width = await Typography.measureWidth(item.text, typography); |
58 |
| - const itemCopy = item; |
59 |
| - itemCopy.width = width + horizontalPadding * 2; |
60 |
| - return itemCopy; |
| 28 | + componentDidUpdate(prevProps, prevState) { |
| 29 | + if (this.state.hideItem && prevState.hideItem) { |
| 30 | + this.setState({ |
| 31 | + hideItem: false |
| 32 | + }); |
61 | 33 | }
|
62 | 34 | }
|
63 | 35 |
|
64 |
| - toggleDynamicItem() { |
65 |
| - const {rightItems} = this.state; |
66 |
| - let newItem; |
67 |
| - if (rightItems[0].text === 'Accessories') { |
68 |
| - newItem = {icon: starIcon, text: 'More', onPress: this.onItemPress, background: Colors.violet10}; |
69 |
| - } else { |
70 |
| - newItem = { |
71 |
| - icon: starIcon, |
72 |
| - text: 'Accessories', |
73 |
| - onPress: this.onItemPress, |
74 |
| - background: Colors.violet10 |
75 |
| - }; |
76 |
| - } |
77 |
| - rightItems[0] = newItem; |
78 |
| - this.setState({rightItems}); |
79 |
| - } |
| 36 | + onWillFullSwipeLeft = () => { |
| 37 | + |
| 38 | + // TODO: consider including this functionality as part of the drawer component |
| 39 | + setTimeout(() => { |
| 40 | + LayoutAnimation.configureNext({ |
| 41 | + update: { |
| 42 | + type: LayoutAnimation.Types.easeInEaseOut, |
| 43 | + property: LayoutAnimation.Properties.scaleY |
| 44 | + }, |
| 45 | + delete: { |
| 46 | + type: LayoutAnimation.Types.easeInEaseOut, |
| 47 | + property: LayoutAnimation.Properties.scaleY, |
| 48 | + duration: 2000 |
| 49 | + }, |
| 50 | + duration: 120 |
| 51 | + }); |
| 52 | + this.setState({hideItem: true}); |
| 53 | + }, 200); |
| 54 | + }; |
80 | 55 |
|
81 |
| - renderContent(id, row) { |
82 |
| - const initials = AvatarHelper.getInitials(row.name); |
| 56 | + renderListItem() { |
83 | 57 | return (
|
84 |
| - <ListItem key={id} onPress={() => this.onContentPress(id)} style={styles.listContent}> |
85 |
| - <ListItem.Part left> |
86 |
| - <Avatar |
87 |
| - source={row.thumbnail ? {uri: row.thumbnail} : null} |
88 |
| - label={initials} |
89 |
| - badgeProps={{backgroundColor: Number(id) % 3 === 0 ? Colors.green30 : undefined}} |
90 |
| - containerStyle={{marginHorizontal: 18}} |
91 |
| - backgroundColor={Colors.white} |
92 |
| - /> |
93 |
| - </ListItem.Part> |
94 |
| - <ListItem.Part middle containerStyle={styles.border}> |
95 |
| - <Text text70>{row.name}</Text> |
96 |
| - </ListItem.Part> |
97 |
| - </ListItem> |
| 58 | + <View bg-grey80 paddingH-20 paddingV-10 row centerV style={{borderBottomWidth: 1, borderColor: Colors.grey60}}> |
| 59 | + <Avatar source={{uri: conversations[0].thumbnail}}/> |
| 60 | + <View marginL-20> |
| 61 | + <Text text70R>{conversations[0].name}</Text> |
| 62 | + <Text text80 marginT-2> |
| 63 | + {conversations[0].text} |
| 64 | + </Text> |
| 65 | + </View> |
| 66 | + </View> |
98 | 67 | );
|
99 | 68 | }
|
100 | 69 |
|
101 | 70 | render() {
|
102 |
| - const {leftItem, rightItems} = this.state; |
103 |
| - |
104 |
| - return ( |
105 |
| - <ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}> |
106 |
| - <Drawer |
107 |
| - leftItem={leftItem} |
108 |
| - rightItems={rightItems} |
109 |
| - style={styles.drawer} |
110 |
| - itemsIconSize={20} |
111 |
| - ref={r => this.firstDrawer = r} |
112 |
| - > |
113 |
| - {this.renderContent('0', conversations[0])} |
114 |
| - </Drawer> |
115 |
| - |
116 |
| - <Drawer |
117 |
| - leftItem={leftItem} |
118 |
| - rightItems={[ |
119 |
| - {icon: shareIcon, text: 'Share', onPress: this.onItemPress}, |
120 |
| - {icon: videoIcon, text: 'Video', onPress: this.onItemPress, background: Colors.blue10}, |
121 |
| - ]} |
122 |
| - style={styles.drawer} |
123 |
| - > |
124 |
| - {this.renderContent('1', conversations[1])} |
125 |
| - </Drawer> |
126 |
| - |
127 |
| - {/* <Drawer |
128 |
| - // leftItem={leftItem} |
129 |
| - rightItems={rightItems} |
130 |
| - style={styles.drawer} |
131 |
| - equalWidths |
132 |
| - > |
133 |
| - {this.renderContent('1', conversations[1])} |
134 |
| - </Drawer> */} |
| 71 | + const { |
| 72 | + showRightItems, |
| 73 | + showLeftItem, |
| 74 | + fullSwipeLeft, |
| 75 | + itemsTintColor, |
| 76 | + bounciness, |
| 77 | + itemsIconSize, |
| 78 | + hideItem |
| 79 | + } = this.state; |
| 80 | + |
| 81 | + const drawerProps = { |
| 82 | + itemsTintColor, |
| 83 | + itemsIconSize, |
| 84 | + bounciness, |
| 85 | + fullSwipeLeft, |
| 86 | + onWillFullSwipeLeft: this.onWillFullSwipeLeft |
| 87 | + }; |
| 88 | + if (showRightItems) { |
| 89 | + drawerProps.rightItems = [ITEMS.read, ITEMS.archive]; |
| 90 | + } |
135 | 91 |
|
136 |
| - <Drawer |
137 |
| - leftItem={{text: 'Archive', background: Colors.blue10, width: 100, onPress: this.onLeftItemPressed}} |
138 |
| - // rightItems={rightItems} |
139 |
| - style={styles.drawer} |
140 |
| - itemsTextStyle={{fontSize: 18, fontWeight: 'bold'}} |
141 |
| - > |
142 |
| - {this.renderContent('2', conversations[2])} |
143 |
| - </Drawer> |
| 92 | + if (showLeftItem) { |
| 93 | + drawerProps.leftItem = ITEMS.delete; |
| 94 | + } |
144 | 95 |
|
145 |
| - <View style={{paddingHorizontal: 50}}> |
146 |
| - <Drawer |
147 |
| - leftItem={leftItem} |
148 |
| - rightItems={[rightItems[1], rightItems[2]]} |
149 |
| - itemsIconSize={24} |
150 |
| - style={{marginTop: 20}} |
151 |
| - onPress={this.onPress} |
152 |
| - > |
153 |
| - {this.renderContent('2', conversations[2])} |
154 |
| - </Drawer> |
| 96 | + return ( |
| 97 | + <View flex> |
| 98 | + <View padding-20 paddingB-0> |
| 99 | + <Text text40 marginB-20> |
| 100 | + Drawer |
| 101 | + </Text> |
155 | 102 | </View>
|
156 |
| - |
157 |
| - <Drawer |
158 |
| - leftItem={{icon: collectionsIcon, background: Colors.blue10, width: 100}} |
159 |
| - rightItems={[ |
160 |
| - {icon: starIcon, background: Colors.dark60}, |
161 |
| - {icon: shareIcon, background: Colors.yellow20}, |
162 |
| - {icon: videoIcon, background: Colors.red30, onPress: this.onItemPress2}, |
163 |
| - {icon: tagsIcon, background: Colors.green30}, |
164 |
| - ]} |
165 |
| - style={styles.drawer} |
166 |
| - itemsIconSize={30} |
167 |
| - itemsTintColor={this.state.itemsTintColor} |
168 |
| - > |
169 |
| - {this.renderContent('4', conversations[4])} |
170 |
| - </Drawer> |
171 |
| - |
172 |
| - <Drawer |
173 |
| - leftItem={leftItem} |
174 |
| - rightItems={rightItems} |
175 |
| - style={styles.drawer} |
176 |
| - itemsTextStyle={{fontSize: 12}} |
177 |
| - > |
178 |
| - <View style={styles.rowContent}> |
179 |
| - <View style={styles.rowIcon}/> |
180 |
| - <View> |
181 |
| - <Text style={styles.rowTitle}>Row Title</Text> |
182 |
| - <Text style={styles.rowSubtitle}>Drag the row left and right</Text> |
183 |
| - </View> |
184 |
| - <View style={styles.rowButtonContainer}> |
185 |
| - <Button |
186 |
| - label={'1'} |
187 |
| - size={'small'} |
188 |
| - round |
189 |
| - backgroundColor={Colors.yellow30} |
190 |
| - white |
191 |
| - onPress={() => this.onButtonPress('1')} |
192 |
| - /> |
193 |
| - </View> |
| 103 | + {!hideItem && ( |
| 104 | + <Drawer key={Date.now()} {...drawerProps}> |
| 105 | + {this.renderListItem()} |
| 106 | + </Drawer> |
| 107 | + )} |
| 108 | + |
| 109 | + <ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}> |
| 110 | + <View padding-20> |
| 111 | + {renderBooleanOption.call(this, 'rightItems', 'showRightItems')} |
| 112 | + {renderBooleanOption.call(this, 'leftItem', 'showLeftItem')} |
| 113 | + {showLeftItem && renderBooleanOption.call(this, 'fullSwipeLeft', 'fullSwipeLeft')} |
| 114 | + {renderColorOption.call(this, 'icon+text color', 'itemsTintColor')} |
| 115 | + {renderSliderOption.call(this, 'bounciness', 'bounciness', {min: 5, max: 15, step: 1, initial: 5})} |
| 116 | + {renderSliderOption.call(this, 'iconSize', 'itemsIconSize', {min: 15, max: 25, step: 1, initial: 20})} |
194 | 117 | </View>
|
195 |
| - </Drawer> |
196 |
| - </ScrollView> |
| 118 | + </ScrollView> |
| 119 | + </View> |
197 | 120 | );
|
198 | 121 | }
|
199 | 122 | }
|
|
0 commit comments