|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + StyleSheet, |
| 4 | + Text, |
| 5 | + Image, |
| 6 | + Animated, |
| 7 | + TouchableOpacity, |
| 8 | + TouchableHighlight, |
| 9 | + View, |
| 10 | +} from 'react-native'; |
| 11 | + |
| 12 | +import SwipeListView from '../SwipeListView'; |
| 13 | + |
| 14 | +export default function Actions() { |
| 15 | + const [listData, setListData] = useState( |
| 16 | + Array(20) |
| 17 | + .fill('') |
| 18 | + .map((_, i) => ({ |
| 19 | + key: `${i}`, |
| 20 | + text: `item #${i}`, |
| 21 | + initialLeftActionState: i % 2 !== 0, |
| 22 | + })) |
| 23 | + ); |
| 24 | + |
| 25 | + const closeRow = (rowMap, rowKey) => { |
| 26 | + if (rowMap[rowKey]) { |
| 27 | + rowMap[rowKey].closeRow(); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + const deleteRow = (rowMap, rowKey) => { |
| 32 | + closeRow(rowMap, rowKey); |
| 33 | + const newData = [...listData]; |
| 34 | + const prevIndex = listData.findIndex(item => item.key === rowKey); |
| 35 | + newData.splice(prevIndex, 1); |
| 36 | + setListData(newData); |
| 37 | + }; |
| 38 | + |
| 39 | + const onRowDidOpen = rowKey => { |
| 40 | + console.log('This row opened', rowKey); |
| 41 | + }; |
| 42 | + |
| 43 | + const onLeftActionStatusChange = rowKey => { |
| 44 | + console.log('onLeftActionStatusChange', rowKey); |
| 45 | + }; |
| 46 | + |
| 47 | + const onRightActionStatusChange = rowKey => { |
| 48 | + console.log('onRightActionStatusChange', rowKey); |
| 49 | + }; |
| 50 | + |
| 51 | + const onRightAction = rowKey => { |
| 52 | + console.log('onRightAction', rowKey); |
| 53 | + }; |
| 54 | + |
| 55 | + const onLeftAction = rowKey => { |
| 56 | + console.log('onLeftAction', rowKey); |
| 57 | + }; |
| 58 | + |
| 59 | + const VisibleItem = props => { |
| 60 | + console.log(props.leftActionState); |
| 61 | + |
| 62 | + const { |
| 63 | + rowHeightAnimatedValue, |
| 64 | + rightActionState, |
| 65 | + leftActionState, |
| 66 | + data, |
| 67 | + removeRow, |
| 68 | + } = props; |
| 69 | + |
| 70 | + if (rightActionState) { |
| 71 | + Animated.timing(rowHeightAnimatedValue, { |
| 72 | + toValue: 0, |
| 73 | + duration: 200, |
| 74 | + }).start(() => { |
| 75 | + removeRow(); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return ( |
| 80 | + <Animated.View |
| 81 | + style={[ |
| 82 | + styles.rowFront, |
| 83 | + { height: rowHeightAnimatedValue }, |
| 84 | + leftActionState && { backgroundColor: 'lightgreen' }, |
| 85 | + ]} |
| 86 | + > |
| 87 | + <TouchableHighlight |
| 88 | + onPress={() => console.log('You touched me')} |
| 89 | + style={[ |
| 90 | + styles.rowFront, |
| 91 | + leftActionState && { |
| 92 | + backgroundColor: 'lightgreen', |
| 93 | + }, |
| 94 | + ]} |
| 95 | + underlayColor={'#AAA'} |
| 96 | + > |
| 97 | + <View> |
| 98 | + <Text>I am {data.item.text} in a SwipeListView</Text> |
| 99 | + </View> |
| 100 | + </TouchableHighlight> |
| 101 | + </Animated.View> |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + const renderItem = (data, rowMap) => { |
| 106 | + const rowHeightAnimatedValue = new Animated.Value(50); |
| 107 | + return ( |
| 108 | + <VisibleItem |
| 109 | + rowHeightAnimatedValue={rowHeightAnimatedValue} |
| 110 | + data={data} |
| 111 | + removeRow={() => deleteRow(rowMap, data.item.key)} |
| 112 | + /> |
| 113 | + ); |
| 114 | + }; |
| 115 | + |
| 116 | + const HiddenItemWithActions = props => { |
| 117 | + const { |
| 118 | + leftActionActivated, |
| 119 | + rightActionActivated, |
| 120 | + swipeAnimatedValue, |
| 121 | + rowActionAnimatedValue, |
| 122 | + rowHeightAnimatedValue, |
| 123 | + onClose, |
| 124 | + onDelete, |
| 125 | + } = props; |
| 126 | + |
| 127 | + if (rightActionActivated) { |
| 128 | + Animated.spring(rowActionAnimatedValue, { |
| 129 | + toValue: 500, |
| 130 | + }).start(); |
| 131 | + } else { |
| 132 | + Animated.spring(rowActionAnimatedValue, { |
| 133 | + toValue: 75, |
| 134 | + }).start(); |
| 135 | + } |
| 136 | + |
| 137 | + return ( |
| 138 | + <Animated.View |
| 139 | + style={[ |
| 140 | + styles.rowBack, |
| 141 | + { height: rowHeightAnimatedValue }, |
| 142 | + leftActionActivated && { backgroundColor: 'lightgreen' }, |
| 143 | + ]} |
| 144 | + > |
| 145 | + <Text>Left</Text> |
| 146 | + {!leftActionActivated && ( |
| 147 | + <TouchableOpacity |
| 148 | + style={[styles.backRightBtn, styles.backRightBtnLeft]} |
| 149 | + onPress={onClose} |
| 150 | + > |
| 151 | + <Text style={styles.backTextWhite}>Close</Text> |
| 152 | + </TouchableOpacity> |
| 153 | + )} |
| 154 | + {!leftActionActivated && ( |
| 155 | + <Animated.View |
| 156 | + style={[ |
| 157 | + styles.backRightBtn, |
| 158 | + styles.backRightBtnRight, |
| 159 | + { flex: 1, width: rowActionAnimatedValue }, |
| 160 | + ]} |
| 161 | + > |
| 162 | + <TouchableOpacity |
| 163 | + style={[ |
| 164 | + styles.backRightBtn, |
| 165 | + styles.backRightBtnRight, |
| 166 | + ]} |
| 167 | + onPress={onDelete} |
| 168 | + > |
| 169 | + <Animated.View |
| 170 | + style={[ |
| 171 | + styles.trash, |
| 172 | + { |
| 173 | + transform: [ |
| 174 | + { |
| 175 | + scale: swipeAnimatedValue.interpolate( |
| 176 | + { |
| 177 | + inputRange: [-90, -45], |
| 178 | + outputRange: [1, 0], |
| 179 | + extrapolate: 'clamp', |
| 180 | + } |
| 181 | + ), |
| 182 | + }, |
| 183 | + ], |
| 184 | + }, |
| 185 | + ]} |
| 186 | + > |
| 187 | + <Image |
| 188 | + source={require('../images/trash.png')} |
| 189 | + style={styles.trash} |
| 190 | + /> |
| 191 | + </Animated.View> |
| 192 | + </TouchableOpacity> |
| 193 | + </Animated.View> |
| 194 | + )} |
| 195 | + </Animated.View> |
| 196 | + ); |
| 197 | + }; |
| 198 | + |
| 199 | + const renderHiddenItem = (data, rowMap) => { |
| 200 | + const rowActionAnimatedValue = new Animated.Value(75); |
| 201 | + const rowHeightAnimatedValue = new Animated.Value(50); |
| 202 | + return ( |
| 203 | + <HiddenItemWithActions |
| 204 | + data={data} |
| 205 | + rowMap={rowMap} |
| 206 | + rowActionAnimatedValue={rowActionAnimatedValue} |
| 207 | + rowHeightAnimatedValue={rowHeightAnimatedValue} |
| 208 | + onClose={() => closeRow(rowMap, data.item.key)} |
| 209 | + onDelete={() => deleteRow(rowMap, data.item.key)} |
| 210 | + /> |
| 211 | + ); |
| 212 | + }; |
| 213 | + |
| 214 | + return ( |
| 215 | + <View style={styles.container}> |
| 216 | + <SwipeListView |
| 217 | + data={listData} |
| 218 | + renderItem={renderItem} |
| 219 | + renderHiddenItem={renderHiddenItem} |
| 220 | + onRowDidOpen={onRowDidOpen} |
| 221 | + leftOpenValue={75} |
| 222 | + rightOpenValue={-150} |
| 223 | + leftActivationValue={100} |
| 224 | + rightActivationValue={-200} |
| 225 | + leftActionValue={0} |
| 226 | + rightActionValue={-500} |
| 227 | + onLeftAction={onLeftAction} |
| 228 | + onRightAction={onRightAction} |
| 229 | + onLeftActionStatusChange={onLeftActionStatusChange} |
| 230 | + onRightActionStatusChange={onRightActionStatusChange} |
| 231 | + /> |
| 232 | + </View> |
| 233 | + ); |
| 234 | +} |
| 235 | + |
| 236 | +const styles = StyleSheet.create({ |
| 237 | + container: { |
| 238 | + backgroundColor: 'white', |
| 239 | + flex: 1, |
| 240 | + }, |
| 241 | + backTextWhite: { |
| 242 | + color: '#FFF', |
| 243 | + }, |
| 244 | + rowFront: { |
| 245 | + alignItems: 'center', |
| 246 | + backgroundColor: '#CCC', |
| 247 | + borderBottomColor: 'black', |
| 248 | + borderBottomWidth: 1, |
| 249 | + justifyContent: 'center', |
| 250 | + height: 50, |
| 251 | + width: '100%', |
| 252 | + }, |
| 253 | + rowBack: { |
| 254 | + alignItems: 'center', |
| 255 | + backgroundColor: '#DDD', |
| 256 | + flex: 1, |
| 257 | + flexDirection: 'row', |
| 258 | + justifyContent: 'space-between', |
| 259 | + paddingLeft: 15, |
| 260 | + }, |
| 261 | + backRightBtn: { |
| 262 | + alignItems: 'flex-end', |
| 263 | + bottom: 0, |
| 264 | + justifyContent: 'center', |
| 265 | + position: 'absolute', |
| 266 | + top: 0, |
| 267 | + width: 75, |
| 268 | + paddingRight: 17, |
| 269 | + }, |
| 270 | + backRightBtnLeft: { |
| 271 | + backgroundColor: 'blue', |
| 272 | + right: 75, |
| 273 | + }, |
| 274 | + backRightBtnRight: { |
| 275 | + backgroundColor: 'red', |
| 276 | + right: 0, |
| 277 | + }, |
| 278 | + trash: { |
| 279 | + height: 25, |
| 280 | + width: 25, |
| 281 | + marginRight: 7, |
| 282 | + }, |
| 283 | +}); |
0 commit comments