Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

搜索栏

一个用于搜索输入功能的 Jetpack Compose 搜索栏组件。

Android
Included in Expo Go
Recommended version:
~57.0.0

Expo UI SearchBar 与官方的 Jetpack Compose Search API 保持一致,并提供带有占位文本支持以及展开为全屏搜索的搜索输入框。

带有占位文本的 Material 3 搜索栏

安装

Terminal
npx expo install @expo/ui

If 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

ExpandedFullScreenSearchBar

Android

Type: React.Element<ExpandedFullScreenSearchBarProps>

ExpandedFullScreenSearchBar component for SearchBar. This component marks its children to be rendered in the expanded full-screen search bar.

Android

Type: React.Element<SearchBarProps>

Renders a SearchBar component.

SearchBarProps

children

Android
Optional • Type: ReactNode

The children of the component.

modifiers

Android
Optional • Type: ModifierConfig[]

Modifiers for the component.

onSearch

Android
Optional • Type: (searchText: string) => void

Callback function that is called when the search text is submitted.

SearchBarPlaceholder

Android

Type: React.Element<PlaceholderProps>

Placeholder component for SearchBar. This component marks its children to be rendered in the placeholder slot.