ts-basic-components-textpicker.md 1.7 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# TextPicker


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


The **<TextPicker>** component allows users to select text from a list of options.


## Required Permissions

None


E
esterzhou 已提交
16
## Child Components
Z
zengyawen 已提交
17 18 19 20 21 22 23 24 25 26 27

None


## APIs

TextPicker(value: {range: string[], selected?: number})

Creates a text picker based on the selection range specified by **range**.

- Parameter
E
esterzhou 已提交
28
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
29
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
30 31
  | range | string[] | Yes | - | Data selection range of the picker. |
  | selected | number | No | First element | Index value of the selected item in the array. |
Z
zengyawen 已提交
32 33 34 35


## Attributes

E
esterzhou 已提交
36
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
38
| defaultPickerItemHeight | Length | - | Default height of a Picker content item element. |
Z
zengyawen 已提交
39 40 41 42


## Events

E
esterzhou 已提交
43 44 45
| Name | Description |
| -------- | -------- |
| onChange(callback: (value: string, index: number) =&gt; void) | Triggered when an item in the picker is selected.<br/>- **value**: text of the selected item.<br/>- **index**: index of the selected item. |
Z
zengyawen 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69


## Example


```
@Entry
@Component
struct TextPickerExample {
  private select: number = 1
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']

  build() {
    Column() {
      TextPicker({range: this.fruits, selected: this.select})
        .onChange((value: string, index: number) => {
          console.info('Picker item changed, value: ' + value + ', index: ' + index)
        })
    }
  }
}
```

![en-us_image_0000001257058417](figures/en-us_image_0000001257058417.png)