From 96f314bfe704a69325f99725631a899f5dae87c7 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 11 Sep 2023 12:58:45 +0200 Subject: [PATCH] ComboBox: Fix inconsistent suggestions We require "Create ..." element to be only focused when there are no suggestions available. This causes some issues, depending on the state, the least focusable index might be either 0 ("Create...") or 1. This patch addresses all the quirks with focus. --- assets/js/liveview/suggestions_dropdown.js | 64 ++++++++++++++----- .../live/components/combo_box.ex | 11 ++-- .../live/components/combo_box_test.exs | 20 ------ 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/assets/js/liveview/suggestions_dropdown.js b/assets/js/liveview/suggestions_dropdown.js index 99026f195..f89cf7b8d 100644 --- a/assets/js/liveview/suggestions_dropdown.js +++ b/assets/js/liveview/suggestions_dropdown.js @@ -1,18 +1,50 @@ // Courtesy of Benjamin von Polheim: // https://blog.devgenius.io/build-a-performat-autocomplete-using-phoenix-liveview-and-alpine-js-8bcbbed17ba7 -let suggestionsDropdown = function(id, opts) { +let suggestionsDropdown = function(id) { return { isOpen: false, id: id, - open() { this.isOpen = true }, - close() { this.isOpen = false }, - focus: opts.focus ?? 0, + focus: null, setFocus(f) { this.focus = f; }, + initFocus() { + if (this.focus === null) { + this.setFocus(this.leastFocusableIndex()) + } + }, + open() { + this.initFocus() + this.isOpen = true + }, + suggestionsCount() { + return this.$refs.suggestions?.querySelectorAll('li').length + }, + hasCreatableOption() { + return this.$refs.suggestions?.querySelector('li').classList.contains("creatable") + }, + leastFocusableIndex() { + if (this.suggestionsCount() === 0) { + return 0 + } + return this.hasCreatableOption() ? 0 : 1 + }, + maxFocusableIndex() { + return this.hasCreatableOption() ? this.suggestionsCount() - 1 : this.suggestionsCount() + }, + nextFocusableIndex() { + const currentFocus = this.focus + return currentFocus + 1 > this.maxFocusableIndex() ? this.leastFocusableIndex() : currentFocus + 1 + }, + prevFocusableIndex() { + const currentFocus = this.focus + return currentFocus - 1 >= this.leastFocusableIndex() ? currentFocus - 1 : this.maxFocusableIndex() + }, + close() { this.isOpen = false }, select() { this.$refs[`dropdown-${this.id}-option-${this.focus}`]?.click() - this.focusPrev() + this.close() + document.getElementById(this.id).blur() }, scrollTo(idx) { this.$refs[`dropdown-${this.id}-option-${idx}`]?.scrollIntoView( @@ -20,23 +52,21 @@ let suggestionsDropdown = function(id, opts) { ) }, focusNext() { - const nextIndex = this.focus + 1 - const total = this.$refs.suggestions?.childElementCount ?? 0 + const nextIndex = this.nextFocusableIndex() if (!this.isOpen) this.open() - if (nextIndex < total) { - this.setFocus(nextIndex) - this.scrollTo(nextIndex); - } + this.setFocus(nextIndex) + this.scrollTo(nextIndex) }, focusPrev() { - const nextIndex = this.focus - 1 - if (this.isOpen && nextIndex >= 0) { - this.setFocus(nextIndex) - this.scrollTo(nextIndex) - } - }, + const prevIndex = this.prevFocusableIndex() + + if (!this.isOpen) this.open() + + this.setFocus(prevIndex) + this.scrollTo(prevIndex) + } } } diff --git a/lib/plausible_web/live/components/combo_box.ex b/lib/plausible_web/live/components/combo_box.ex index 186008050..17c2757c2 100644 --- a/lib/plausible_web/live/components/combo_box.ex +++ b/lib/plausible_web/live/components/combo_box.ex @@ -80,7 +80,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do
0, do: 1, else: 0 - end end diff --git a/test/plausible_web/live/components/combo_box_test.exs b/test/plausible_web/live/components/combo_box_test.exs index 54b2fb32e..1cc76fe6c 100644 --- a/test/plausible_web/live/components/combo_box_test.exs +++ b/test/plausible_web/live/components/combo_box_test.exs @@ -57,26 +57,6 @@ defmodule PlausibleWeb.Live.Components.ComboBoxTest do assert text_of_attr(li2, "x-bind:class") =~ "focus === 2" end - test "Alpine.js: component refers to window.suggestionsDropdown including initial focus value" do - assert new_options(2) - |> render_sample_component() - |> find("div#input-picker-main-test-component") - |> text_of_attr("x-data") =~ - "window.suggestionsDropdown('test-component', {focus: 0})" - - assert new_options(2) - |> render_sample_component(creatable: true) - |> find("div#input-picker-main-test-component") - |> text_of_attr("x-data") =~ - "window.suggestionsDropdown('test-component', {focus: 1})" - - assert [] - |> render_sample_component(creatable: true) - |> find("div#input-picker-main-test-component") - |> text_of_attr("x-data") =~ - "window.suggestionsDropdown('test-component', {focus: 0})" - end - test "Alpine.js: component sets up keyboard navigation" do main = new_options(2)