1. Basic Usage
-
Import and Creation: In an ETS file, first import the Search component via
import { Search } from '@ohos.arkui.advanced.Search'. Then create a Search instance in the component'sbuildmethod:
```typescript Search({ value: this.searchText, placeholder: 'Search...' }) .width('90%') .height(40) .onSubmit((value: string) => { // Handle search submission logic }) .onChange((value: string) => { // Handle input change logic }); ```
This sets the initial text (value), placeholder text, dimensions, and event listeners for onSubmit (search submission) and onChange (input changes).
2. Property Configuration
-
Search Button Style: Customize the search button using
searchButton:
.searchButton('SEARCH', { fontSize: '16fp', fontColor: '#ff3f97e9' }) -
Placeholder Styling:
placeholderColorsets placeholder text color
placeholderFontsets placeholder font style:
.placeholderColor(Color.Grey) .placeholderFont({ size: 14, weight: 400 }) Text Styling:
textFontcustomizes input text appearanceAlignment:
textAlign(API 9+) sets text alignment (TextAlign.Startdefault)Icon Styling:
searchIcon(API 10+) customizes search icon
cancelButtoncustomizes clear buttonText Copying:
copyOption(API 9+) enables text copying (default:CopyOptions.LocalDevice)Additional Properties:
fontColor- Input text color
caretStyle- Cursor appearance
enableKeyboardOnFocus- Auto-open keyboard on focus
3. Event Handling
-
Search Submission:
onSubmittriggers when: • Search icon clicked • Search button pressed • Keyboard search key pressed -
Input Changes:
onChangetriggers during text input -
Text Operations:
onCopy- Copy operationsonCut- Cut operationsonPaste- Paste operationsonTextSelectionChange- Text selection changesonContentScroll- Content scrolling
4. Controller Usage
-
Controller Creation:
controller: SearchController = new SearchController() -
Cursor Control:
this.controller.caretPosition(1)positions cursor after first character -
Exit Edit Mode:
this.controller.stopEditing()closes keyboard (custom keyboard scenarios)
5. Custom Keyboards
-
Create custom keyboard component:
@Builder CustomKeyboardBuilder() { Column() { // Custom keyboard layout and logic } } -
Bind to Search component:
Search({ controller: this.controller, value: this.inputValue}) .customKeyboard(this.CustomKeyboardBuilder())
6. Input Types & Restrictions
-
Input Types (API 11+):
SearchType.Normal- Default text inputSearchType.NUMBER- Numeric onlySearchType.PHONE_NUMBER- Phone number formatSearchType.EMAIL- Email format -
Length Limit:
maxLengthrestricts character count
7. Advanced Features
-
Text Styling (API 12+):
decoration- Text underline stylingtextIndent- First-line indentationletterSpacing- Character spacinglineHeight- Line heightselectedBackgroundColor- Selection highlight color -
Input Preview:
enablePreviewTextenables/disables input preview (default enabled) -
Input Filtering:
inputFilteruses regex to restrict allowed characters
By combining these features, developers can create powerful search components tailored to specific application requirements. Consider API version support when implementing features, and leverage new capabilities to optimize search functionality.
Top comments (0)