TagItemView.js 14.7 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TextInput,
    TouchableOpacity,
    Image,
    ScrollView,
    Modal,
    StyleSheet
} from 'react-native';
import {SwipeRow} from "react-native-swipe-list-view";
import {getFooterBottom} from "../utils/utils"

const checkedImg = require("../img/checked.png");
const unCheckedImg = require("../img/dati-xuanze-02.png");

/**
 * 标签
 */
export default class TagItemView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            isAddTag:false,// 新增
            isEditTag:false,// 编辑标签
            showModal:false,
            editTagData: null,// 编辑标签数据
            tagDataList:[{tagColor: 'rgba(255, 195, 0, 1)', tagName: '一般',checked: true},{tagColor: 'rgba(255, 195, 0, 1)', tagName: '困难',checked: false}],// 标签列表
        }
        this.attrData = this.props.attrData;
        this.swipeRowIndex = '';// 侧滑下标
    }

    componentWillMount() {
    }

    showModal() {
        this.setState({
            showModal: true,
        });
    }

    /**
     * 模态框取消操作
     */
    closeModal() {
        this.setState({
            showModal: false,
        });
    }

    /**
     * 标签item渲染
     */
    renderTagItem(value, index) {
        return (
            <SwipeRow
                key={"SwipeRow"+index}
                ref={'SwipeRow'+index}
                leftOpenValue={138}
                rightOpenValue={-138}
                stopRightSwipe={-138}
                disableRightSwipe={true}   //禁止向右滑动
                onRowOpen={(rowData, rowMap) =>{
                    // 打开侧滑
                    if (this.swipeRowIndex !== 'SwipeRow'+index && this.swipeRowIndex !== '') {
                        this.refs[this.swipeRowIndex].closeRow();
                    }
                    this.swipeRowIndex = 'SwipeRow'+index;
                }}
            >
                <View style={{width: 138, height: 48, alignItems: 'center', flexDirection: 'row', position: 'absolute', right: 0}}>
                    <TouchableOpacity
                        style={{width: 69, height: 48, backgroundColor: 'rgba(255, 141, 26, 1)', justifyContent: 'center', alignItems: 'center'}}
                        activeOpacity={0.8}
                        onPress={() => {
                            this.setState({
                                isEditTag: true
                            });
                        }}
                    >
                        <Text style={{fontSize: 14, color: 'rgba(255, 255, 255, 1)'}} allowFontScaling={false}>编辑</Text>
                    </TouchableOpacity>
                    <TouchableOpacity
                        style={{width: 69, height: 48, backgroundColor: 'rgba(212, 48, 48, 1)', justifyContent: 'center', alignItems: 'center'}}
                        activeOpacity={0.8}
                        onPress={() => {
                            let tempList = this.state.tagDataList.splice(0, 1, value);
                            this.setState({
                                tagDataList: tempList
                            });
                            this.swipeRowIndex = '';
                        }}
                    >
                        <Text style={{fontSize: 14, color: 'rgba(255, 255, 255, 1)'}} allowFontScaling={false}>删除</Text>
                    </TouchableOpacity>
                </View>
                <TouchableOpacity
                    activeOpacity={1}
                    onPress={()=>{
                        value.checked = !value.checked;
                        this.setState({
                            updateCheck: false
                        });
                        if (!!this.swipeRowIndex && this.swipeRowIndex !== '') {
                            this.refs[this.swipeRowIndex].closeRow();
                        }
                    }}
                >
                    <View style={{height: 48, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 20, backgroundColor: 'white'}}>
                        <View style={{backgroundColor: value.tagColor, width: 24, height: 24, borderRadius: 4}}/>
                        <Text style={{flex: 1, fontSize: 14, color: 'rgba(0, 0, 0 , 1)', marginLeft: 10}}>{value.tagName}</Text>
                        <Image style={{width: 20,height: 20}} source={value.checked ? checkedImg : unCheckedImg} resizeMode={'contain'}/>
                    </View>
                </TouchableOpacity>
            </SwipeRow>
        );
    }

    render() {
        let {name, labelName, labelColor} = this.attrData;

        return (
            <View>
                <TouchableOpacity
                    style={{width: '100%', backgroundColor: 'white', flexDirection: 'row', paddingVertical: 10, paddingHorizontal: 6, alignItems: 'center'}}
                    activeOpacity={0.8}
                    onPress={()=> {
                        this.showModal();
                    }}
                >
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>{name|| '标签'}</Text>
                    <View style={{flex: 1, justifyContent: 'flex-end', alignItems: 'center', flexDirection: 'row'}}>
                        {!!labelName && labelName.length > 0 &&
                        <Text
                            style={{
                                fontSize: 14,
                                color: 'white',
                                backgroundColor: labelColor||'rgba(255, 141, 26, 1)',
                                paddingVertical: 3,
                                paddingHorizontal: 10,
                                borderRadius: 4
                            }}
                        >{labelName}</Text>}
                        {labelName === undefined || labelName.length === 0 &&
                        <Text style={{fontSize: 14, color: 'rgba(166, 166, 166, 1)'}}>添加标签</Text>}
                        <Image style={{width: 16, height: 16, marginLeft: 10}} source={require('../img/bread.png')} resizeMode={'contain'}/>
                    </View>
                </TouchableOpacity>
                <Modal animationType={"fade"} transparent={true} visible={this.state.showModal} onRequestClose={() => this.closeModal()}>
                    {/** 目录弹窗 */}
                    <View style={{flex: 1}}>
                        <TouchableOpacity style={styles.modalBg} activeOpacity={1} onPress={() => this.closeModal()}/>
                        {/** 实体内容 */}
                        <View style={styles.contentBg}>
                            {/** 标题栏 */}
                            <View style={styles.titleBar}>
                                <TouchableOpacity
                                    style={{paddingLeft: 15, paddingRight: 5}}
                                    activeOpacity={0.8}
                                    onPress={() => this.closeModal()}
                                >
                                    <Image style={{width: 14, height: 14}}
                                           source={require('../img/loadBack.png')}/>
                                </TouchableOpacity>
                                <Text style={styles.modelTitle}>添加标签</Text>
                                <TouchableOpacity
                                    style={{
                                        justifyContent: "center",
                                        alignItems: "center",
                                        paddingVertical: 3,
                                        paddingHorizontal: 8,
                                    }}
                                    activeOpacity={0.8}
                                    onPress={() => {
                                        //this.props.navigation.navigate("CreateTagView");
                                        //this.closeModal();
                                        // 新增标签
                                        this.setState({
                                            isAddTag: true
                                        })
                                    }}
                                >
                                    <Text style={{fontSize: 16, color: global.homeColor}}>+新增</Text>
                                </TouchableOpacity>
                            </View>
                            <View style={{width: '100%', height: 1, backgroundColor: 'rgba(245,245,245,1)'}}/>
                            {/** 内容 */}
                            {(!this.state.isAddTag && !this.state.isEditTag) && !!this.state.tagDataList && this.state.tagDataList.length > 0 &&
                                <View style={{flex: 1}}>
                                    <ScrollView style={{flex: 1}} horizontal={false} showsVerticalScrollIndicator={false}>
                                        {this.state.tagDataList && this.state.tagDataList.map((value, index) => this.renderTagItem(value, index))}
                                    </ScrollView>
                                    <TouchableOpacity
                                        style={{
                                            width: '100%',
                                            backgroundColor: global.homeColor,
                                            justifyContent: 'center',
                                            alignItems: 'center',
                                            position: 'absolute',
                                            bottom: 0,
                                            height: 46+getFooterBottom(),
                                            paddingBottom: getFooterBottom()
                                        }}
                                        activeOpacity={0.8}
                                        onPress={()=>{}}
                                    >
                                        <Text style={{fontSize: 16, color: 'rgba(242, 244, 245, 1)'}}>确定</Text>
                                    </TouchableOpacity>
                                </View>
                            }
                            {/** 新增 */}
                            {(this.state.isAddTag || this.state.isEditTag) &&
                                <View style={{width: '100%'}}>
                                    <View style={{borderRadius: 4, borderWidth: 1, borderColor: 'rgba(229, 229, 229, 1)', alignItems: 'center', flexDirection: 'row', margin: 15}}>
                                        <View style={{marginLeft: 5, backgroundColor: 'rgba(255, 141, 26, 1)', width: 24, height: 24, borderRadius: 4}}/>
                                        <TextInput
                                            style={{
                                                flex: 1,
                                                fontSize: 16,
                                                color: 'rgba(0, 0, 0, 1)',
                                                backgroundColor: 'white',
                                                textAlign: 'left',
                                                marginLeft: 15,
                                                paddingVertical: 6,
                                                paddingHorizontal: 0
                                            }}
                                            maxLength={10}
                                            onChangeText={text => {
                                                if (this.onChangeText) {
                                                    this.onChangeText(text)
                                                }
                                            }}
                                            placeholderTextColor={'#999'}
                                            placeholder={'标签1'}
                                            underlineColorAndroid="transparent"
                                        />
                                    </View>
                                </View>
                            }
                            {(this.state.isAddTag || this.state.isEditTag) &&
                                <View style={{width: '100%', backgroundColor:'rgba(240, 242, 245, 1)', position: 'absolute', bottom: 0, flexDirection: 'row'}}>
                                    <TouchableOpacity
                                        style={{paddingVertical: 8, paddingHorizontal: 15}}
                                        activeOpacity={0.8}
                                        onPress={()=>{
                                            this.setState({
                                                isAddTag: false,
                                                isEditTag: false
                                            })
                                        }}
                                    >
                                        <Text style={{fontSize: 16, color: 'rgba(80, 80, 80, 1)'}}>取消</Text>
                                    </TouchableOpacity>
                                    <View style={{flex: 1}}/>
                                    <TouchableOpacity
                                        style={{paddingVertical: 6, paddingHorizontal: 12}}
                                        activeOpacity={0.8}
                                        onPress={()=>{
                                            this.setState({
                                                isAddTag: false,
                                                isEditTag: false
                                            })
                                        }}
                                    >
                                        <Text style={{fontSize: 16, color: 'rgba(80, 80, 80, 1)'}}>确认</Text>
                                    </TouchableOpacity>
                                </View>
                            }
                        </View>
                    </View>
                </Modal>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    modalBg: {
        backgroundColor: 'rgba(0,0,0,.5)',
        width: '100%',
        height: '100%',
        display: 'flex',
        position: 'absolute'
    },
    contentBg: {
        width: '100%',
        flex: 1,
        marginTop: 122,
        backgroundColor: "rgba(255, 255, 255, 1)",
        borderTopLeftRadius: 16,
        borderTopRightRadius: 16
    },
    titleBar: {
        flexDirection: "row",
        justifyContent: "flex-start",
        alignItems: "center",
        marginTop: 15,
        marginBottom: 10,
        marginRight: 15
    },
    modelTitle: {
        flex: 1,
        color: "rgba(0, 0, 0, 1)",
        fontSize: 16,
        textAlign: "center",
        justifyContent: "center"
    },
    checkedBox: {
        width: 20,
        height: 20,
        marginLeft: 8,
        marginRight: 8
    },
});