ts-basic-components-search.md 4.4 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 11 12 13 14

## Required Permissions

None

## Child Components

E
ester.zhou 已提交
15
Not supported
E
add doc  
ester.zhou 已提交
16 17 18 19 20 21 22

## APIs

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

- Parameters

E
esterzhou 已提交
23 24 25 26 27 28
  | Name        | Type            | Mandatory  | Default Value | 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 已提交
29 30 31 32


## Attributes

E
esterzhou 已提交
33 34 35 36 37 38 39
| Name                     | Type                                    | Default Value | Description                   |
| ----------------------- | ---------------------------------------- | ---- | --------------------- |
| searchButton            | string                                   | –   | Text on the search button located next to the search text box. By default, there is no search button.|
| placeholderColor        | [ResourceColor](../../ui/ts-types.md)    | -    | Placeholder text color.     |
| placeholderFont         | [Font](../../ui/ts-types.md)             | -    | Placeholder text style.   |
| textFont                | [Font](../../ui/ts-types.md)             | -    | Text font for the search text box.          |
| copyOption<sup>9+</sup> | [CopyOptions](ts-basic-components-text.md) | CopyOptions.CrossDevice | Whether copy and paste is allowed.|
E
add doc  
ester.zhou 已提交
40 41 42

## Events

E
esterzhou 已提交
43 44 45 46 47 48 49
| 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 已提交
50 51 52 53 54

## SearchController

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

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 65 66

Sets the position of the caret.

- Parameters

E
esterzhou 已提交
67 68 69
  | Name  | Type  | Mandatory  | Default Value | 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 73 74



##  Example

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

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