ts-basic-components-search.md 10.2 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
#  Search

E
ester.zhou 已提交
3
The **\<Search>** component provides an area for users to enter search queries.
E
esterzhou 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
7
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
add doc  
ester.zhou 已提交
8 9 10

## Child Components

E
ester.zhou 已提交
11
Not supported
E
add doc  
ester.zhou 已提交
12 13 14

## APIs

E
ester.zhou 已提交
15
Search(options?: { value?: string; placeholder?: ResourceStr; icon?: string; controller?: SearchController })
E
add doc  
ester.zhou 已提交
16

E
ester.zhou 已提交
17
**Parameters**
E
add doc  
ester.zhou 已提交
18

E
ester.zhou 已提交
19 20 21
| Name     | Type                                            | Mandatory| Description                                                    |
| ----------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| value       | string                                               | No  | Text input in the search text box.<br>Since API version 10, this parameter supports two-way binding through [$$](../../quick-start/arkts-two-way-sync.md).|
E
ester.zhou 已提交
22
| placeholder | [ResourceStr](ts-types.md#resourcestr)<sup>10+</sup> | No  | Text displayed when there is no input.                                    |
E
ester.zhou 已提交
23
| icon        | string                                               | No  | Path to the search icon. By default, the system search icon is used.<br>**NOTE**<br>The icon data source can be a local or online image.<br>- The supported formats include PNG, JPG, BMP, SVG, GIF, and pixelmap.<br>- The Base64 string is supported in the following format: data:image/[png\|jpeg\|bmp\|webp];base64,[base64 data], where [base64 data] is a Base64 string.|
E
ester.zhou 已提交
24
| controller  | SearchController                                     | No  | Controller of the **\<Search>** component.                                      |
E
add doc  
ester.zhou 已提交
25 26 27

## Attributes

E
ester.zhou 已提交
28 29 30 31
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

| Name                   | Type                                        | Description                                          |
| ----------------------- | ------------------------------------------------ | ---------------------------------------------- |
E
ester.zhou 已提交
32
| searchButton<sup>10+</sup> | value: string,<br>option?: [SearchButtonOption](#searchbuttonoption10)              | Text on the search button located next to the search text box. By default, there is no search button.              |
E
ester.zhou 已提交
33
| placeholderColor        | [ResourceColor](ts-types.md#resourcecolor)       | Placeholder text color.                          |
E
ester.zhou 已提交
34 35
| placeholderFont         | [Font](ts-types.md#font)                         | Placeholder text style, including the font size, font width, font family, and font style. Currently, only the default font family is supported.                        |
| textFont                | [Font](ts-types.md#font)                         | Style of the text entered in the search box, including the font size, font width, font family, and font style. Currently, only the default font family is supported.                          |
E
ester.zhou 已提交
36 37
| textAlign               | [TextAlign](ts-appendix-enums.md#textalign)      | Text alignment mode in the search text box.<br>Default value: **TextAlign.Start**   |
| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.                            |
E
ester.zhou 已提交
38 39 40 41
| searchIcon<sup>10+</sup>   | [IconOptions](#iconoptions10)                                                  | Style of the search icon on the left.                                      |
| cancelButton<sup>10+</sup> | {<br>style? : [CancelButtonStyle](#cancelbuttonstyle10)<br>icon?: [IconOptions](#iconoptions10) <br>} | Style of the Cancel button on the right.                                      |
| fontColor<sup>10+</sup>    | [ResourceColor](ts-types.md#resourcecolor)                   | Font color of the input text.                                   |
| caretStyle<sup>10+</sup>  | [CaretStyle](#caretstyle10)                                                  | Caret style.                                              |
E
ester.zhou 已提交
42 43
| enableKeyboardOnFocus<sup>10+</sup> | boolean | Whether to enable the input method when the component obtains focus.<br>Default value: **true**  |
| selectionMenuHidden<sup>10+</sup> | boolean | Whether to display the text selection menu when the text box is long-pressed or right-clicked.<br>Default value: **false**|
E
ester.zhou 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
## IconOptions<sup>10+</sup>

| Name| Type                                  | Mandatory| Description   |
| ------ | ------------------------------------------ | ---- | ----------- |
| size   | [Length](ts-types.md#length)               | No  | Icon size.   |
| color  | [ResourceColor](ts-types.md#resourcecolor) | No  | Icon color.   |
| src    | [ResourceStr](ts-types.md#resourcestr)     | No  | Image source of the icon.|

## CaretStyle<sup>10+</sup>

| Name| Type                                  | Mandatory| Description|
| ------ | ------------------------------------------ | ---- | -------- |
| width  | [Length](ts-types.md#length)               | No  | Caret width.|
| color  | [ResourceColor](ts-types.md#resourcecolor) | No  | Caret color.|

## SearchButtonOption<sup>10+</sup>

| Name   | Type                                  | Mandatory| Description        |
| --------- | ------------------------------------------ | ---- | ---------------- |
| fontSize  | [Length](ts-types.md#length)               | No  | Font size of the button.|
| fontColor | [ResourceColor](ts-types.md#resourcecolor) | No  | Font color of the button.|

## CancelButtonStyle<sup>10+</sup>

| Name                   | Description            |
| ----------------------- | ---------------- |
E
ester.zhou 已提交
70 71 72
| CONSTANT  | The Cancel button is always displayed.|
| INVISIBLE | The Cancel button is always hidden.|
| INPUT     | The Cancel button is displayed when there is text input.|
E
add doc  
ester.zhou 已提交
73 74 75

## Events

E
ester.zhou 已提交
76 77
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

E
ester.zhou 已提交
78 79 80 81 82 83 84 85 86
| Name                                                        | Description                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| onSubmit(callback: (value: string) => void)                  | Triggered when users click the search icon or the search button, or touch the search button on a soft keyboard.<br> - **value**: current text input.|
| onChange(callback: (value: string) => void)                  | Triggered when the input in the text box changes.<br> - **value**: current text input.|
| onCopy(callback: (value: string) => void)                    | Triggered when data is copied to the pasteboard, which is displayed when the search text box is long pressed.<br> - **value**: text copied.|
| onCut(callback: (value: string) => void)                     | Triggered when data is cut from the pasteboard, which is displayed when the search text box is long pressed.<br> - **value**: text cut.|
| onPaste(callback: (value: string) => void)                   | Triggered when data is pasted from the pasteboard, which is displayed when the search text box is long pressed.<br> -**value**: text pasted.|
| onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void)<sup>10+</sup> | Triggered when the text selection position changes.<br>**selectionStart**: start position of the text selection area. The start position of text in the text box is **0**.<br>**selectionEnd**: end position of the text selection area.|
| onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void)<sup>10+</sup> | Triggered when the text content is scrolled.<br>**totalOffsetX**: X coordinate offset of the text in the content area.<br>**totalOffsetY**: Y coordinate offset of the text in the content area.|
E
add doc  
ester.zhou 已提交
87 88 89

## SearchController

E
ester.zhou 已提交
90
Implements the controller of the **\<Search>** component. Currently, the controller can be used to control the caret position.
E
add doc  
ester.zhou 已提交
91

E
ester.zhou 已提交
92
### Objects to Import
E
add doc  
ester.zhou 已提交
93 94 95
```
controller: SearchController = new SearchController()
```
E
ester.zhou 已提交
96
### caretPosition
E
add doc  
ester.zhou 已提交
97

E
ester.zhou 已提交
98
caretPosition(value: number): void
E
add doc  
ester.zhou 已提交
99 100 101

Sets the position of the caret.

E
ester.zhou 已提交
102
**Parameters**
E
add doc  
ester.zhou 已提交
103

E
ester.zhou 已提交
104 105 106
| Name| Type| Mandatory| Description                          |
| ------ | -------- | ---- | ---------------------------------- |
| value  | number   | Yes  | Length from the start of the character string to the position where the caret is located.|
E
add doc  
ester.zhou 已提交
107

E
ester.zhou 已提交
108 109 110 111 112 113
### stopEditing<sup>10+</sup>

stopEditing(): void

Exits the editing state.

E
add doc  
ester.zhou 已提交
114 115
##  Example

E
ester.zhou 已提交
116 117
### Example 1

E
ester.zhou 已提交
118 119
```ts
// xxx.ets
E
add doc  
ester.zhou 已提交
120 121 122
@Entry
@Component
struct SearchExample {
E
esterzhou 已提交
123 124
  @State changeValue: string = ''
  @State submitValue: string = ''
E
add doc  
ester.zhou 已提交
125 126 127
  controller: SearchController = new SearchController()

  build() {
E
ester.zhou 已提交
128 129 130 131 132
    Column() {
      Text('onSubmit:' + this.submitValue).fontSize(18).margin(15)
      Text('onChange:' + this.changeValue).fontSize(18).margin(15)
      Search({ value: this.changeValue, placeholder: 'Type to search...', controller: this.controller })
        .searchButton('SEARCH')
E
ester.zhou 已提交
133
        .width('95%')
E
ester.zhou 已提交
134
        .height(40)
E
ester.zhou 已提交
135
        .backgroundColor('#F5F5F5')
E
add doc  
ester.zhou 已提交
136
        .placeholderColor(Color.Grey)
E
ester.zhou 已提交
137 138
        .placeholderFont({ size: 14, weight: 400 })
        .textFont({ size: 14, weight: 400 })
E
add doc  
ester.zhou 已提交
139
        .onSubmit((value: string) => {
E
esterzhou 已提交
140
          this.submitValue = value
E
add doc  
ester.zhou 已提交
141 142
        })
        .onChange((value: string) => {
E
esterzhou 已提交
143
          this.changeValue = value
E
add doc  
ester.zhou 已提交
144
        })
E
ester.zhou 已提交
145 146 147 148 149 150 151
        .margin(20)
      Button('Set caretPosition 1')
        .onClick(() => {
          // Move the caret to after the first entered character.
          this.controller.caretPosition(1)
        })
    }.width('100%')
E
add doc  
ester.zhou 已提交
152 153 154
  }
}
```
E
ester.zhou 已提交
155 156

![search](figures/search.gif)
E
ester.zhou 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

### Example 2

```ts
// xxx.ets
@Entry
@Component
struct SearchButtoonExample {
  @State submitValue: string = ''

  build() {
    Column() {
      Text('onSubmit:' + this.submitValue).fontSize(18).margin(15)
      Search({ placeholder: 'Type to search...' })
        .searchButton('SEARCH')
        .searchIcon({
          src: $r('app.media.search')
        })
        .cancelButton({
          style: CancelButtonStyle.CONSTANT,
          icon: {
            src: $r('app.media.cancel')
          }
        })
        .width('90%')
        .height(40)
        .backgroundColor('#F5F5F5')
        .placeholderColor(Color.Grey)
        .placeholderFont({ size: 14, weight: 400 })
        .textFont({ size: 14, weight: 400 })
        .onSubmit((value: string) => {
          this.submitValue = value
        })
        .margin(20)
    }.width('100%')
  }
}
```

![searchButton](figures/searchButton.gif)