• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Select
2
3The **<Select\>** component provides a drop-down list box that allows users to select among multiple options.
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## Child Components
10
11Not supported
12
13## APIs
14
15Select(options: Array\<[SelectOption](#selectoption)\>)
16
17**Parameters**
18
19| Name | Type                                      | Mandatory| Description      |
20| ------- | ---------------------------------------------- | ---- | -------------- |
21| options | Array\<[SelectOption](#selectoption)\> | Yes  | Options in the drop-down list box.|
22
23## SelectOption
24
25| Name| Type                           | Mandatory| Description      |
26| ------ | ----------------------------------- | ---- | -------------- |
27| value  | [ResourceStr](ts-types.md#resourcestr) | Yes  | Value of an option in the drop-down list box.|
28| icon   | [ResourceStr](ts-types.md#resourcestr) | No  | Icon of an option in the drop-down list box.|
29
30## Attributes
31
32In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
33
34| Name                   | Type                             | Description                                         |
35| ----------------------- | ------------------------------------- | --------------------------------------------- |
36| selected                | number                                | Index of the initial selected option in the drop-down list box. The index of the first option is **0**.<br>If this attribute is set to an invalid value or is not set, the default value **-1** will be used, which means that no option will be selected. If this attribute is set to **undefined** or **null**, the first option will be selected.<br>Since API version 10, this attribute supports two-way binding through [$$](../../quick-start/arkts-two-way-sync.md).|
37| value                   | string                                | Text of the drop-down button. By default, it will be replaced by the content of the selected option.<br>Since API version 10, this attribute supports two-way binding through [$$](../../quick-start/arkts-two-way-sync.md).|
38| font                    | [Font](ts-types.md#font)          | Text font of the drop-down button.<br>Default value:<br>{<br>size: '16fp',<br>weight: FontWeight.Medium<br>} <br>**NOTE**<br>When **size** is set to **0**, the text is not displayed. When **size** is set to a negative value, the text is displayed at its default size.|
39| fontColor               | [ResourceColor](ts-types.md#resourcecolor) | Text color of the drop-down button.<br>Default value: **'\#E5182431'**|
40| selectedOptionBgColor   | [ResourceColor](ts-types.md#resourcecolor) | Background color of the selected option in the drop-down list box.<br>Default value: **'\#33007DFF'**|
41| selectedOptionFont      | [Font](ts-types.md#font)          | Text font of the selected option in the drop-down list box.<br>Default value:<br>{<br>size: '16fp',<br>weight: FontWeight.Regular<br>} <br>**NOTE**<br>When **size** is set to **0**, the text is not displayed. When **size** is set to a negative value, the text is displayed at its default size.|
42| selectedOptionFontColor | [ResourceColor](ts-types.md#resourcecolor) | Text color of the selected option in the drop-down list box.<br>Default value: **'\#ff007dff'**|
43| optionBgColor           | [ResourceColor](ts-types.md#resourcecolor) | Background color of an option in the drop-down list box.<br>Default value: **'\#ffffffff'**|
44| optionFont              | [Font](ts-types.md#font)          | Text font of an option in the drop-down list box.<br>Default value:<br>{<br>size: '16fp',<br>weight: FontWeight.Regular<br>}<br>**NOTE**<br>When **size** is set to **0**, the text is not displayed. When **size** is set to a negative value, the text is displayed at its default size.|
45| optionFontColor         | [ResourceColor](ts-types.md#resourcecolor) | Text color of an option in the drop-down list box.<br>Default value: **'\#ff182431'**|
46| space<sup>10+</sup>         | [Length](ts-types.md#length)               | Spacing between the text and arrow of an option.<br>**NOTE**<br>This attribute cannot be set in percentage.<br>Default value: **8**<br>If this attribute to **null**, **undefined**, or a value less than or equal to 8, the default value is used.|
47| arrowPosition<sup>10+</sup> | [ArrowPosition](#arrowposition10)                  | Alignment between the text and arrow of an option.<br>Default value: **ArrowPosition.END**|
48| menuAlign<sup>10+</sup> | alignType: [MenuAlignType](#menualigntype10),<br> offset?: [Offset](ts-types.md#offset)    | Alignment between the drop-down button and the drop-down menu.<br> - **alignType**: alignment type. Mandatory.<br>Default value: **MenuAlignType.START**<br> - **offset**: offset of the drop-down menu relative to the drop-down button after alignment based on the specified alignment type.<br> Default value: **{dx: 0, dy: 0}**|
49
50## ArrowPosition<sup>10+</sup>
51
52| Name               | Description              |
53| ------------------- | ------------------ |
54| END<sup>10+</sup>   | The text is in front of the arrow.|
55| START<sup>10+</sup> | The arrow is in front of the text.|
56
57
58## MenuAlignType<sup>10+</sup>
59
60| Name               | Description              |
61| ------------------- | ------------------ |
62| START               | Aligned with the start edge in the same direction as the language in use.|
63| CENTER              | Aligned with the center.|
64| END                 | Aligned with the end edge in the same direction as the language in use.|
65
66## Events
67
68| Name                                                        | Description                                                    |
69| ------------------------------------------------------------ | ------------------------------------------------------------ |
70| onSelect(callback: (index: number, value?: string) => void) | Invoked when an option in the drop-down list box is selected.<br>**index**: index of the selected option.<br>**value**: value of the selected option.|
71
72##  Example
73
74```ts
75// xxx.ets
76@Entry
77@Component
78struct SelectExample {
79  @State text: string = "TTTTT"
80  @State index: number = 2
81  @State space: number = 8
82  @State arrowPosition: ArrowPosition = ArrowPosition.END
83  build() {
84    Column() {
85      Select([{ value: 'aaa', icon: "/common/public_icon.svg" },
86        { value: 'bbb', icon: "/common/public_icon.svg" },
87        { value: 'ccc', icon: "/common/public_icon.svg" },
88        { value: 'ddd', icon: "/common/public_icon.svg" }])
89        .selected(this.index)
90        .value(this.text)
91        .font({ size: 16, weight: 500 })
92        .fontColor('#182431')
93        .selectedOptionFont({ size: 16, weight: 400 })
94        .optionFont({ size: 16, weight: 400 })
95        .space(this.space)
96        .arrowPosition(this.arrowPosition)
97        .menuAlign(MenuAlignType.START, {dx:0, dy:0})
98        .onSelect((index:number, text?: string | undefined)=>{
99          console.info('Select:' + index)
100          this.index = index;
101          if(text){
102            this.text = text;
103          }
104        })
105    }.width('100%')
106  }
107}
108```
109
110![](figures/select.png)
111