This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
SearchBar
一个用于搜索输入功能的 Jetpack Compose SearchBar 组件。
Android
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
Expo UI SearchBar 与官方 Jetpack Compose Search API 相匹配,并提供了一个搜索输入框,支持占位文本和展开的全屏搜索。
安装
Terminal
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
基本搜索栏
BasicSearchBarExample.tsx
import { useState } from 'react'; import { Host, SearchBar } from '@expo/ui/jetpack-compose'; export default function BasicSearchBarExample() { const [query, setQuery] = useState(''); return ( <Host matchContents> <SearchBar onSearch={searchText => setQuery(searchText)} /> </Host> ); }
带占位文本的搜索栏
使用 SearchBar.Placeholder 子组件在搜索字段为空时显示提示文本。
SearchBarPlaceholderExample.tsx
import { useState } from 'react'; import { Host, SearchBar } from '@expo/ui/jetpack-compose'; export default function SearchBarPlaceholderExample() { const [query, setQuery] = useState(''); return ( <Host matchContents> <SearchBar onSearch={searchText => setQuery(searchText)}> <SearchBar.Placeholder>搜索项目...</SearchBar.Placeholder> </SearchBar> </Host> ); }
API
import { SearchBar } from '@expo/ui/jetpack-compose';
Components
Type: React.Element<ExpandedFullScreenSearchBarProps>
ExpandedFullScreenSearchBar component for SearchBar. This component marks its children to be rendered in the expanded full-screen search bar.
Type: React.Element<PlaceholderProps>
Placeholder component for SearchBar. This component marks its children to be rendered in the placeholder slot.