Picker
The Picker component is used to select a single value from a set of mutually exclusive options. It supports various display styles and allows users to choose a single value. The selected value and the change event can be managed using the value and onChanged properties.
Type Definitions
PickerValue: The type of the selected value, which can be eithernumberorstring.PickerProps<T extends PickerValue>: The property type for thePickercomponent, which includes:value: The current selected value, which can be anumberorstring(optional).onChanged: A callback function triggered when the selected value changes, with the new value (T) as the parameter.children: The option views, where each child must have atagproperty to indicate its value. This can be aJSX.Elementor an array ofJSX.Elements.title: A string describing the option's purpose, used in certain cases.systemImage: The name of the system image resource, used in certain cases.label: AJSX.Elementthat describes the purpose of the selection, used in certain cases.
Component Functionality
The Picker component manages the user's selection through the value and onChanged properties. The value is the current selected value, and the onChanged is a callback function that is invoked when the user changes the selection. The children property defines the options' views, allowing multiple layouts to be used for displaying the options. Each children element must have a tag property to mark its value, for example, <Text tag={1}>Option 1</Text>.
Picker Styles
The Picker component supports the following styles to adjust how the component is displayed:
automatic: The default style, automatically determined based on the picker’s context.inline: Displays each option inline with other views in the current container.menu: Displays the options in a menu that opens when the user presses a button or as a submenu within a larger menu.navigationLink: Displays a navigation link that presents the options in a List-style picker view when clicked.palette: Displays the options as a row of compact elements.segmented: Displays the options in a segmented control style.wheel: Displays the options in a scrollable wheel, showing the selected option and a few neighboring options.
Example Usage
Below are examples of how to use the Picker component:
Example 1: Picker with Numeric Values
Example 2: Picker with String Values
Example 3: Picker with Title and System Image
Common Use Cases for Picker
- Form Selection:
Pickercan be used in forms to select a single value from a predefined set of options. - Settings Interface: In app settings,
Pickercan be used to choose colors, themes, languages, etc. - Navigation Options: In more complex interfaces,
Pickercan serve as a tool for selecting options within multi-level menus.
Notes
- Each
childrenelement in thePickermust use thetagproperty to mark its value, for example:<Text tag={1}>Option 1</Text>. - The
valueandonChangedproperties must be used together to ensure correct functionality when the user changes the selected value. - The
pickerStyleproperty provides various styles to enhance the user experience. Select the one that fits your use case best.
Related APIs
JSX.Element: The type used for defining the view structure. ThePickercomponent’schildrenproperty relies on this type.useState: A React hook for managing the selected value’s state.
