• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextPicker
2
3The **\<TextPicker>** component allows users to scroll to select text.
4
5>  **NOTE**
6>
7>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17TextPicker(options?: {range: string[] | string[][] | Resource | TextPickerRangeContent[] | TextCascadePickerRangeContent[], selected?: number, value?: string})
18
19Creates a text picker based on the selection range specified by **range**.
20
21**Parameters**
22
23| Name| Type| Mandatory| Description|
24| -------- | -------- | -------- | -------- |
25| range | string[] \| string[][]<sup>10+</sup> \| [Resource](ts-types.md#resource) \|<br>[TextPickerRangeContent](#textpickerrangecontent10)[]<sup>10+</sup> \| [TextCascadePickerRangeContent](#textcascadepickerrangecontent10)[]<sup>10+</sup> | Yes| Data selection range of the picker. This parameter cannot be set to an empty array. If set to an empty array, it will not be displayed. If it is dynamically changed to an empty array, the current value remains displayed.<br>**NOTE**<br>For a single-column picker, use a value of the string[], Resource, or TextPickerRangeContent[] type.<br>For a multi-column picker, use a value of the string[] type.<br>For a multi-column linked picker, use a value of the TextCascadePickerRangeContent[] type.<br>The Resource type supports only [strarray.json](../../quick-start/resource-categories-and-access.md#resource-group-subdirectories).|
26| selected | number \| number[]<sup>10+</sup> | No| Index of the default item in the range.<br>Default value: **0**<br>**NOTE**<br>For a single-column picker, use a value of the number type.<br>For a multi-column (linked) picker, use a value of the number[] type.<br>Since API version 10, this parameter supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.|
27| value | string \| string[]<sup>10+</sup> | No| Value of the default item in the range. The priority of this parameter is lower than that of **selected**.<br>Default value: value of the first item<br>**NOTE**<br>This parameter works only when the picker contains text only.  <br>For a single-column picker, use a value of the string type.<br>For a multi-column (linked) picker, use a value of the string[] type.<br>Since API version 10, this parameter supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables. |
28
29## TextPickerRangeContent<sup>10+</sup>
30
31| Name| Type                                                | Mandatory| Description  |
32| ------ | -------------------------------------------------------- | ---- | ---------- |
33| icon   | string \| [Resource](ts-types.md#resource) | Yes  | Image resource.|
34| text   | string \| [Resource](ts-types.md#resource) | No  | Text information.|
35
36## TextCascadePickerRangeContent<sup>10+</sup>
37
38| Name| Type                                                | Mandatory| Description  |
39| ------ | -------------------------------------------------------- | ---- | ---------- |
40| text   | string \| [Resource](ts-types.md#resource) | Yes  | Text information.|
41| children   | [TextCascadePickerRangeContent](#textcascadepickerrangecontent10)[] | No  | Linkage data.|
42
43## Attributes
44
45In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
46
47| Name| Type| Description|
48| -------- | -------- | -------- |
49| defaultPickerItemHeight | number \| string | Height of each item in the picker.|
50| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | Font color, font size, and font width for the top and bottom items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '14fp', <br>weight: FontWeight.Regular<br>}<br>} |
51| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | Font color, font size, and font width of all items except the top, bottom, and selected items.<br>Default value:<br>{<br>color: '#ff182431',<br>font: {<br>size: '16fp', <br>weight: FontWeight.Regular<br>}<br>} |
52| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | Font color, font size, and font width of the selected item.<br>Default value:<br>{<br>color: '#ff007dff',<br>font: {<br>size: '20vp', <br>weight: FontWeight.Medium<br>}<br>} |
53| selectedIndex<sup>10+</sup> | number \| number[] | Index of the default selected item in the array. Its priority is higher than that of the selected value in **options**.<br>**NOTE**<br>For a single-column picker, use a value of the number type. For a multi-column (linked) picker, use a value of the number[] type.|
54| canLoop<sup>10+</sup> | boolean | Whether to support scroll looping. The value **true** means to support scroll looping, and **false** means the opposite.<br>Default value: **true**|
55
56## Events
57
58In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
59
60| Name| Description|
61| -------- | -------- |
62| onAccept(callback: (value: string, index: number) => void)<sup>(deprecated) </sup>  | Triggered when the OK button in the dialog box is clicked.<br>- **value**: value of the selected item.<br>- **index**: index of the selected item.<br>This API is deprecated since API version 10.<br>**NOTE**<br>This event can be triggered only in the [text picker dialog box](ts-methods-textpicker-dialog.md).|
63| onCancel(callback: () => void)<sup>(deprecated) </sup>  | Triggered when the Cancel button in the dialog box is clicked.<br>This API is deprecated since API version 10.<br>**NOTE**<br>This event can be triggered only in the [text picker dialog box](ts-methods-textpicker-dialog.md).|
64| onChange(callback: (value: string \| string[]<sup>10+</sup>, index: number \| number[]<sup>10+</sup>) =&gt; void) | Triggered when an item in the picker is selected.<br>- **value**: value of the selected item. (For a multi-column picker, **value** is of the array type.)<br>- **index**: index of the selected item. (For a multi-column picker, **index** is of the array type.)<br>**NOTE**<br>When the picker contains text only or both text and imagery, **value** indicates the text value of the selected item. When the picker contains imagery only, **value** is empty.|
65
66
67## Example
68
69```ts
70// xxx.ets
71@Entry
72@Component
73struct TextPickerExample {
74  private select: number = 1
75  private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4']
76  private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4']
77  private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4']
78  private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits]
79  private cascade: TextCascadePickerRangeContent[] = [
80    {
81      text: 'Category 1',
82      children: [{ text: 'Subcategory 1', children: [{ text: 'Subcategory 2' }, { text: 'Subcategory 3' }, { text: 'Subcategory 4' }] },
83        { text: 'Item 1', children: [{ text: ''Item 2' }, { text: ''Item 3' }, { text: ''Item 4' }] }]
84    },
85    {
86      text: 'Category 2',
87      children: [{ text: 'Subcategory 1', children: [{ text: 'Subcategory 2' }, { text: 'Subcategory 3' }, { text: 'Subcategory 4' }] },
88        { text: 'Item 1', children: [{ text: ''Item 2' }, { text: ''Item 3' }, { text: ''Item 4' }] }]
89    },
90    {
91      text: 'Category 3',
92      children: [{ text: 'Subcategory 1', children: [{ text: 'Subcategory 2' }, { text: 'Subcategory 3' }, { text: 'Subcategory 4' }] },
93        { text: 'Item 1', children: [{ text: ''Item 2' }, { text: ''Item 3' }, { text: ''Item 4' }] }]
94    }
95  ]
96
97  build() {
98    Column() {
99
100      TextPicker({ range: this.apfruits, selected: this.select })
101        .onChange((value: string, index: number) => {
102          console.info('Picker item changed, value: ' + value + ', index: ' + index)
103        }).margin({ bottom: 50 })
104
105      TextPicker({ range: this.multi })
106        .onChange((value: string | string[], index: number | number[]) => {
107          console.info('TextPicker multi-column: onChange' + JSON.stringify(value) + ',' + 'index:' + JSON.stringify(index))
108        }).margin({ bottom: 50 })
109
110      TextPicker({ range: this.cascade })
111        .onChange((value: string | string[], index: number | number[]) => {
112          console.info('TextPicker multi-column linkage: onChange' + JSON.stringify(value) + ',' + 'index:' + JSON.stringify(index))
113        })
114    }
115  }
116}
117```
118
119![textpicker](figures/textpicker.gif)
120
121```ts
122// xxx.ets
123@Entry
124@Component
125struct TextPickerExample {
126  private select: number = 1
127  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
128
129  build() {
130    Column() {
131      TextPicker({ range: this.fruits, selected: this.select })
132        .onChange((value: string, index: number) => {
133          console.info('Picker item changed, value: ' + value + ', index: ' + index)
134        })
135        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
136        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
137        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
138    }.width('100%').height('100%')
139  }
140}
141```
142
143![textpicker](figures/textpicker1.gif)
144