React Native TextInput Onfocus不允许其他儿童组件
嵌套的TextInput组件不允许调用其他组件的OnPress函数。只有当未集中文本输入时,ONPRESS才能正常工作。
React本机版本:0.66.3
这是我的代码
export const Component = (props): JSX.Element {
const { searchText, onChangeSearchText, onClearSearchText } = props
const [searchViewFocused, setSearchViewFocused] = useState<boolean>(false)
const searchIcon = searchViewFocused ? "searchActive" : "searchInactive"
const cancelIcon = searchViewFocused ? "cancelActive" : "cancelInactive"
return (
<View style={styles.searchBoxContainer}>
<View style={[styles.searchBox, searchViewFocused && styles.searchBarActive]}>
<Icon styles={styles.searchIcon} icon={searchIcon} />
<TextInput
style={styles.searchInput}
onChangeText={onChangeSearchText}
value={searchText}
onFocus={() => setSearchViewFocused(true)}
onBlur={() => setSearchViewFocused(false)}
autoCompleteType={"off"}
numberOfLines={1}
/>
{searchText !== "" && (
<Pressable style={styles.clearIcon} onPress={onClearSearchText}>
<Icon icon={cancelIcon} />
</Pressable>
)}
</View>
</View>
)
})
是预期的图像
。
VS
问题
当将交叉图标按在重点状态上时,文本输入是不重要的,而是我试图实现的目标是清除搜索文本&amp;输入仍然集中。
注意:当不集中输入时,ONPRESS的工作正常很好
。谢谢!
PS:尝试触摸可接触性&amp;另外,我已经尝试将滚动浏览中的组件包装在键盘上shouldpersistaps ='andled',如SO答案之一所述,但没有成功。
Nested TextInput component does not allow other components' onPress function to be called. Only when the TextInput is not focused, the onPress works fine.
React Native Version : 0.66.3
Here is my code
export const Component = (props): JSX.Element {
const { searchText, onChangeSearchText, onClearSearchText } = props
const [searchViewFocused, setSearchViewFocused] = useState<boolean>(false)
const searchIcon = searchViewFocused ? "searchActive" : "searchInactive"
const cancelIcon = searchViewFocused ? "cancelActive" : "cancelInactive"
return (
<View style={styles.searchBoxContainer}>
<View style={[styles.searchBox, searchViewFocused && styles.searchBarActive]}>
<Icon styles={styles.searchIcon} icon={searchIcon} />
<TextInput
style={styles.searchInput}
onChangeText={onChangeSearchText}
value={searchText}
onFocus={() => setSearchViewFocused(true)}
onBlur={() => setSearchViewFocused(false)}
autoCompleteType={"off"}
numberOfLines={1}
/>
{searchText !== "" && (
<Pressable style={styles.clearIcon} onPress={onClearSearchText}>
<Icon icon={cancelIcon} />
</Pressable>
)}
</View>
</View>
)
})
Attached are the images of
Expected.
VS
The issue
When the cross icon is pressed on focused state, the textInput is unfocused rather What I am trying to achieve is that the search text gets cleared & the input remains focused.
Note: The onPress works perfectly fine when input is not focused
Any help is appreciated. Thanks!
PS: Tried TouchableOpacity & also I have tried wrapping the components inside a ScrollView to use keyboardShouldPersistTaps='handled' as mentioned in one of the SO answer but to no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到解决方法的方法是将整个组件包装到scrollView中并添加prop
键盘shouldpersistTaps ='andled
'
键盘shouldpersistTaps ='andled'
不起作用的关键是将整个组件包装在滚动浏览中,
这是有效的:
猜猜这是一个愚蠢的错误,但值得指出!
Found a workaround to this is to wrap the whole component into a ScrollView and adding the prop
keyboardShouldPersistTaps='handled'
Previously I was making the View inside the
Component
as ScrollView and addingkeyboardShouldPersistTaps='handled'
which did not workThe key was to wrap the entire component inside the ScrollView,
Here's what worked:
Guess this was a silly mistake, but worth pointing out!
您正在根据文本输入是否有焦点来设置整个组件的焦点。由于清晰的按钮在文本输入外面,因此按下它会导致组件失去焦点。
一种解决方案是将TextInput实例存储在参考文献中。按下清除按钮时,您可以重新调整文本输入。我在下面复制了您的组件,并添加了一些新行,这些线路在评论中标记。
You're setting the focus of the entire component based on whether the TextInput has focus. Since the clear button is outside the text input, pressing it causes the component to lose focus.
One solution is to store the TextInput instance in a ref. When the clear button is pressed, you can refocus the text input. I've copied your component below and added some new lines, which are marked in comments.