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

E
ester.zhou 已提交
3 4
> **NOTE**
>
5
> 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 已提交
6

E
ester.zhou 已提交
7
The **\<Search>** component provides an input area for users to search.
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

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


## Attributes

33
| Name | Type | Default Value | Description |
E
add doc  
ester.zhou 已提交
34
| -------- | -------- | -------- | -------- |
35 36 37 38
| 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. |
E
add doc  
ester.zhou 已提交
39 40 41

## Events

42
| Name | Description |
E
add doc  
ester.zhou 已提交
43
| -------- | -------- |
E
ester.zhou 已提交
44 45 46 47 48
| onSubmit(callback: (value: string) => void) | Triggered when users click the search icon or the search button, or tap 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 已提交
49 50 51 52 53

## SearchController

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

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

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

Sets the position of the caret.

- Parameters

66
  | Name | Type | Mandatory | Default Value | Description |
E
add doc  
ester.zhou 已提交
67
  | ---- | ------ | ---- | ---- | --------------------- |
E
ester.zhou 已提交
68
  | value | number | Yes   | - | Length from the start of the text string to the position where the caret is located. |
E
add doc  
ester.zhou 已提交
69 70 71 72 73



##  Example

E
ester.zhou 已提交
74 75
```ts
// xxx.ets
E
add doc  
ester.zhou 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
@Entry
@Component
struct SearchExample {
  @State changevalue: string = ''
  @State submitvalue: string = ''
  controller: SearchController = new SearchController()

  build() {
    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text(this.submitvalue)
      Text(this.changevalue)
      Search({value: '', placeholder: 'Type to search', controller: this.controller})
        .searchButton('Search')
        .width(400)
        .height(35)
        .backgroundColor(Color.White)
        .placeholderColor(Color.Grey)
        .placeholderFont({ size: 50, weight: 10, family: 'serif', style: FontStyle.Normal })
        .onSubmit((value: string) => {
          this.submitvalue = value
        })
        .onChange((value: string) => {
          this.changevalue = value
        })
        .margin({ top: 30 })
    }
  }
}
```