TagItemView.js 11.7 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TextInput,
    TouchableOpacity, 
    Image, 
    ScrollView, 
    Modal, 
    StyleSheet
} from 'react-native';

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

const defaultHeight = (30);
const HPading = (16);
const VPading = (6);

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

    constructor(props) {
        super(props);
        this.state = {
            isAddTag:false,
            showModal:false,
            tagDataList:[{tagColor: 'rgba(255, 195, 0, 1)', tagName: '一般',checked: true},{tagColor: 'rgba(255, 195, 0, 1)', tagName: '困难',checked: false}],// 标签列表
        }
        this.attrData = this.props.attrData;
    }

    componentWillMount() {
    }

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

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

    renderTagItem(value, index) {
        return (
            <TouchableOpacity 
                style={{flexDirection: 'row', width: '100%', alignItems: 'center'}}
                activeOpacity={0.8}
                onPress={()=>{
                    value.checked = !value.checked;
                    this.setState({
                        updateCheck: false
                    })
                }}
            >
                <View style={{backgroundColor: value.tagColor, width: 24, height: 24, borderRadius: 4}}/>
                <Text style={{fontSize: 14, color: 'rgba(0, 0, 0 , 1)', marginLeft: 10}}>{value.tagName}</Text>
                <View style={{flex: 1, justifyContent: 'flex-end'}}>
                    <Image style={{width: 20,height: 20,marginLeft: 8,marginRight: 8}} 
                        source={value.checked ? checkedImg : unCheckedImg} resizeMode={'contain'}/>
                </View>
            </TouchableOpacity>
        );
    }

    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.tagDataList && this.state.tagDataList.length > 0 &&
                                <View>
                                    <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 && 
                                <View style={{width: '100%', borderRadius: 8, borderWidth: 1, borderColor: 'rgba(0,0,0,0.5)', flexDirection: 'row'}}>
                                    <View style={{backgroundColor: 'rgba(255, 195, 0, 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>
                            }
                            {this.state.isAddTag &&
                                <View style={{width: '100%', backgroundColor:'rgba(245, 245, 245, 1)', position: 'absolute', bottom: 0, flexDirection: 1}}>
                                    <TouchableOpacity
                                        style={{paddingVertical: 6, paddingHorizontal: 12}}
                                        activeOpacity={0.8}
                                        onPress={()=>{
                                            this.setState({
                                                isAddTag: false
                                            })
                                        }}
                                    >
                                        <Text style={{fontSize: 14, color: 'rgba(0, 0, 0, 1)'}}>取消</Text>
                                    </TouchableOpacity>
                                    <View style={{flex: 1}}/>
                                    <TouchableOpacity
                                        style={{paddingVertical: 6, paddingHorizontal: 12}}
                                        activeOpacity={0.8}
                                        onPress={()=>{
                                            this.setState({
                                                isAddTag: false
                                            })
                                        }}
                                    >
                                        <Text style={{fontSize: 14, color: 'rgba(0, 0, 0, 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
    },
});