4.0.1 • Published 1 year ago

react-native-draggable-flatlist v4.0.1

Weekly downloads
15,148
License
MIT
Repository
github
Last release
1 year ago

⚠️ You are viewing the README for v4 (view v3, v2 )

React Native Draggable FlatList

A drag-and-drop-enabled FlatList component for React Native. Fully native interactions powered by Reanimated and React Native Gesture Handler. To use swipeable list items in a DraggableFlatList see React Native Swipeable Item.

Draggable FlatList demo

Install

  1. Follow installation instructions for reanimated and react-native-gesture-handler. RNGH may require you to make changes to MainActivity.java. Be sure to follow all Android instructions!
  2. Install this package using npm or yarn

with npm:

npm install --save react-native-draggable-flatlist

with yarn:

yarn add react-native-draggable-flatlist
  1. import DraggableFlatList from 'react-native-draggable-flatlist'

Api

Props

All props are spread onto underlying FlatList

NameTypeDescription
dataT[]Items to be rendered.
refReact.RefObject<FlatList<T>>FlatList ref to be forwarded to the underlying FlatList.
renderItem(params: { item: T, getIndex: () => number \| undefined, drag: () => void, isActive: boolean}) => JSX.ElementCall drag when the row should become active (i.e. in an onLongPress or onPressIn).
renderPlaceholder(params: { item: T, index: number }) => React.ReactNodeComponent to be rendered underneath the hovering component
keyExtractor(item: T, index: number) => stringUnique key for each item (required)
onDragBegin(index: number) => voidCalled when row becomes active.
onRelease(index: number) => voidCalled when active row touch ends.
onDragEnd(params: { data: T[], from: number, to: number }) => voidCalled after animation has completed. Returns updated ordering of data
autoscrollThresholdnumberDistance from edge of container where list begins to autoscroll when dragging.
autoscrollSpeednumberDetermines how fast the list autoscrolls.
animationConfigPartial<WithSpringConfig>Configure list animations. See reanimated spring config
activationDistancenumberDistance a finger must travel before the gesture handler activates. Useful when using a draggable list within a TabNavigator so that the list does not capture navigator gestures.
onScrollOffsetChange(offset: number) => voidCalled with scroll offset. Stand-in for onScroll.
onPlaceholderIndexChange(index: number) => voidCalled when the index of the placeholder changes
dragItemOverflowbooleanIf true, dragged item follows finger beyond list boundary.
dragHitSlopobject: {top: number, left: number, bottom: number, right: number}Enables control over what part of the connected view area can be used to begin recognizing the gesture. Numbers need to be non-positive (only possible to reduce responsive area).
debugbooleanEnables debug logging and animation debugger.
containerStyleStyleProp<ViewStyle>Style of the main component.
simultaneousHandlersReact.Ref<any> or React.Ref<any>[]References to other gesture handlers, mainly useful when using this component within a ScrollView. See Cross handler interactions.
itemEnteringAnimationReanimated AnimationBuilder (docs)Animation when item is added to list.
itemExitingAnimationReanimated AnimationBuilder (docs)Animation when item is removed from list.
itemLayoutAnimationReanimated AnimationBuilder (docs)Animation when list items change position (enableLayoutAnimationExperimental prop must be true).
enableLayoutAnimationExperimentalbooleanFlag to turn on experimental support for itemLayoutAnimation.

Cell Decorators

Cell Decorators are an easy way to add common hover animations. For example, wrapping renderItem in the <ScaleDecorator> component will automatically scale up the active item while hovering (see example below).

ScaleDecorator, ShadowDecorator, and OpacityDecorator are currently exported. Developers may create their own custom decorators using the animated values provided by the useOnCellActiveAnimation hook.

Nesting DraggableFlatLists

It's possible to render multiple DraggableFlatList components within a single scrollable parent by wrapping one or more NestableDraggableFlatList components within an outer NestableScrollContainer component.

NestableScrollContainer extends the ScrollView from react-native-gesture-handler, and NestableDraggableFlatList extends DraggableFlatList, so all available props may be passed into both of them.

