React Native - 关键词搜索框

在这里插入图片描述

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useState} from 'react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  useColorScheme,
  View,
  Alert,
  TextInput,
  Button,
  LayoutAnimation,
  TouchableOpacity,
} from 'react-native';

const App = () => {
  const [expanded, setExpanded] = useState(false);
  const [value, changeValue] = useState('');

  const hide = val => {
    setExpanded(false);
    changeValue(val);
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.search}>
        <TextInput
          style={styles.inputStyle}
          returnKeyType="search"
          placeholder="请输入关键字"
          value={value}
          onChangeText={value => {
            changeValue(value);
            setExpanded(true);
          }}
        />
        <View style={styles.btnStyle}>
          <Text style={styles.searchText}>搜索</Text>
        </View>
      </View>
      {expanded ? (
        <TouchableOpacity style={styles.result} onPress={() => hide(value + '大街')}>
          <Text style={styles.item}>
            {value}大街
          </Text>
        </TouchableOpacity>
      ) : null}
      {expanded ? (
        <TouchableOpacity style={styles.result} onPress={() => hide(value + '车站')}>
          <Text style={styles.item}>{value}车站</Text>
        </TouchableOpacity>
      ) : null}
      {expanded ? (
        <TouchableOpacity style={styles.result} onPress={() => hide(value + '机场')}>
          <Text style={styles.item}>{value}机场</Text>
        </TouchableOpacity>
      ) : null}
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    alignItems: 'center',
  },
  search: {
    width: 400,
    height: 32,
    flexDirection: 'row',
    flexWrap: 'nowrap',
    margin: 0,
    padding: 0,
  },
  inputStyle: {
    flex: 8,
    height: 32,
    fontSize: 16,
    borderColor: 'rgba(0,0,0,.2)',
    borderWidth: 1,
    borderRightWidth: 0,
    borderTopLeftRadius: 5,
    borderBottomLeftRadius: 5,
    marginLeft: 20,
    paddingLeft: 5,
  },
  btnStyle: {
    flex: 2,
    height: 32,
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: 20,
    backgroundColor: '#FFD700',
    borderColor: 'rgba(0,0,0,.2)',
    borderWidth: 1,
    borderTopRightRadius: 5,
    borderBottomRightRadius: 5,
  },
  searchText: {
    fontSize: 16,
    color: '#fff',
  },
  result: {
    width: 360,
    height: 32,
    justifyContent: 'center',
    borderColor: 'rgba(0,0,0,.2)',
    borderWidth: 1,
    borderTopWidth: 0,
    marginLeft: 20,
    marginRight: 20,
  },
  item: {
    fontSize: 16,
    paddingLeft: 5,
  },
});

export default App;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现带搜索框的侧边菜单可以分为以下几个步骤: 1. 安装依赖 需要使用到 react-navigation、react-native-vector-icons 和 react-native-elements 这三个库,可以使用 npm 或 yarn 进行安装。 2. 创建侧边菜单和搜索框 使用 react-navigation 中的 DrawerNavigator 创建侧边菜单,然后在 DrawerNavigator 的 contentComponent 中添搜索框。可以使用 react-native-elements 中的 SearchBar 组件来实现搜索框。 3. 处理搜索功能 获取搜索框中的输入值后,可以将其传递给侧边菜单中的列表组件进行筛选。可以使用 FlatList 组件来渲染列表,并在其 renderItem 属性中根据搜索关键字来筛选列表项。 代码实现如下: ```javascript import React, { useState } from 'react'; import { StyleSheet, View, Text } from 'react-native'; import { createDrawerNavigator } from 'react-navigation-drawer'; import { SearchBar } from 'react-native-elements'; import Icon from 'react-native-vector-icons/FontAwesome'; const data = [ { name: 'Apple', price: '$1.99' }, { name: 'Banana', price: '$0.99' }, { name: 'Cherry', price: '$2.99' }, { name: 'Durian', price: '$5.99' }, { name: 'Elderberry', price: '$3.99' }, ]; const SideMenu = ({ navigation }) => { const [search, setSearch] = useState(''); const filteredData = search ? data.filter(item => item.name.includes(search)) : data; return ( <View style={styles.container}> <SearchBar placeholder="Search" onChangeText={setSearch} value={search} containerStyle={styles.searchBar} inputContainerStyle={styles.inputContainer} searchIcon={<Icon name="search" size={20} color="gray" />} /> {filteredData.map((item, index) => ( <Text key={index} style={styles.item}> {item.name} ({item.price}) </Text> ))} </View> ); }; const DrawerNavigator = createDrawerNavigator( { Home: { screen: SideMenu, navigationOptions: { title: 'Side Menu', drawerIcon: <Icon name="bars" size={20} color="gray" />, }, }, }, { drawerWidth: 200, contentComponent: SideMenu, } ); const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', paddingVertical: 20, paddingHorizontal: 10, }, searchBar: { backgroundColor: '#fff', borderBottomWidth: 0, borderTopWidth: 0, }, inputContainer: { backgroundColor: '#f2f2f2', }, item: { fontSize: 16, paddingVertical: 10, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: 'gray', }, }); export default DrawerNavigator; ``` 在上面的代码中,我们先定义了一个 data 数组作为列表数据源,然后使用 useState 定义了一个 search 状态来保存搜索框中的输入值,然后根据 search 的值对列表数据进行筛选。在 SideMenu 组件中,我们使用 SearchBar 组件来实现搜索框,并使用 FlatList 组件来渲染列表。在 FlatList 的 renderItem 属性中,我们对每个列表项进行渲染,并根据 search 的值来筛选列表项。 最后,我们使用 createDrawerNavigator 创建了一个侧边菜单,并将 SideMenu 组件作为 contentComponent 属性传递给 DrawerNavigator,在 SideMenu 组件中,我们渲染了一个带搜索框的列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易山易酒易诗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值