Skip to content

Commit

Permalink
Add option to keep focus when tab used to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Feb 17, 2023
1 parent ea07b57 commit f4c0c4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 0 additions & 1 deletion examples/styling.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Grid > Container {

#dropdown-3 .autocomplete--selection-cursor {
background: $success 20%;

}

#dropdown-4 {
Expand Down
11 changes: 9 additions & 2 deletions textual_autocomplete/_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(
self,
input: Input,
dropdown: Dropdown,
tab_moves_focus: bool = False,
*,
id: str | None = None,
classes: str | None = None,
Expand All @@ -144,11 +145,13 @@ def __init__(
Args:
input: The input widget that you want to power the dropdown.
dropdown: The dropdown widget. This will be populated by AutoComplete.
tab_moves_focus: Set to True to also shift focus after completing using the Tab key.
"""
super().__init__(id=id, classes=classes)
self.input = input
self.dropdown = dropdown
self.dropdown.input_widget = self.input
self.tab_moves_focus = tab_moves_focus

def compose(self) -> ComposeResult:
yield self.input
Expand All @@ -171,7 +174,12 @@ def on_key(self, event: events.Key) -> None:
self.dropdown.close()
event.stop()
elif key == "tab":
self._select_item()
# Only interfere if there's a dropdown visible,
# otherwise, we want things to behave like a normal input.
if self.dropdown.display:
self._select_item()
if not self.tab_moves_focus:
event.stop() # Prevent focus change

def on_input_submitted(self, event: Input.Submitted) -> None:
self._select_item()
Expand Down Expand Up @@ -277,7 +285,6 @@ def on_mount(self, event: events.Mount) -> None:
callback=self._input_value_changed,
)

# TODO - this watcher wasn't firing, potential Textual issue.
self.watch(
self.input_widget,
attribute_name="cursor_position",
Expand Down

0 comments on commit f4c0c4b

Please sign in to comment.