Note: When using NestableDraggableFlatLists, all React Native warnings about nested list performance will be disabled.

import { NestableScrollContainer, NestableDraggableFlatList } from "react-native-draggable-flatlist"

...

  const [data1, setData1] = useState(initialData1);
  const [data2, setData2] = useState(initialData2);
  const [data3, setData3] = useState(initialData3);

  return (
    <NestableScrollContainer>
      <Header text='List 1' />
      <NestableDraggableFlatList
        data={data1}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        onDragEnd={({ data }) => setData1(data)}
      />
      <Header text='List 2' />
      <NestableDraggableFlatList
        data={data2}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        onDragEnd={({ data }) => setData2(data)}
      />
      <Header text='List 3' />
      <NestableDraggableFlatList
        data={data3}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        onDragEnd={({ data }) => setData3(data)}
      />
    </NestableScrollContainer>
  )

Nested DraggableFlatList demo

Example

Example snack: https://snack.expo.dev/@computerjazz/draggable-flatlist-examples

import React, { useState } from "react";
import { Text, View, StyleSheet, TouchableOpacity } from "react-native";
import DraggableFlatList, {
  ScaleDecorator,
} from "react-native-draggable-flatlist";

const NUM_ITEMS = 10;
function getColor(i: number) {
  const multiplier = 255 / (NUM_ITEMS - 1);
  const colorVal = i * multiplier;
  return `rgb(${colorVal}, ${Math.abs(128 - colorVal)}, ${255 - colorVal})`;
}

type Item = {
  key: string;
  label: string;
  height: number;
  width: number;
  backgroundColor: string;
};

const initialData: Item[] = [...Array(NUM_ITEMS)].map((d, index) => {
  const backgroundColor = getColor(index);
  return {
    key: `item-${index}`,
    label: String(index) + "",
    height: 100,
    width: 60 + Math.random() * 40,
    backgroundColor,
  };
});

export default function App() {
  const [data, setData] = useState(initialData);

  const renderItem = ({ item, drag, isActive }: RenderItemParams<Item>) => {
    return (
      <ScaleDecorator>
        <TouchableOpacity
          onLongPress={drag}
          disabled={isActive}
          style={[
            styles.rowItem,
            { backgroundColor: isActive ? "red" : item.backgroundColor },
          ]}
        >
          <Text style={styles.text}>{item.label}</Text>
        </TouchableOpacity>
      </ScaleDecorator>
    );
  };

  return (
    <DraggableFlatList
      data={data}
      onDragEnd={({ data }) => setData(data)}
      keyExtractor={(item) => item.key}
      renderItem={renderItem}
    />
  );
}

const styles = StyleSheet.create({
  rowItem: {
    height: 100,
    width: 100,
    alignItems: "center",
    justifyContent: "center",
  },
  text: {
    color: "white",
    fontSize: 24,
    fontWeight: "bold",
    textAlign: "center",
  },
});
4.0.1

1 year ago

4.0.0

1 year ago

4.0.0-beta.11

2 years ago

4.0.0-beta.10

2 years ago

4.0.0-beta.12

2 years ago

4.0.0-beta.8

2 years ago

4.0.0-beta.7

2 years ago

3.1.2

2 years ago

4.0.0-beta.6

2 years ago

4.0.0-beta.5

2 years ago

4.0.0-beta.4

2 years ago

4.0.0-beta.3

2 years ago

4.0.0-beta.2

2 years ago

4.0.0-beta.1

2 years ago

4.0.0-beta.0

2 years ago

4.0.0-beta.9

2 years ago

3.0.7

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.4

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

3.0.0-beta.10

2 years ago

3.0.0-beta.11

2 years ago

3.0.0-beta.12

2 years ago

3.0.0-beta.13

2 years ago

3.0.0-beta.9

2 years ago

3.0.0-beta.8

2 years ago

2.6.2

3 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.6

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.5

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.0

4 years ago

2.3.6

4 years ago

2.3.5

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.11

4 years ago

2.0.12

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago