{"size": 3428, "ext": "js", "max_stars_repo_path": "modules/v1/page-display/popup/popup_select.js", "max_stars_repo_name": "gzh1612/react-native-womasoft", "max_stars_repo_stars_event_min_datetime": "2020-06-12T06:35:57.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-12T06:35:57.000Z", "max_issues_repo_path": "modules/v1/page-display/popup/popup_select.js", "max_issues_repo_name": "gzh1612/react-native-womasoft", "max_issues_repo_issues_event_min_datetime": "2021-08-03T10:11:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T10:11:49.000Z", "max_forks_repo_path": "modules/v1/page-display/popup/popup_select.js", "max_forks_repo_name": "gzh1612/react-native-womasoft", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 17, "max_forks_count": null, "avg_line_length": 26.1679389313, "max_line_length": 106, "alphanum_fraction": 0.5697199533}
import React from 'react';
import {View, ScrollView, StyleSheet, Text, TouchableOpacity} from 'react-native';
import language from '../language';
import theme from '../theme';

import Modals from './init_modal';
import redux from "../../data-storage/redux";

//执行显示
const perform_show = (data, style) => {
    redux.update(Modals.reduxName, {
        type: 2,
        display: true,
        style: style,
        data: data,
    });
};

//执行隐藏
const hide = () => {
    redux.update(Modals.reduxName, {
        type: 2,
        display: false,
        style: {},
        data: null,
        onPress: null,
    })
};

/**
 * 初始化
 * @param arr   数据数组
 * @param params    [text:显示字段名称,isCancel:是否显示取消按钮]
 * @param resolve   选中返回方法，带参数
 * @returns {*}
 * @constructor
 */
const SelectInit = (arr, params, resolve) => {
    const css = theme.get();
    const style = styles(css);
    const lang = language.all('modal');
    let cancelText = '取消';
    if (lang) cancelText = lang['btn_cancel'];
    let cancelView = <View/>;
    if (typeof params.isCancel !== "boolean") params.isCancel = true;
    if (params.isCancel) cancelView = <TouchableOpacity activeOpacity={.5} onPress={() => hide()}>
        <Text style={style.selectItem}>{cancelText}</Text>
    </TouchableOpacity>;
    let height = css.height * 0.7;
    let arrCount = arr.length;
    if (params.isCancel) arrCount = arrCount + 1;
    if (arrCount * 60 < height) height = arrCount * 60;

    return <View style={style.selectView}>
        <TouchableOpacity style={{width: css.width, height: css.height - height}} onPress={() => hide()}/>
        <ScrollView style={{height: height}}>
            {itemView(arr, params, resolve)}
            {cancelView}
        </ScrollView>
    </View>
};
//循环item
const itemView = (arr, params, resolve) => {
    if (arr.length <= 0) return <View/>;
    const css = theme.get();
    const style = styles(css);
    return arr.map((item, key) => {
        let text = item.text;
        if (params.text) text = item[params.text];
        return <TouchableOpacity activeOpacity={.5} key={key} onPress={() => {
            if (typeof resolve === 'function') resolve(item, key);
            hide();
        }}>
            <Text style={style.selectItem}>{text}</Text>
        </TouchableOpacity>
    });
};

/**
 *
 * @param arr   数据数组 [{text:'显示值',其他参数随意}]
 * @param params  [text:显示字段名称, isCancel:是否显示取消按钮]
 * @returns {Promise<>}
 */
const show = (arr, params = {}) => {
    const css = theme.get();
    const style = styles(css);
    return new Promise((resolve, reject) => {
        if (!arr) return reject(false);
        const select = SelectInit(arr, params, (res, key) => resolve(res, key));
        // Modal.show({data: select, style: style.select});
        perform_show(select, style.select);
    });

};

//显示多
const selectMore = () => {

};


const styles = (css) => StyleSheet.create({
    select: {
        flexDirection: 'column',
        justifyContent: 'flex-end',
        alignItems: 'center',
    },
    selectView: {
        width: css.width - 15,
        paddingBottom: 20,
    },
    selectItem: {
        marginTop: 10,
        borderWidth: 1,
        borderColor: css.page.border,
        borderRadius: 5,
        backgroundColor: '#fff',
        height: 50,
        lineHeight: 50,
        textAlign: 'center',
        overflow: 'hidden',
        // elevation: 5,
    },
});

export default {
    show,
    hide,
}
