Giter Site home page Giter Site logo

randomer / react-native-draggable-flatlist Goto Github PK

View Code? Open in Web Editor NEW

This project forked from computerjazz/react-native-draggable-flatlist

0.0 1.0 0.0 216 KB

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

License: MIT License

TypeScript 100.00%

react-native-draggable-flatlist's Introduction

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

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. npm install or yarn add react-native-draggable-flatlist
  3. import DraggableFlatList from 'react-native-draggable-flatlist'

Api

Props

All props are spread onto underlying FlatList

Name Type Description
data T[] Items to be rendered.
horizontal boolean Orientation of list.
renderItem (params: { item: T, index: number, drag: () => void, isActive: boolean}) => JSX.Element Call drag when the row should become active (i.e. in an onLongPress or onPressIn).
keyExtractor (item: T, index: number) => string Unique key for each item
onDragBegin (index: number) => void Called when row becomes active.
onRelease (index: number) => void Called when active row touch ends.
onDragEnd (params: { data: T[], from: number, to: number }) => void Called after animation has completed. Returns updated ordering of data
autoscrollThreshold number Distance from edge of container where list begins to autoscroll when dragging.
autoscrollSpeed number Determines how fast the list autoscrolls.
onRef (ref: React.RefObject<DraggableFlatList<T>>) => void Returns underlying Animated FlatList ref.
animationConfig Partial<Animated.SpringConfig> Configure list animations. See reanimated spring config

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

react-native-draggable-flatlist's People

Contributors

aeisenberger avatar buzzhoot avatar computerjazz avatar frankyfrankfrank avatar kevgrig avatar magrinj avatar mortenjoe avatar nsainaney avatar rgoldiez avatar websurgeon avatar ymatsushita2019 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.