0.1.3 • Published 6 years ago

react-native-sortable v0.1.3

Weekly downloads
1
License
Apache-2.0
Repository
-
Last release
6 years ago

Sortable

Install

npm install --save react-native-sortable

Usage

state = {
    friends: Array(3).fill().map(_=> Math.random().toString(36).slice(2, 7))
  };

  render() {
    return (
      <Sortable 
        style={styles.container}
        onChangeChildIndex={this._onChangeChildIndex}
      >
        {this.state.friends.map((_, key)=> (
          <Sortable.Item key={key} style={styles.item}>
            <Text style={styles.instructions}>{_ }</Text>
          </Sortable.Item>
        ))}
      </Sortable>
    );
  }

  _onChangeChildIndex = ({oldIndex, newIndex})=> {

    const list = this.state.friends;
    var newlist = list;
    if (newIndex < oldIndex) {
      newlist = list.slice(0, newIndex).concat([list[oldIndex]]).concat(list.slice(newIndex, oldIndex)).concat(list.slice(oldIndex+1));
    }else if (newIndex > oldIndex){
      newlist = list.slice(0, oldIndex).concat(list.slice(oldIndex+1, newIndex+1)).concat([list[oldIndex]]).concat(list.slice(newIndex+1));
    }

    this.setState({
      friends: newlist
    })
  }