react-native - 无法关闭后退按钮单击时打开的可搜索下拉菜单
问题描述
我正在使用以下模块使用可搜索的下拉菜单。
https://www.npmjs.com/package/react-native-searchable-dropdown
当下拉菜单打开时,当我单击后退按钮时,下拉菜单不会关闭。在我选择任何选项之前,它只会被打开。如何解决这个问题?
<View style={{ marginTop: 5 }}>
<SearchableDropdown
onItemSelect={(item, index) => {
setItemname(item.value);
setItemIndex(index);
}}
itemStyle={{
padding: 10,
marginTop: 2,
backgroundColor: '#ddd',
borderColor: '#bbb',
borderWidth: 1,
borderRadius: 5,
}}
itemTextStyle={{ color: '#222' }}
itemsContainerStyle={{ maxHeight: 140 }}
items={items}
defaultIndex={itemindex}
resetValue={false}
textInputProps={
{
placeholder: "Select Item",
underlineColorAndroid: "transparent",
style: {
padding: 12,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
},
}
}
listProps={{
nestedScrollEnabled: true,
}}
/>
</View>
解决方案
怎么了
该SearchableDropDown
组件使用TextInput
's onBlur
prop 将其focus
状态设置为false
on blur (代码参考)。
当focus
状态为包含项目的渲染时(true
代码参考)。FlatList
问题是当您按下后退按钮时onBlur
不会触发,这意味着focus
状态未设置为false
并且FlatList
仍然呈现带有项目的 。
可能的解决方法
您可以根据键盘是否隐藏有条件地隐藏平面列表。
import React, { Component, useState, useEffect, useRef } from "react";
import { View, TextInput, Keyboard } from "react-native";
import SearchableDropdown from "react-native-searchable-dropdown";
const App = () => {
const [itemname, setItemname] = useState("");
const [itemindex, setItemIndex] = useState(0);
const [isKeyboardVisible, setKeyboardVisible] = useState(false);
useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener(
"keyboardDidShow",
() => {
setKeyboardVisible(true);
}
);
const keyboardDidHideListener = Keyboard.addListener(
"keyboardDidHide",
() => {
setKeyboardVisible(false);
}
);
return () => {
keyboardDidHideListener.remove();
keyboardDidShowListener.remove();
};
}, []);
return (
<View style={{ marginTop: 5 }}>
<SearchableDropdown
onItemSelect={(item, index) => {
setItemname(item.value);
setItemIndex(index);
}}
itemStyle={{
padding: 10,
marginTop: 2,
backgroundColor: "#ddd",
borderColor: "#bbb",
borderWidth: 1,
borderRadius: 5,
}}
itemTextStyle={{ color: "#222" }}
itemsContainerStyle={
isKeyboardVisible ? { maxHeight: 140 } : { display: "none" }
}
items={items}
defaultIndex={itemindex}
resetValue={false}
textInputProps={{
placeholder: "Select Item",
underlineColorAndroid: "transparent",
style: {
padding: 12,
borderWidth: 1,
borderColor: "#ccc",
borderRadius: 5,
},
}}
listProps={{
nestedScrollEnabled: true,
}}
/>
</View>
);
};
export default App;
上面的实现设置为当状态为时设置itemsContainerStyle
为的对象。display
'none'
isKeyboardVisible
false
推荐阅读
- sass - SASS 后置 & 号是否反转选择器嵌套?
- api - Postman API 测试所有软件方法和功能
- android - 为什么底部导航抽屉片段显示空数据
- python - 默认管理器“对象”对我不起作用
- vue.js - vue中的对象数据属性上是否需要存在所有属性?
- haskell - haskell 如何管理递归函数调用的内存
- python - 通过 python 将销售数据作为 VARCHAR 附加到 MS SQL-Server 表中,只有两位小数
- powerbi - 列匹配条件时如何显示行?
- json - 在服务器上找不到 Wordrpress wp-json -- localhost
- css - 如何自定义角度材料设计 mat-datepicker 图标大小