挑选器

与 @react-native-picker/picker 兼容的选择器。

Android
iOS
Web
Bundled version:
~55.0.15

For the complete documentation index, see llms.txt. Use this file to discover all available pages.

一个 Picker 组件,具有与 @react-native-picker/picker 兼容的 API。它在 iOS 上使用 SwiftUI 轮 Picker,在 Android 上使用 Material 3 ExposedDropdownMenuBox,在网页上使用原生 <select> 元素。

🌐 A Picker component with an API compatible with @react-native-picker/picker. It uses a SwiftUI wheel Picker on iOS, a Material 3 ExposedDropdownMenuBox on Android, and a native <select> element on web.

在内部,这个组件封装了平台特定的 @expo/ui 原语:

🌐 Under the hood this component wraps the platform-specific @expo/ui primitives:

如果你需要更底层的控制,请直接使用那些原语。

🌐 If you need lower-level control, use those primitives directly.

安装

🌐 Installation

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.

@react-native-picker/picker 迁移

🌐 Migrating from @react-native-picker/picker

  • 将导入从 import { Picker } from '@react-native-picker/picker' 更新为 import { Picker } from '@expo/ui/community/picker'
  • modepromptdropdownIconColordropdownIconRippleColornumberOfLinesselectionColoritemStyleaccessibilityLabel 属性不受支持。
  • Picker.Item 上,style 属性仅适用于 colorbackgroundColorfontFamilyfontSize。顶层的 colorfontFamily 属性仍然作为相应 style 值的别名被支持。
  • enabledPicker.Item 上仅适用于安卓。
  • reffocus()blur() 方法仅在 Android 上有效(打开/关闭下拉菜单)。在 iOS 上,滚轮选择器始终可见。

基本用法

🌐 Basic usage

PickerExample.tsx
import { useState } from 'react'; import { Text, View } from 'react-native'; import { Picker } from '@expo/ui/community/picker'; export default function PickerExample() { const [language, setLanguage] = useState('java'); return ( <View> <Picker selectedValue={language} onValueChange={value => setLanguage(value)}> <Picker.Item label="Java" value="java" /> <Picker.Item label="JavaScript" value="js" /> <Picker.Item label="Objective C" value="objc" /> <Picker.Item label="Swift" value="swift" /> </Picker> <Text>Selected: {language}</Text> </View> ); }

每项样式和状态

🌐 Per-item styling and state

style 传递给 Picker.Item 以控制每个项目的 colorbackgroundColorfontFamilyfontSize,以及使用 enabled={false} 在 Android 上禁用特定项目。

🌐 Pass a style to Picker.Item to control color, backgroundColor, fontFamily, and fontSize per item, and enabled={false} to disable specific items on Android.

fontFamily 在 iOS 上接受 iOS 字体名称(例如,'Menlo'),在 Android 上接受 Compose 通用字体族('monospace''serif''sansSerif''cursive')或使用 expo-font 加载的字体。

StyledPickerExample.tsx
import { useState } from 'react'; import { Platform } from 'react-native'; import { Picker } from '@expo/ui/community/picker'; const monospace = Platform.select({ ios: 'Menlo', android: 'monospace' }); const serif = Platform.select({ ios: 'Georgia', android: 'serif' }); export default function StyledPickerExample() { const [language, setLanguage] = useState('java'); return ( <Picker selectedValue={language} onValueChange={value => setLanguage(value)}> <Picker.Item label="Java" value="java" style={{ color: '#e11d48', fontFamily: monospace, fontSize: 14 }} /> <Picker.Item label="JavaScript" value="js" style={{ color: '#2563eb', fontFamily: serif, fontSize: 18 }} enabled={false} /> <Picker.Item label="Objective C" value="objc" style={{ color: '#059669', fontFamily: monospace, fontSize: 16 }} /> <Picker.Item label="Swift" value="swift" style={{ color: '#d97706', fontFamily: serif, fontSize: 30 }} enabled={false} /> </Picker> ); }

命令式聚焦和模糊(Android)

🌐 Imperative focus and blur (Android)

在 Android 上使用 ref 以编程方式打开和关闭下拉菜单。在 iOS 上,这些方法无效,因为滚轮选择器始终可见。

🌐 Use a ref to programmatically open and close the dropdown on Android. On iOS, these methods are no-ops because the wheel picker is always visible.

RefPickerExample.tsx
import { useRef, useState } from 'react'; import { Button } from 'react-native'; import { Picker, type PickerRef } from '@expo/ui/community/picker'; export default function RefPickerExample() { const [language, setLanguage] = useState('java'); const pickerRef = useRef<PickerRef>(null); return ( <> <Button title="2秒后打开并关闭" onPress={() => { pickerRef.current?.focus(); setTimeout(() => pickerRef.current?.blur(), 2000); }} /> <Picker ref={pickerRef} selectedValue={language} onValueChange={setLanguage}> <Picker.Item label="Java" value="java" /> <Picker.Item label="JavaScript" value="js" /> <Picker.Item label="Objective C" value="objc" /> <Picker.Item label="Swift" value="swift" /> </Picker> </> ); }

应用接口

🌐 API

import { Picker } from '@expo/ui/community/picker';

Components

Picker

Android
iOS
Web

Type: React.Element<PickerProps<T>>

A drop-in replacement for @react-native-picker/picker on web. Renders a native <select> element.

Props for the Picker component. Compatible with @react-native-picker/picker.

PickerProps

children

Android
iOS
Web
Optional • Type: ReactNode

Picker.Item children that define the available options.

enabled

Android
iOS
Web
Optional • Type: boolean

Whether the picker is enabled.

onValueChange

Android
iOS
Web
Optional • Type: (itemValue: T, itemIndex: number) => void

Callback when an item is selected. Called with (itemValue, itemIndex).

ref

Android
iOS
Web
Optional • Type: Ref<PickerRef>

Ref handle exposing focus() and blur() methods.

selectedValue

Android
iOS
Web
Optional • Type: T

The currently selected value. Must match the value of one of the Picker.Item children.

style

Android
iOS
Web
Optional • Type: StyleProp<ViewStyle>

Style applied to the picker container.

testID

Android
iOS
Web
Optional • Type: string

Test identifier.

Picker.Item

Android
iOS
Web

Type: React.Element<ComponentType<PickerItemProps>>

Props for the Picker.Item component. Compatible with @react-native-picker/picker.

PickerItemProps

color

Android
iOS
Web
Optional • Type: string

Text color for the item. Equivalent to setting color in the style prop.

enabled

Android
Optional • Type: boolean

Whether the item is enabled.

fontFamily

Android
iOS
Web
Optional • Type: string

Custom font family for the item. Equivalent to setting fontFamily in the style prop.

label

Android
iOS
Web
Optional • Type: string

Display text for the item.

style

Android
iOS
Web
Optional • Type: StyleProp<TextStyle>

Style applied to the item label. Only the following values take effect: color, backgroundColor, fontFamily, and fontSize. When also set via the top-level color or fontFamily props, values from style win.

testID

Android
iOS
Web
Optional • Type: string

Test identifier.

value

Android
iOS
Web
Optional • Type: T

Value passed to onValueChange when this item is selected.

Types

PickerItemValue

Android
iOS
Web

Literal Type: union

Acceptable values are: string | number | null

PickerRef

Android
iOS
Web

Ref handle for the Picker component. Compatible with @react-native-picker/picker.

PropertyTypeDescription
blur() => void
Only for:
Android

Programmatically closes the picker.

focus() => void
Only for:
Android

Programmatically opens the picker.