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

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

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 21 22 23 24
| Name     | Type        | Mandatory| Description                                                    |
| ----------- | ---------------- | ---- | ------------------------------------------------------------ |
| value       | string           | No  | Text input in the search text box.                                                |
| placeholder | string           | No  | Text displayed when there is no input.                                        |
| 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.|
| controller  | SearchController | No  | Controller.                                                    |
E
add doc  
ester.zhou 已提交
25 26 27

## Attributes

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

| Name                   | Type                                        | Description                                          |
| ----------------------- | ------------------------------------------------ | ---------------------------------------------- |
| 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.                         |
| placeholderFont         | [Font](ts-types.md#font) | Placeholder text style.                     |
| textFont                | [Font](ts-types.md#font)     | Text font for the search text box.                        |
| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.                          |
| textAlign               | [TextAlign](ts-appendix-enums.md#textalign)      | Text alignment mode.<br>Default value: **TextAlign.Start**|
E
add doc  
ester.zhou 已提交
38 39 40

## Events

E
esterzhou 已提交
41 42 43 44 45 46 47
| 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.<br> -**value**: text copied.     |
| onCut(callback: (value: string) => void) | Triggered when data is cut from the pasteboard.<br> -**value**: text cut.     |
| onPaste(callback: (value: string) => void) | Triggered when data is pasted from the pasteboard.<br> -**value**: text pasted.     |
E
add doc  
ester.zhou 已提交
48 49 50 51 52

## SearchController

Defines the controller of the **\<Search>** component.

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

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

Sets the position of the caret.

E
ester.zhou 已提交
63
**Parameters**
E
add doc  
ester.zhou 已提交
64

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

##  Example

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

  build() {
    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
E
esterzhou 已提交
82 83 84
      Text(this.submitValue)
      Text(this.changeValue)
      Search({value: this.changeValue, placeholder: 'Type to search', controller: this.controller})
E
add doc  
ester.zhou 已提交
85 86 87 88 89
        .searchButton('Search')
        .width(400)
        .height(35)
        .backgroundColor(Color.White)
        .placeholderColor(Color.Grey)
E
esterzhou 已提交
90
        .placeholderFont({ size: 26, weight: 10, family: 'serif', style: FontStyle.Normal })
E
add doc  
ester.zhou 已提交
91
        .onSubmit((value: string) => {
E
esterzhou 已提交
92
          this.submitValue = value
E
add doc  
ester.zhou 已提交
93 94
        })
        .onChange((value: string) => {
E
esterzhou 已提交
95
          this.changeValue = value
E
add doc  
ester.zhou 已提交
96
        })
E
esterzhou 已提交
97
        .margin({ top: 30, left:10, right:10 })
E
add doc  
ester.zhou 已提交
98 99 100 101
    }
  }
}
```
E
esterzhou 已提交
102
![search](figures/search.png)