Skip to content

Commit 9a98111

Browse files
authored
Creating item_activated callback for material components SearchBar widget (#9778)
Currently, `SearchBar` presents a list of items but is hard coded to treat each of these items as "precanned" search text. This PR introduces an `item_activated` callback to allow hosts to override this default behavior.
1 parent 9444b47 commit 9a98111

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

ui-libraries/material/docs/src/content/docs/components/AppBars/search_bar.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ Invoked when a key is pressed in the search bar.
9595
### key-released(event: KeyEvent) -> EventResult
9696
Invoked when a key is released in the search bar.
9797

98+
### item-activated(index: int) -> bool
99+
Invoked when a search item is clicked. If a callback returns 'true', then do not perform the default behavior of changing the search text.
100+
98101
## Functions
99102

100103
### clear-focus()

ui-libraries/material/src/ui/components/search_bar.slint

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export component SearchBar {
132132
callback action_button_clicked(index: int);
133133
callback key_pressed(event: KeyEvent) -> EventResult;
134134
callback key_released(event: KeyEvent) -> EventResult;
135+
callback item-activated(index: int) -> bool;
135136

136137
property <color> color: MaterialPalette.on_surface_variant;
137138
property <length> item_height: MaterialStyleMetrics.size_72;
@@ -288,7 +289,9 @@ export component SearchBar {
288289
action_button_icon: item.action_button_icon;
289290

290291
clicked => {
291-
root.text = self.text;
292+
if (!root.item-activated(index)) {
293+
root.text = self.text;
294+
}
292295
popup.close();
293296
}
294297

0 commit comments

Comments
 (0)