ts-basic-components-search.md 5.5 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 15 16

## APIs

Search(options?: { value?: string; placeholder?: string; icon?: string; controller?: SearchController })

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

E
ester.zhou 已提交
19 20
| Name     | Type        | Mandatory| Description                                                    |
| ----------- | ---------------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
21 22
| value       | string           | No  | Text input in the search text box.                                |
| placeholder | string | 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. The supported icon formats are .svg, .jpg, and .png.|
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 33
| searchButton            | string                                           | Text on the search button located next to the search text box. By default, there is no search button.        |
| 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
add doc  
ester.zhou 已提交
38 39 40

## Events

E
ester.zhou 已提交
41 42 43 44 45 46
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

| Name                                        | Description                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| onSubmit(callback: (value: string) => void) | Invoked 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) | Invoked when the input in the text box changes.<br> - **value**: current text input. |
E
ester.zhou 已提交
47 48 49
| onCopy(callback: (value: string) => void)   | Invoked 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)    | Invoked 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)  | Invoked when data is pasted from the pasteboard, which is displayed when the search text box is long pressed.<br> -**value**: text pasted.     |
E
add doc  
ester.zhou 已提交
50 51 52

## SearchController

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

E
ester.zhou 已提交
55
### Objects to Import
E
add doc  
ester.zhou 已提交
56 57 58
```
controller: SearchController = new SearchController()
```
E
ester.zhou 已提交
59
### caretPosition
E
add doc  
ester.zhou 已提交
60

E
ester.zhou 已提交
61
caretPosition(value: number): void
E
add doc  
ester.zhou 已提交
62 63 64

Sets the position of the caret.

E
ester.zhou 已提交
65
**Parameters**
E
add doc  
ester.zhou 已提交
66

E
ester.zhou 已提交
67 68 69
| 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 已提交
70 71 72

##  Example

E
ester.zhou 已提交
73 74
```ts
// xxx.ets
E
add doc  
ester.zhou 已提交
75 76 77
@Entry
@Component
struct SearchExample {
E
esterzhou 已提交
78 79
  @State changeValue: string = ''
  @State submitValue: string = ''
E
add doc  
ester.zhou 已提交
80 81 82
  controller: SearchController = new SearchController()

  build() {
E
ester.zhou 已提交
83 84 85 86 87
    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
add doc  
ester.zhou 已提交
88
        .width(400)
E
ester.zhou 已提交
89
        .height(40)
E
ester.zhou 已提交
90
        .backgroundColor('#F5F5F5')
E
add doc  
ester.zhou 已提交
91
        .placeholderColor(Color.Grey)
E
ester.zhou 已提交
92 93
        .placeholderFont({ size: 14, weight: 400 })
        .textFont({ size: 14, weight: 400 })
E
add doc  
ester.zhou 已提交
94
        .onSubmit((value: string) => {
E
esterzhou 已提交
95
          this.submitValue = value
E
add doc  
ester.zhou 已提交
96 97
        })
        .onChange((value: string) => {
E
esterzhou 已提交
98
          this.changeValue = value
E
add doc  
ester.zhou 已提交
99
        })
E
ester.zhou 已提交
100 101 102 103 104 105 106
        .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 已提交
107 108 109
  }
}
```
E
ester.zhou 已提交
110 111

![search](figures/search.gif)