react-native TreeView (树视图) 实现

QQ20181023-163722-HD.gif

Tree View:

/**
 * create: 2018-10-23 (10:40 早)
 * email : [email protected]
 */


import React, {Component} from 'react'
import {
    View,
    FlatList,
    Platform,
    LayoutAnimation,
    UIManager,
    Animated,
    Text,
    Image,
    TouchableOpacity
} from "react-native";

export default class NurTreeView extends Component {


    propTypes: {
        data: PropTypes.isRequired,//array([])
        onItemClick: PropTypes.isRequired,//func
        titleKey: PropTypes.string,
    };


    /**
     * render flatList item
     * @param item
     * @param index
     * @param style
     * @returns {*}
     */
    renderItem(item, index, style) {

        return (
             this.props.onItemClick && this.props.onItemClick(item)}
                style={{
                    justifyContent: 'space-between',
                    flexDirection: 'row',
                    alignItems: 'center',
                }}
            >

                
                    
                    {item[this.props.titleKey]}
                


                

            

            {/**items*/}
            {item.isOpen ? this.renderFlatList(item.data,
                ({item, index}) => this.renderItem(item, index, {
                    paddingLeft: 20,
                })) : null}

        );
    }


    /**
     * render FlatList
     */
    renderFlatList(data, renderItem) {
        return 
    }

    render() {
        return (

            {this.renderFlatList(this.props.data,
                ({item, index}) => this.renderItem(item, index, {
                    marginTop: 8,
                    backgroundColor: index % 2 === 0 ? '#006c9b' : '#009a4e',
                    paddingRight: 16
                }))}

        )
    }
}

使用方式:

import React, {Component} from 'react'
import {View} from "react-native";
import NurTreeView from "../../utils/NurTreeView";


export default class TreeViewPage extends Component {

    // 构造
    constructor(props) {
        super(props);

        let _data = [];
        for (let i = 0; i < 20; i++) {
            _data.push({name: 'this is a title' + i, isOpen: false, data: []})
        }

        // 初始状态
        this.state = {
            data: _data,
        };
    }


    /**
     * 点击item
     * */
    onMoreClick(item) {
        item.isOpen = !item.isOpen;
        item.data = [
            {
                name: "item name",
                isOpen: false,
                data: []
            },
            {
                name: "item name",
                isOpen: false,
                data: []
            },
        ];

        this.setState({
            test: 0,
        })
    }

    render() {

        return (

            {/**tree view*/}
             this.onMoreClick(item)}
                titleKey={'name'}/>
        )
    }

}

你可能感兴趣的:(react-native TreeView (树视图) 实现)