Skip to content

Commit

Permalink
fix: resolve multiple search UX issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed Jan 1, 2025
1 parent f5660f0 commit 1cb2856
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app/components/search/search.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
id="term"
class="block w-full min-w-0 flex-1 rounded-none rounded-r-md border-gray-300 px-3 py-2 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
[placeholder]="subRedditName$ | async"
(keydown)="preventSpace($event)"
/>
</div>
</div>
Expand Down
19 changes: 16 additions & 3 deletions src/app/components/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,31 @@ export class SearchComponent {
term: ''
})

/**
* Prevents the space key from being entered into the search field.
*/
public preventSpace(event: KeyboardEvent) {
if (
event.key === ' ' &&
!event.metaKey &&
!event.ctrlKey &&
!event.altKey
) {
event.preventDefault()
}
}

/**
* Handles submission of the search field.
* The input is trimmed to remove any leading or trailing white space
* as this can lead to a frustrating user experience.
*/
public onSubmit(event: Event): void {
if (this.searchForm.value.term) {
this.redditService.setSubRedditName(this.searchForm.value.term.trim())
}

/**
* Closes the keyboard on mobile devices after submission.
* Closes the keyboard on mobile devices after submission of the search form.
* This is necessary because the keyboard does not close automatically on mobile devices.
*/
event.preventDefault()
const inputElement = (event.target as HTMLFormElement).querySelector(
Expand Down

0 comments on commit 1cb2856

Please sign in to comment.