SelectColorModal.js 10.5 KB
import React, { Component } from 'react';
import {
    View,
    Text,
    StyleSheet,
    TouchableOpacity,
    Image,
    Modal, ScrollView, Slider
} from 'react-native';
import {width} from "../utils/getSize";

const defaultColor = '#FF8D1A';
const colorsys = require('colorsys');

export default class SelectColorModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
            showModal: false,
            selectColor: defaultColor,// 选择的颜色
            selectRValue: 0,// R值
            selectGValue: 0,// G值
            selectBValue: 0,// B值
        };
        this.onCallback = this.props.onCallback;
        this.selectColorList = ['#010101', '#3664FF', '#67C858',
            '#F6CC2A', '#F34434'];
    }

    resetInitColor(selectColor) {
        if (selectColor && selectColor.length > 0) {
            let rgbColorObj = colorsys.parseCss(selectColor);
            this.setState({
                selectColor: selectColor,
                selectRValue: rgbColorObj.r,
                selectGValue: rgbColorObj.g,// G值
                selectBValue: rgbColorObj.b,// B值
            })
        }
    }

    showSelectColorModal =(selectColor)=> {
        this.setState({
            showModal: true
        })
        this.resetInitColor(selectColor);
    }

    closeModal() {
        this.setState({
            showModal: false
        })
    }

    /**
     * 颜色item
     * @param value
     * @param index
     */
    renderSelectColorItem(value, index) {
        return (
            <TouchableOpacity
                key={index}
                style={{width: '20%', paddingTop: 20, justifyContent: 'center', alignItems: 'center'}}
                activeOpacity={0.8}
                onPress={()=>{
                    this.closeModal();
                    if (this.onCallback) {
                        this.onCallback(value)
                    }
                }}
            >
                <View style={{borderRadius: 20, width: 30, height: 30, backgroundColor: value}}/>
            </TouchableOpacity>
        );
    }

    render() {
        return (
            <Modal animationType={"fade"} transparent={true} visible={this.state.showModal} onRequestClose={() => {}}>
                {/** 目录弹窗 */}
                <View style={{flex: 1}}>
                    <TouchableOpacity style={styles.modalBg} activeOpacity={1} onPress={() => this.closeModal()}/>
                    {/** 实体内容 */}
                    <View style={styles.contentBg}>
                        {/** 标题栏 */}
                        <View style={styles.titleBar}>
                            <TouchableOpacity
                                style={{
                                    justifyContent: "center",
                                    alignItems: "center",
                                    paddingVertical: 3,
                                    paddingHorizontal: 15,
                                }}
                                activeOpacity={0.8}
                                onPress={() => this.closeModal()}
                            >
                                <Text style={{fontSize: 16, color: 'rgba(128, 128, 128, 1)'}}>取消</Text>
                            </TouchableOpacity>
                            <Text style={styles.modelTitle}>颜色</Text>
                            <TouchableOpacity
                                style={{
                                    justifyContent: "center",
                                    alignItems: "center",
                                    paddingVertical: 3,
                                    paddingHorizontal: 15,
                                }}
                                activeOpacity={0.8}
                                onPress={() => {
                                    this.closeModal();
                                    // 完成颜色选择
                                    if (this.onCallback) {
                                        let selectColor = colorsys.rgb2Hex(this.state.selectRValue, this.state.selectGValue, this.state.selectBValue);
                                        this.onCallback(selectColor);
                                    }
                                }}
                            >
                                <Text style={{fontSize: 16, color: global.homeColor}}>完成</Text>
                            </TouchableOpacity>
                        </View>
                        {/** 内容 */}
                        <ScrollView style={{flex: 1}} horizontal={false} showsVerticalScrollIndicator={false}>
                            <View style={{backgroundColor: this.state.selectColor, width: 200, height: 120,alignSelf: 'center', borderRadius:4, marginTop: 15}}/>
                            <View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
                                <Text style={{fontSize: 16, color: 'black', marginRight: 6}}>R</Text>
                                <Slider
                                    style={{flex: 1}}
                                    minimumValue={0}
                                    maximumValue={255}
                                    step={1}
                                    value={this.state.selectRValue}
                                    onValueChange={(value) => {
                                        let _color = 'rgb('+value+','+this.state.selectGValue+','+this.state.selectBValue+')';
                                        this.setState({
                                            selectColor: _color,
                                            selectRValue: value
                                        });
                                    }}
                                />
                                <Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
                                    borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectRValue+''}</Text>
                            </View>
                            {/** G值选择 */}
                            <View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
                                <Text style={{fontSize: 16, color: 'black', marginRight: 6}}>G</Text>
                                <Slider
                                    style={{flex: 1}}
                                    minimumValue={0}
                                    maximumValue={255}
                                    step={1}
                                    value={this.state.selectGValue}
                                    onValueChange={(value) => {
                                        let _color = 'rgb('+this.state.selectRValue+','+value+','+this.state.selectBValue+')';
                                        this.setState({
                                            selectColor: _color,
                                            selectGValue: value
                                        });
                                    }}
                                />
                                <Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
                                    borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectGValue+''}</Text>
                            </View>
                            {/** B值选择 */}
                            <View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
                                <Text style={{fontSize: 16, color: 'black', marginRight: 6}}>G</Text>
                                <Slider
                                    style={{flex: 1}}
                                    minimumValue={0}
                                    maximumValue={255}
                                    step={1}
                                    value={this.state.selectBValue}
                                    onValueChange={(value) => {
                                        let _color = 'rgb('+this.state.selectRValue+','+this.state.selectGValue+','+value+')';
                                        this.setState({
                                            selectColor: _color,
                                            selectBValue: value
                                        });
                                    }}
                                />
                                <Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
                                    borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectBValue+''}</Text>
                            </View>
                            <View style={{marginVertical: 15, width: width, height: 1, backgroundColor: 'rgba(0,0,0,0.4)'}}/>
                            {/** 选取常用颜色 */}
                            <Text style={{marginLeft: 20, fontSize: 16, color: 'black'}}>常用颜色</Text>
                            <View style={{flexDirection: 'row', justifyContent: 'flex-start', flexWrap: 'wrap', alignItems: 'flex-start',
                                paddingHorizontal: 20, width: width}}>
                                {this.selectColorList && this.selectColorList.length > 0 && this.selectColorList.map(
                                    ((value, index) => this.renderSelectColorItem(value, index)))}
                            </View>
                        </ScrollView>
                    </View>
                </View>
            </Modal>
        );
    }
}

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",
        paddingVertical: 10,
        paddingRight: 15,
        borderTopLeftRadius: 16,
        borderTopRightRadius: 16,
        backgroundColor: 'rgba(235, 235, 235, 1)'
    },
    modelTitle: {
        flex: 1,
        color: "rgba(0, 0, 0, 1)",
        fontSize: 16,
        textAlign: "center",
        justifyContent: "center"
    },
});