@@ -76,124 +76,106 @@ Gesture handlers can sometimes capture a gesture unintentionally. If you are usi
7676
7777### Example
7878
79- ``` javascript
80- import React from " react" ;
79+ https://snack.expo.io/@computerjazz/swipeable-draggable-list
80+
81+ ``` typescript
82+ import React from ' react' ;
8183import {
8284 Text ,
8385 View ,
8486 StyleSheet ,
8587 FlatList ,
8688 LayoutAnimation ,
89+ TouchableOpacity ,
8790 Platform ,
8891 UIManager ,
89- TouchableOpacity ,
90- Dimensions
91- } from " react-native" ;
92-
93- const { width } = Dimensions .get (" window" );
94-
95- import { TouchableOpacity as RNGHTouchableOpacity } from " react-native-gesture-handler" ;
96- import Animated from " react-native-reanimated" ;
97- import SwipeableItem from ' react-native-swipeable-item' ;
98- import DraggableFlatList from " react-native-draggable-flatlist" ;
92+ } from ' react-native' ;
93+ import Animated from ' react-native-reanimated' ;
94+ import SwipeableItem , { UnderlayParams } from ' react-native-swipeable-item' ;
95+ import DraggableFlatList , {
96+ RenderItemParams ,
97+ } from ' react-native-draggable-flatlist' ;
9998const { multiply, sub } = Animated ;
10099
101- const isAndroid = Platform .OS === " android" ;
102-
103- if (isAndroid && UIManager .setLayoutAnimationEnabledExperimental ) {
104- UIManager .setLayoutAnimationEnabledExperimental (true );
100+ if (Platform .OS === ' android' ) {
101+ UIManager .setLayoutAnimationEnabledExperimental &&
102+ UIManager .setLayoutAnimationEnabledExperimental (true );
105103}
106104
107- const PlatformTouchable = isAndroid ? TouchableOpacity : TouchableOpacity;
108-
109- const NUM_ITEMS = 3 ;
110- function getColor (i ) {
105+ const NUM_ITEMS = 20 ;
106+ function getColor(i : number ) {
111107 const multiplier = 255 / (NUM_ITEMS - 1 );
112108 const colorVal = i * multiplier ;
113109 return ` rgb(${colorVal }, ${Math .abs (128 - colorVal )}, ${255 - colorVal }) ` ;
114110}
115111
116- const initialData = [... Array (NUM_ITEMS )].fill (0 ).map ((d , index ) => ({
117- text: ` Row ${ index} ` ,
118- key: ` key-${ index} ` , // Note: It's bad practice to use index as your key. Don't do it in production!
119- backgroundColor: getColor (index),
120- hasLeft: index % 3 === 0 || index % 3 === 1 ,
121- hasRight: index % 3 === 0 || index % 3 === 2
122- }));
112+ type Item = {
113+ key: string ;
114+ text: string ;
115+ backgroundColor: string ;
116+ height: number ;
117+ };
118+
119+ const initialData: Item [] = [... Array (NUM_ITEMS )].fill (0 ).map ((d , index ) => {
120+ const backgroundColor = getColor (index );
121+ return {
122+ text: ` Row ${index } ` ,
123+ key: ` key-${backgroundColor } ` ,
124+ backgroundColor ,
125+ height: 100 ,
126+ };
127+ });
123128
124129class App extends React .Component {
125130 state = {
126- data: initialData
131+ data: initialData ,
127132 };
128133
129134 itemRefs = new Map ();
130135
131- deleteItem = item => {
132- const updatedData = this .state .data .filter (d => d !== item);
136+ deleteItem = ( item : Item ) => {
137+ const updatedData = this .state .data .filter (( d ) => d !== item );
133138 // Animate list to close gap when item is deleted
134139 LayoutAnimation .configureNext (LayoutAnimation .Presets .spring );
135140 this .setState ({ data: updatedData });
136141 };
137142
138- renderUnderlayLeft = ({ item, percentOpen }) => (
143+ renderUnderlayLeft = ({ item , percentOpen }: UnderlayParams < Item > ) => (
139144 < Animated .View
140145 style = {[styles .row , styles .underlayLeft , { opacity: percentOpen }]} // Fade in on open
141146 >
142- < PlatformTouchable onPressOut= {() => this .deleteItem (item . item )}>
147+ <TouchableOpacity onPressOut={() => this.deleteItem(item)}>
143148 <Text style={styles.text}>{`[x ]`}</Text>
144- < / PlatformTouchable >
149+ </TouchableOpacity >
145150 </Animated.View>
146151 );
147152
148- renderUnderlayRight = ({ item, percentOpen, close }) => (
153+ renderUnderlayRight = ({
154+ item,
155+ percentOpen,
156+ open,
157+ close,
158+ }: UnderlayParams < Item > ) => (
149159 < Animated .View
150160 style = {[
151161 styles .row ,
152162 styles .underlayRight ,
153163 {
154- transform: [{ translateX: multiply (sub (1 , percentOpen), - 100 ) }] // Translate from left on open
155- }
156- ]}
157- >
158- < PlatformTouchable onPressOut= {close}>
164+ transform: [{ translateX: multiply (sub (1 , percentOpen ), - 100 ) }], // Translate from left on open
165+ },
166+ ]}>
167+ <TouchableOpacity onPressOut={close}>
159168 <Text style={styles.text}>CLOSE</Text>
160- < / PlatformTouchable >
169+ </TouchableOpacity >
161170 </Animated.View>
162171 );
163172
164- renderOverlay = ({ item, openLeft, openRight, openDirection, close }) => {
165- const { text , backgroundColor , hasLeft , hasRight } = item .item ;
166- return (
167- < View style= {[styles .row , { backgroundColor }]}>
168- < View style= {[styles .flex , { alignItems: " flex-start" }]}>
169- {hasRight && (
170- < PlatformTouchable
171- onPressOut= {!! openDirection ? close : () => openRight (1 )}
172- >
173- < Text style= {styles .text }> {` <` }< / Text >
174- < / PlatformTouchable>
175- )}
176- < / View>
177- < PlatformTouchable style= {styles .flex } onLongPress= {item .drag }>
178- < Text style= {styles .text }> {text}< / Text >
179- < / PlatformTouchable>
180- < View style= {[styles .flex , { alignItems: " flex-end" }]}>
181- {hasLeft && (
182- < PlatformTouchable onPressOut= {!! openDirection ? close : openLeft}>
183- < Text style= {styles .text }> {` >` }< / Text >
184- < / PlatformTouchable>
185- )}
186- < / View>
187- < / View>
188- );
189- };
190-
191- renderItem = ({ item, index, drag }) => {
173+ renderItem = ({ item, index, drag }: RenderItemParams < Item > ) => {
192174 return (
193175 < SwipeableItem
194176 key = {item.key }
195- item= {{ item, drag } }
196- ref= {ref => {
177+ item = {item }
178+ ref = {( ref) => {
197179 if (ref && ! this .itemRefs .get (item .key )) {
198180 this .itemRefs .set (item .key , ref );
199181 }
@@ -206,25 +188,33 @@ class App extends React.Component {
206188 });
207189 }
208190 }}
209- overSwipe= {50 }
191+ overSwipe = {20 }
210192 renderUnderlayLeft = {this.renderUnderlayLeft }
211- snapPointsLeft= {item .hasLeft ? [100 ] : undefined }
212193 renderUnderlayRight = {this.renderUnderlayRight }
213- snapPointsRight= {item .hasRight ? [100 , width] : undefined }
214- renderOverlay= {this .renderOverlay }
215- / >
194+ snapPointsLeft = {[150 ]}
195+ snapPointsRight={[175 ]}>
196+ <View
197+ style={[
198+ styles .row ,
199+ { backgroundColor: item .backgroundColor , height: item .height },
200+ ]}>
201+ <TouchableOpacity onLongPress={drag}>
202+ <Text style={styles.text}>{item.text}</Text>
203+ </TouchableOpacity>
204+ </View>
205+ </SwipeableItem>
216206 );
217207 };
218208
219209 render() {
220210 return (
221211 <View style={styles.container}>
222212 <DraggableFlatList
223- activationDistance= {15 }
224- keyExtractor= {item => item .key }
213+ keyExtractor={(item) => item.key}
225214 data={this.state.data}
226215 renderItem={this.renderItem}
227216 onDragEnd={({ data }) => this.setState({ data })}
217+ activationDistance={20}
228218 />
229219 </View>
230220 );
@@ -235,32 +225,30 @@ export default App;
235225
236226const styles = StyleSheet.create({
237227 container: {
238- flex: 1
239- },
240- flex: {
241- flex: 1
228+ flex: 1 ,
242229 },
243230 row: {
244- flexDirection: " row" ,
231+ flexDirection: ' row' ,
245232 flex: 1 ,
246- alignItems: " center" ,
247- justifyContent: " space-around " ,
248- padding: 15
233+ alignItems: ' center' ,
234+ justifyContent: ' center ' ,
235+ padding: 15 ,
249236 },
250237 text: {
251- fontWeight: " bold" ,
252- color: " white" ,
253- fontSize: 32
238+ fontWeight: ' bold' ,
239+ color: ' white' ,
240+ fontSize: 32 ,
254241 },
255242 underlayRight: {
256243 flex: 1 ,
257- backgroundColor: " teal" ,
258- justifyContent: " flex-start"
244+ backgroundColor: ' teal' ,
245+ justifyContent: ' flex-start' ,
259246 },
260247 underlayLeft: {
261248 flex: 1 ,
262- backgroundColor: " tomato" ,
263- justifyContent: " flex-end"
264- }
249+ backgroundColor: ' tomato' ,
250+ justifyContent: ' flex-end' ,
251+ },
265252});
253+
266254` ` `
0 commit comments