Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native TouchableOpacity和TextInput在定位为&x27;绝对值';_React Native_Scrollview_Absolute - Fatal编程技术网

React native TouchableOpacity和TextInput在定位为&x27;绝对值';

React native TouchableOpacity和TextInput在定位为&x27;绝对值';,react-native,scrollview,absolute,React Native,Scrollview,Absolute,我有一个自定义下拉列表,其中一个下拉视图位于绝对位置。当组件直接位于scrollview内部时,我无法执行textinput和触摸操作 自定义组件代码: import React, { Component } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, ScrollView, TextInput } from 'react-native'; import Icon_Feather from 'react-nat

我有一个自定义下拉列表,其中一个下拉视图位于绝对位置。当组件直接位于scrollview内部时,我无法执行textinput和触摸操作

自定义组件代码:

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, ScrollView, TextInput } from 'react-native';
import Icon_Feather from 'react-native-vector-icons/Feather';
import Icon_MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import PropTypes from 'prop-types';
import _ from 'lodash';

let dropDownData = []

class MultiSelectDropdown extends Component {

    constructor(props) {
        super(props);

        this.state = {
            isVisible: false,
            data: [],
            searchData: [],
            selectedData: [],
            // selectAllStatus: false,
            searchableText: null,
            defaultSelectedValues: ''
        }
    }

    UNSAFE_componentWillReceiveProps = async (props) => {
        await this.setState({
            data: props.data,
            defaultSelectedValues: props.selectedData != undefined ? props.selectedData : [],
            isVisible: props.isVisible
        })
        // alert(this.state.data)
        await this.formatReceivedData()
    }

    formatReceivedData = async () => {
        var pushedData = []
        let tempData = JSON.parse(JSON.stringify(this.state.data))
        tempData.forEach(element => {
            let obj = element
            Object.assign(obj, { selected: false })
            pushedData.push(obj)
        });

        await this.setState({
            selectedData: pushedData,
            searchData: pushedData,
        })

        if (this.state.defaultSelectedValues.length > 0)
            await this.checkDefaultValues();
    }

    checkDefaultValues = async () => {
        this.state.data.forEach((ele1, index1) => {
            this.state.defaultSelectedValues.forEach((ele2, index2) => {
                if (_.isEqual(ele1, ele2)) {
                    this.state.selectedData[index1].selected = true
                    this.setState({
                        selectedData: this.state.selectedData
                    })
                }
            });
        });
    }

    getLayout(layout) {
        this.setState({
            top: layout.height - 1
        });
    }

    toggle() {
        // this.setState({
        //     isVisible: ! this.state.isVisible,
        // }, () => {
        //     const isVisible = this.state.isVisible;

        //     // if (isVisible) {
        //  //  this.props.onOpen();
        //  // } else {
        //  //  this.props.onClose();
        //  // }
        // });
        this.setState({
            isVisible: !this.state.isVisible,
        })
    }

    selectItem = async (data, index) => {
        var tempSelectedData = this.state.selectedData

        if (tempSelectedData[index].selected == false)
            tempSelectedData[index].selected = true
        else
            tempSelectedData[index].selected = false

        var truePropertyData = tempSelectedData.filter((item) => {
            return item.selected != false
        })

        if (truePropertyData.length > this.props.maxItemsToSelect) {
            // max
            tempSelectedData[index].selected = false;
            var truePropertyData1 = tempSelectedData.filter((item) => {
                return item.selected!= false
            })
            this.removeExtraAddedProperty(truePropertyData1, index);
        } else {
            this.setState({
                selectedData: tempSelectedData,
                // selectAllStatus: false
            })
            this.removeExtraAddedProperty(truePropertyData, index);
        }

    }

    getSelectedItemCount = () => {
        var truePropertyData = this.state.selectedData.filter((item) => {
            return item.selected!= false
        })
        return truePropertyData;
    }

    getLabelDisplayText = () => {
        let count = this.getSelectedItemCount().length;
        return this.props.multipleDataSelectedText.replace('%d', count);
    }

    selectAll = async () => {
        var tempSelectedData = JSON.parse(JSON.stringify(this.state.selectedData))

        var status = await this.selectAllOrUnselect()

        // if (this.state.selectAllStatus) {
        if (status) {
            tempSelectedData.forEach((ele, i) => {
                tempSelectedData[i].selected= false
            });
            this.setState({
                selectedData: tempSelectedData,
                // selectAllStatus: false
            })

            var truePropertyData = tempSelectedData.filter((item) => {
                return item.selected!= false
            })
            this.removeExtraAddedProperty(truePropertyData, 0)

        } else {
            tempSelectedData.forEach((ele, i) => {
                tempSelectedData[i].selected= true
            });
            this.setState({
                selectedData: tempSelectedData,
                // selectAllStatus: true
            })
            this.removeExtraAddedProperty(this.state.selectedData, 0)
        }

    }

    removeExtraAddedProperty = (truePropertyData, index) => {
        let tempTruePropertyData = JSON.parse(JSON.stringify(truePropertyData))
        var actualData = []
        if (tempTruePropertyData.length > 0) {
            tempTruePropertyData.forEach(element => {
                var obj = element
                delete obj['selected']
                actualData.push(obj)
            });
        } else
            this.props.onItemChange(truePropertyData, index)

        this.props.onItemChange(actualData, index)
    }

    selectAllOrUnselect = () => {
        var returnStatus;
        var trueFilteredData = this.state.selectedData.filter((item, i) => {
            return item.selected == true
        })

        if (trueFilteredData.length == this.state.selectedData.length)
            returnStatus = true
        else
            returnStatus = false

        return returnStatus;
    }

    updateSearch = async (text) => {
        await this.setState({
            searchableText: text
        })

        let toSearch = (this.state.searchableText).toLowerCase();
        let result = this.state.data.filter(o => o[this.props.displayLabel].toLowerCase().includes(toSearch));

        await this.setState({
            searchData: result
        })
    }

    render() {
        return (
            <View style={[this.props.containerStyle, {
                ...(Platform.OS !== 'android' && {
                    zIndex: this.props.zIndex
                })
            }]}>
                <TouchableOpacity
                    onLayout={(event) => this.getLayout(event.nativeEvent.layout)}
                    onPress={() => this.toggle()}
                    activeOpacity={1}
                    style={[
                        styles.dropDown,
                        this.props.style,
                        this.state.isVisible && styles.noBottomRadius, {
                            flexDirection: 'row', flex: 1
                        }
                    ]}
                >
                    <View style={[styles.dropDownDisplay]}>
                        {/* <Text style={[this.props.labelStyle, { flex: 1, marginRight: 5 }]}> */}
                        {/* {this.props.placeHolder} {this.getSelectedItemCount().length} */}
                        {/* {this.getSelectedItemCount().length > 0 ?
                                'Under Test'
                            :
                                JSON.stringify(this.state.placeHolder)
                            } */}
                        {/* </Text> */}
                        {this.getSelectedItemCount().length > 0 ?
                            <Text style={[this.props.labelStyle, { flex: 1, marginRight: 5 }]}>
                                {this.getLabelDisplayText()}
                            </Text>
                            :
                            <Text style={[this.props.labelStyle, { flex: 1, marginRight: 5 }]}>
                                {this.props.placeHolder} {JSON.stringify(this.state.isVisible)}
                            </Text>
                        }
                    </View>

                    <View style={[styles.arrow]}>
                        {!this.state.isVisible ?
                            <Icon_Feather name="chevron-down" size={15} color={'black'} />
                            :
                            <Icon_Feather name="chevron-up" size={15} color={'black'} />
                        }
                    </View>

                </TouchableOpacity>

                <View style={[
                    styles.dropDown,
                    styles.dropDownBox,
                    this.props.dropDownStyle,
                    !this.state.isVisible && styles.hidden, {
                        top: this.state.top,
                        maxHeight: this.props.dropDownMaxHeight,
                        zIndex: this.props.zIndex
                    }
                ]}>

                    {/* Search Text */}
                    <View style={{ width: '100%', flexDirection: 'row' }}>
                        <TextInput
                            style={[styles.input, this.props.searchableStyle]}
                            defaultValue={this.state.searchableText}
                            placeholder={this.props.searchablePlaceholder}
                            onChangeText={(text) => this.updateSearch(text)}
                        />
                    </View>

                    <ScrollView style={{ width: '100%' }} nestedScrollEnabled={true}>
                        {this.state.searchData.length > 0 ? this.state.searchData.map((item, index) => (
                            <View key={index}>
                                <TouchableOpacity
                                    style={[styles.dropDownItem, this.props.itemStyle]}
                                    onPress={() => this.selectItem(item, index)}
                                >
                                    <View style={{ flexDirection: 'row' }}>
                                        <View style={{ paddingRight: 5 }}>
                                            {(this.state.selectedData[index] != undefined && this.state.selectedData[index].selected == true) ?
                                                <Icon_MaterialIcons name={'check-box'} size={22} color={'black'} />
                                                :
                                                <Icon_MaterialIcons name={'check-box-outline-blank'} size={22} color={'black'} />
                                            }
                                        </View>
                                        <View style={{ justifyContent: 'center', alignItems: 'center' }}>
                                            <Text style={[this.props.labelStyle]}>
                                                {item[this.props.displayLabel]}
                                            </Text>
                                        </View>
                                    </View>
                                </TouchableOpacity>
                            </View>
                        ))
                            :
                            <Text>
                                Not Found
                            </Text>
                        }
                    </ScrollView>

                </View>
            </View >
        );
    }
}

MultiSelectDropdown.defaultProps = {
    containerStyle: {},
    style: {},
    zIndex: 5000,
    placeHolder: 'Select an option',
    labelStyle: {},
    dropDownStyle: {},
    dropDownMaxHeight: 250,
    itemStyle: {},
    displayLabel: '',
    searchableStyle: {},
    searchablePlaceholder: 'Search',
    multipleDataSelectedText: '%d items have been selected',
    data: PropTypes.array.isRequired,
    maxItemsToSelect: '',
    isVisible: false,
    selectAllText: 'Select All',
    unselectAllText: 'Un-Select All'
}

MultiSelectDropdown.propTypes = {
    containerStyle: PropTypes.object,
    style: PropTypes.object,
    zIndex: PropTypes.number,
    placeHolder: PropTypes.string,
    labelStyle: PropTypes.object,
    dropDownStyle: PropTypes.object,
    dropDownMaxHeight: PropTypes.number,
    itemStyle: PropTypes.object,
    displayLabel: PropTypes.string,
    searchableStyle: PropTypes.object,
    isVisible: PropTypes.bool,
    searchablePlaceholder: PropTypes.string,
    selectedData: PropTypes.any,
    multipleDataSelectedText: PropTypes.string,
    maxItemsToSelect: PropTypes.string,
    selectAllText: PropTypes.string,
    unselectAllText: PropTypes.string
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'white',
    },
    noBottomRadius: {
        borderBottomLeftRadius: 0,
        borderBottomRightRadius: 0,
    },
    dropDown: {
        paddingHorizontal: 10,
        paddingVertical: 5,
        backgroundColor: '#fff',
        borderTopRightRadius: 5,
        borderTopLeftRadius: 5,
        borderBottomRightRadius: 5,
        borderBottomLeftRadius: 5,
        borderWidth: 1,
        borderColor: '#dfdfdf',
    },
    dropDownDisplay: {
        flexDirection: 'row',
        alignItems: 'center',
        borderTopRightRadius: 0,
        borderBottomRightRadius: 0,
        flexGrow: 1
    },
    arrow: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        textAlign: 'center',
        paddingVertical: 8,
        borderTopLeftRadius: 0,
        borderBottomLeftRadius: 0,
    },
    dropDownBox: {
        borderTopLeftRadius: 0,
        borderTopRightRadius: 0,
        alignItems: 'center',
        justifyContent: 'center',
        textAlign: 'center',
        position: 'absolute',
        width: '100%',
        zIndex: 1
    },
    hidden: {
        position: 'relative',
        display: 'none',
        borderWidth: 0
    },
    dropDownItem: {
        paddingVertical: 8,
        width: '100%',
        justifyContent: 'center'
    },
    input: {
        flex: 1,
        borderColor: '#dfdfdf',
        borderBottomWidth: 1,
        paddingHorizontal: 0,
        paddingVertical: 8,
        marginBottom: 2,
    },
});

export default MultiSelectDropdown;

import React,{Component}来自'React';
从“react native”导入{View、Text、StyleSheet、TouchableOpacity、ScrollView、TextInput};
从“反应本机向量图标/羽毛”导入图标_羽毛;
从“反应本机向量图标/唯物主义者”导入图标_唯物主义者;
从“道具类型”导入道具类型;
从“lodash”进口;
让dropDownData=[]
类MultiSelectDropdown扩展了组件{
建造师(道具){
超级(道具);
此.state={
isVisible:false,
数据:[],
searchData:[],
所选数据:[],
//selectAllStatus:false,
searchableText:null,
defaultSelectedValues:“”
}
}
不安全组件WillReceiveProps=async(props)=>{
等待这一天({
数据:道具数据,
defaultSelectedValues:props.selectedData!=未定义?props.selectedData:[],
isVisible:props.isVisible
})
//警报(this.state.data)
等待此消息。formatReceivedData()
}
formatReceivedData=async()=>{
var pushedData=[]
让tempData=JSON.parse(JSON.stringify(this.state.data))
tempData.forEach(元素=>{
设obj=元素
assign(obj,{selected:false})
推送数据。推送(obj)
});
等待这一天({
selectedData:pushedData,
searchData:pushedData,
})
如果(this.state.defaultSelectedValues.length>0)
等待此消息。checkDefaultValues();
}
checkDefaultValues=async()=>{
this.state.data.forEach((ele1,index1)=>{
this.state.defaultSelectedValues.forEach((ele2,index2)=>{
如果(u.isEqual(ele1,ele2)){
this.state.selectedData[index1]。selected=true
这是我的国家({
selectedData:this.state.selectedData
})
}
});
});
}
getLayout(布局){
这是我的国家({
顶部:布局高度-1
});
}
切换(){
//这是我的国家({
//isVisible:!this.state.isVisible,
// }, () => {
//const isVisible=this.state.isVisible;
////如果(isVisible){
////this.props.onOpen();
////}其他{
////this.props.onClose();
//  // }
// });
这是我的国家({
isVisible:!this.state.isVisible,
})
}
selectItem=async(数据、索引)=>{
var tempSelectedData=this.state.selectedData
if(tempSelectedData[index].selected==false)
tempSelectedData[index]。selected=true
其他的
tempSelectedData[index]。selected=false
var truePropertyData=tempSelectedData.filter((项)=>{
return item.selected!=false
})
如果(truePropertyData.length>this.props.maxItemsToSelect){
//马克斯
tempSelectedData[index]。selected=false;
var truePropertyData1=tempSelectedData.filter((项)=>{
返回项目。已选择!=false
})
此.removeExtraAddedProperty(truePropertyData1,索引);
}否则{
这是我的国家({
selectedData:tempSelectedData,
//selectAllStatus:false
})
此.removeExtraAddedProperty(truePropertyData,索引);
}
}
getSelectedItemCount=()=>{
var truePropertyData=this.state.selectedData.filter((项)=>{
返回项目。已选择!=false
})
返回truePropertyData;
}
getLabelDisplayText=()=>{
让count=this.getSelectedItemCount().length;
返回此.props.multipleDataSelectedText.replace(“%d”,计数);
}
selectAll=async()=>{
var tempSelectedData=JSON.parse(JSON.stringify(this.state.selectedData))
var status=wait this.selectAllorRunSelect()
//if(this.state.selectAllStatus){
如果(状态){
tempSelectedData.forEach((ele,i)=>{
tempSelectedData[i]。selected=false
});
这是我的国家({
selectedData:tempSelectedData,
//selectAllStatus:false
})
var truePropertyData=tempSelectedData.filter((项)=>{
返回项目。已选择!=false
})
此.removeExtraAddedProperty(truePropertyData,0)
}否则{
tempSelectedData.forEach((ele,i)=>{
tempSelectedData[i]。selected=true
});
这是我的国家({
selectedData:tempSelectedData,
//selectAllStatus:true
})
this.removeExtraAddedProperty(this.state.selectedData,0)
}
}
removeExtraAddedProperty=(truePropertyData,索引)=>{
让TestRuePropertyData=JSON.parse(JSON.stringify(truePropertyData))
var actualData=[]
如果(TestRuePropertyData.length>0){
forEach(元素=>{
var obj=元素
删除对象['selected']
实际数据推送(obj)
});
}否则
this.props.onItemChange(truePropertyData,索引)
this.props.onItemChange(实际数据,索引)
}
selectAllOrUnselect=()=>{
风险值状态;
var trueFilteredData=this.state.selectedData.filter((项,i)=>{
return item.selected==true
})
if(trueFilteredata.length==this.state.se