Socket
Socket
Sign inDemoInstall

draggable-flatlist

Package Overview
Dependencies
527
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    draggable-flatlist

A drag-and-drop-enabled FlatList component for React Native


Version published
Weekly downloads
129
increased by578.95%
Maintainers
1
Install size
113 kB
Created
Weekly downloads
 

Readme

Source

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 requires 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.
horizontalbooleanOrientation of list.
renderItem(params: { item: T, index: number, drag: () => void, isActive: boolean}) => JSX.ElementCall drag when the row should become active (i.e. in an onLongPress or onPressIn).
keyExtractor(item: T, index: number) => stringUnique key for each item
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.
onRef(ref: React.RefObject<DraggableFlatList<T>>) => voidReturns underlying Animated FlatList ref.
animationConfigPartial<Animated.SpringConfig>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.
layoutInvalidationKeystringChanging this value forces a remeasure of all item layouts. Useful if item size/ordering updates after initial mount.
onScrollOffsetChange(offset: number) => voidCalled with scroll offset. Stand-in for onScroll.
refreshControlReact ComponentFor iOS you can use standart RefreshControl component. For android you should use custom implementataion. You can left this field undefined, default implementation will be used. Se example for more details.
refreshingbooleanAndroid only! Set this true while waiting for new data from a refresh.
onRefresh() => voidAndroid only! Function that will be called when user pulls down for refresh.
refreshControlOffsetnumberAndroid only! Padding from top for flatlist container for pull to refresh implementation.
NameTypeDescription
:-------------------------:----------------------------------------------------------------------------------------:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
dataT[]Items to be rendered.
horizontalbooleanOrientation of list.
renderItem(params: { item: T, index: number, 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
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.
onRef(ref: React.RefObject<DraggableFlatList<T>>) => voidReturns underlying Animated FlatList ref.
animationConfigPartial<Animated.SpringConfig>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.
layoutInvalidationKeystringChanging this value forces a remeasure of all item layouts. Useful if item size/ordering updates after initial mount.
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.

Example

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

const exampleData = [...Array(20)].map((d, index) => ({
  key: `item-${index}`, // For example only -- don't use index as your key!
  label: index,
  backgroundColor: `rgb(${Math.floor(Math.random() * 255)}, ${
    index * 5
  }, ${132})`,
}));

class Example extends Component {
  state = {
    data: exampleData,
  };

  renderItem = ({ item, index, drag, isActive }) => {
    return (
      <TouchableOpacity
        style={{
          height: 100,
          backgroundColor: isActive ? "blue" : item.backgroundColor,
          alignItems: "center",
          justifyContent: "center",
        }}
        onLongPress={drag}
      >
        <Text
          style={{
            fontWeight: "bold",
            color: "white",
            fontSize: 32,
          }}
        >
          {item.label}
        </Text>
      </TouchableOpacity>
    );
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <DraggableFlatList
          data={this.state.data}
          renderItem={this.renderItem}
          keyExtractor={(item, index) => `draggable-item-${item.key}`}
          onDragEnd={({ data }) => this.setState({ data })}
        />
      </View>
    );
  }
}

export default Example;

Example with custom pull to refresh for Android

import React, { Component } from "react";
import {
  View,
  TouchableOpacity,
  Text,
  RefreshControl,
  ActivityIndicator,
  Platform,
} from "react-native";
import DraggableFlatList from "react-native-draggable-flatlist";

const exampleData = [...Array(20)].map((d, index) => ({
  key: `item-${index}`, // For example only -- don't use index as your key!
  label: index,
  backgroundColor: `rgb(${Math.floor(Math.random() * 255)}, ${
    index * 5
  }, ${132})`,
}));

class Example extends Component {
  state = {
    data: exampleData,
    refreshing: false,
  };

  renderItem = ({ item, index, drag, isActive }) => {
    return (
      <TouchableOpacity
        style={{
          height: 100,
          backgroundColor: isActive ? "blue" : item.backgroundColor,
          alignItems: "center",
          justifyContent: "center",
        }}
        onLongPress={drag}
      >
        <Text
          style={{
            fontWeight: "bold",
            color: "white",
            fontSize: 32,
          }}
        >
          {item.label}
        </Text>
      </TouchableOpacity>
    );
  };

  onRefresh: () => {
    //update something
  };

  render() {
    const iOSrefreshControl = (
      <RefreshControl
        onRefresh={this.onRefresh}
        refreshing={this.state.refreshing}
      />
    );

    const androidRefreshControl = (
      <View
        style={{
          height: 50,
          zIndex: -1,
          position: "absolute",
          left: 0,
          right: 0,
          top: 0,
        }}
      >
        <View
          style={{
            flex: 1,
            justifyContent: "center",
            alignItems: "center",
          }}
        >
          <ActivityIndicator size="large" />
        </View>
      </View>
    );

    return (
      <View style={{ flex: 1 }}>
        <DraggableFlatList
          data={this.state.data}
          renderItem={this.renderItem}
          keyExtractor={(item, index) => `draggable-item-${item.key}`}
          onDragEnd={({ data }) => this.setState({ data })}
          refreshControl={
            Platform.OS === "ios" ? iOSrefreshControl : androidRefreshControl
          }
          onRefresh={this.onRefresh}
          refreshing={this.state.refreshing}
          refreshControlOffset={50}
        />
      </View>
    );
  }
}

export default Example;

Keywords

FAQs

Last updated on 28 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc