React Native小技巧

1、输入框 + clear按钮 不关闭键盘

RN的默认行为: scrollview + textinput + 自定义的清除按钮,输入框获焦的时候 弹起键盘,此时点击清空按钮的时候会先关闭弹框,需要再次点击清除按钮才会清空输入框

观察原生app的输入框可以 点击清空按钮的时候 输入框还是获焦状态 而且键盘没有关闭,然后摸索实现方式。

方式一:失焦再获焦,这样会让键盘有关闭动作再升起来,效果虽然达到了,但是还差一些意思。

ts 复制代码
export default function SearchInput() {
  const [text, setText] = useState('');
  const inputRef = useRef<TextInput>(null);

  const handleClear = () => {
    setText('');
    // 立即重新聚焦
    requestAnimationFrame(() => {
      inputRef.current?.focus();
    });
  };

  return (
    <View style={styles.container}>
      <TextInput ref={inputRef} style={styles.input} value={text} onChangeText={setText} placeholder="输入内容..." />
      {text.length > 0 && (
        <TouchableOpacity
          style={styles.closeButton}
          onPress={handleClear}
          activeOpacity={0.7}
          hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
        >
          <Text style={styles.closeText}>×</Text>
        </TouchableOpacity>
      )}
    </View>
  );
}

方式二:使用react-native-gesture-handler自带的Pressable组件,它运行在原生线程上,不会触发 JS 线程的失焦事件,完美的达到我需要的效果。

ts 复制代码
import React, { useRef, useState } from 'react';
import { StyleSheet, View, TextInput, Text } from 'react-native';
// 主要是这里的 Pressable组件!
import { Pressable } from 'react-native-gesture-handler';

export default function SearchInput() {
  const [text, setText] = useState('');
  const inputRef = useRef<TextInput>(null);

  const handleClear = () => {
    setText('');
  };

  return (
    <View style={styles.container}>
      <TextInput ref={inputRef} style={styles.input} value={text} onChangeText={setText} placeholder="输入内容..." />
      {text.length > 0 && (
        <Pressable
          style={styles.closeButton}
          onPress={handleClear}
          hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
        >
          <Text style={styles.closeText}>×</Text>
        </Pressable>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: COLORS.white,
    borderRadius: scaleLinear(9),
    paddingHorizontal: scaleLinear(SPACING.md),
    height: scaleLinear(38),
    borderWidth: 1,
    borderColor: COLORS.gray4,
  },
  input: {
    flex: 1,
    fontSize: scaleLinear(14),
    color: COLORS.textPrimary,
    paddingVertical: 0,
  },
  closeButton: {
    width: scaleLinear(24),
    height: scaleLinear(24),
    borderRadius: scaleLinear(12),
    backgroundColor: COLORS.gray1,
    alignItems: 'center',
    justifyContent: 'center',
    marginLeft: scaleLinear(4),
  },
  closeText: {
    fontSize: scaleLinear(16),
    color: COLORS.textSecondary,
    textAlign: 'center',
    lineHeight: scaleLinear(22),
  },
});
相关推荐
用泥种荷花5 分钟前
【LangChain学习笔记】创建智能体
前端
再吃一根胡萝卜8 分钟前
在 Ant Design Vue 的 a-table 中将特定数据行固定在底部
前端
掘金安东尼32 分钟前
Vercel:我们为 React2Shell 发起了一项价值 100 万美元的黑客挑战
前端·javascript·github
掘金安东尼35 分钟前
浏览器处理Base64数据的速度有多快?
前端·javascript·github
掘金安东尼36 分钟前
为不同场景设计多样化的页面过渡动画
前端·javascript·github
elangyipi12342 分钟前
2025 搜索优化新革命:GEO 正在悄然取代 SEO?
前端·人工智能
持续升级打怪中43 分钟前
深入解析深浅拷贝:原理、实现与最佳实践
开发语言·前端·javascript
我有一棵树43 分钟前
空值合并运算符 ?? ,|| 的替代方案
前端·javascript
Apifox43 分钟前
Apifox 12 月更新| AI 生成用例同步生成测试数据、接口文档完整性检测、设计 SSE 流式接口、从 Git 仓库导入数据
前端·后端·测试
禾叙_1 小时前
【NIO】ByteBuffer
前端·html·nio