嵌套的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>
  )
})

附件是的图像

预期.

expected

VS公司

问题

issue

当在聚焦状态下按下十字图标时,文本输入是非聚焦的,而我试图实现的是搜索文本被清除&amp;投入仍然集中.

注意:输入未聚焦时,onPress工作正常

非常感谢您的帮助.谢谢

PS:try touch 不透明度(&T);此外,我还try 将组件包装在ScrollView中,以使用KeyboardShoulderPersistTaps='handled',如其中一个SO答案中所述,但没有成功.

推荐答案

找到了一种解决方法,将整个组件包装到滚动视图中,并添加props keyboardShouldPersistTaps='handled'

之前,我将Component中的视图设置为ScrollView,并添加了keyboardShouldPersistTaps='handled',但这不起作用

export const Component = (props): JSX.Element {
  ...
  return (
    <ScrollView contentContainerStyle={styles.searchBoxContainer} 
                keyboardShouldPersistTaps='handled'>
     ...
    </ScrollView>
  )
})

关键是将整个组件包装在ScrollView中,

以下是有效的方法:

<ScrollView keyboardShouldPersistTaps='handled'>
     <Component {...props}/>
</ScrollView>

我猜这是一个愚蠢的错误,但值得指出!

Typescript相关问答推荐

如何将@for的结果绑定到变量?

如何在Jest中嘲笑location.back()方法调用进行Angular 组件测试?

创建一个通用上下文,允许正确推断其中的子项

类型断言添加到类型而不是替换

即使子网是公共的,AWS CDK EventBridge ECS任务也无法分配公共IP地址

使用带有RxJS主题的服务在Angular中的组件之间传递数据

如果请求是流,如何等待所有响应块(Axios)

泛型函数中输入参数类型的推断问题

如何将CSV/TXT文件导入到服务中?

在Reaction-Native-ReAnimated中不存在Animated.Value

如何 bootstrap TS编译器为类型化属性`T`推断正确的类型?

如何将别名添加到vitest配置文件?

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

在构建Angular 应用程序时,有没有办法通过CLI传递参数?

Cypress-验证别名的存在或将别名中的文本与';if';语句中的字符串进行比较

在TypeScript中,如何通过一系列过滤器缩小类型?

递归生成常量树形 struct 的键控类型

Route.ts文件中未导出HTTP方法

如何从联合类型推断函数参数?

为什么我的 Typescript 函数中缺少 void 隐式返回类型?