Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/flicker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,30 @@ defmodule Flicker do
doc: "Input placeholder. Defaults to the `:search_placeholder` message."
)

attr(:aria_labelledby, :string,
default: nil,
doc: """
The id of a host element that names the combobox input, such as the
form field's own `<label>`. When set, Flicker renders no visually-hidden
label of its own, so the name is not read twice, and the listbox is
labelled by the same element.
"""
)

attr(:aria_describedby, :string,
default: nil,
doc: """
Space-separated ids of host elements that describe the combobox input,
such as the form field's help text and error. Appended after Flicker's
own hint ids on the input's `aria-describedby`.
"""
)

attr(:aria_invalid, :boolean,
default: false,
doc: "Renders `aria-invalid=\"true\"` on the combobox input when `true`."
)

attr(:debounce, :integer,
default: nil,
doc: "Input debounce (ms). Defaults to `config :flicker, :default_debounce` (150)."
Expand Down Expand Up @@ -376,6 +400,9 @@ defmodule Flicker do
min_chars={@min_chars}
placeholder={@placeholder}
debounce={@debounce}
aria_labelledby={@aria_labelledby}
aria_describedby={@aria_describedby}
aria_invalid={@aria_invalid}
dispatch={@dispatch}
loading_delay={@loading_delay}
theme={@theme}
Expand Down
44 changes: 39 additions & 5 deletions lib/flicker/components/select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ defmodule Flicker.Components.Select do
|> assign_new(:limit, fn -> @default_limit end)
|> assign_new(:min_chars, fn -> @default_min_chars end)
|> assign_new(:placeholder, fn -> nil end)
|> assign_new(:aria_describedby, fn -> nil end)
|> assign_new(:aria_invalid, fn -> false end)
|> assign_new(:aria_labelledby, fn -> nil end)
|> assign_new(:dispatch, fn -> @default_dispatch end)
|> assign_new(:loading_delay, fn -> @default_loading_delay end)
# What is asking for the current search, for `Flicker.Dispatch` to judge
Expand Down Expand Up @@ -1116,10 +1119,12 @@ defmodule Flicker.Components.Select do
end
end

defp describedby(input_id, dispatch_pending, trigger_hint) do
# The host's ids go last so Flicker's live hints are announced first.
defp describedby(input_id, dispatch_pending, trigger_hint, host_describedby) do
[
dispatch_pending && "#{input_id}-dispatch-hint",
trigger_hint && "#{input_id}-trigger-hint"
trigger_hint && "#{input_id}-trigger-hint",
host_describedby
]
|> Enum.filter(& &1)
|> case do
Expand Down Expand Up @@ -1278,7 +1283,13 @@ defmodule Flicker.Components.Select do
# two. Called as `{combobox_input(assign(assigns, :input_class, ...))}`.
defp combobox_input(assigns) do
~H"""
<label id={"#{@input_id}-label"} for={@input_id} class="flicker-sr-only" style={@sr_only_style}>
<label
:if={!@aria_labelledby}
id={"#{@input_id}-label"}
for={@input_id}
class="flicker-sr-only"
style={@sr_only_style}
>
{message(assigns, :search_placeholder)}
</label>
<input
Expand All @@ -1296,7 +1307,9 @@ defmodule Flicker.Components.Select do
placeholder={@placeholder || message(assigns, :search_placeholder)}
disabled={!@connected?}
aria-keyshortcuts={@aria_keyshortcuts}
aria-describedby={describedby(@input_id, @dispatch_pending, @trigger_hint)}
aria-describedby={describedby(@input_id, @dispatch_pending, @trigger_hint, @aria_describedby)}
aria-labelledby={@aria_labelledby}
aria-invalid={@aria_invalid && "true"}
phx-keyup="query"
phx-debounce={Dispatch.debounce_attr(@dispatch, @debounce)}
phx-focus="focus"
Expand Down Expand Up @@ -1593,7 +1606,7 @@ defmodule Flicker.Components.Select do
:if={@open}
id={@listbox_id}
role="listbox"
aria-labelledby={"#{@input_id}-label"}
aria-labelledby={@aria_labelledby || "#{@input_id}-label"}
aria-busy={@aria_busy}
class={[@theme.listbox, @results_stale && @theme.results_stale]}
>
Expand Down Expand Up @@ -1983,15 +1996,36 @@ defmodule Flicker.Components.Select do
this.cursorInput = input
this.onKeyupCursor = () => input.setAttribute("phx-value-cursor", String(input.selectionStart))
this.onClickOrSelectCursor = () => this.pushEventTo(this.el, "cursor", { cursor: input.selectionStart })
// Text that arrives with no key held (context-menu paste, drag
// and drop, browser autofill) gets no keyup, so `phx-keyup`
// never pushes "query" for it. Typing and Ctrl/Cmd+V hold a key
// down across the `input` event and still go through `phx-keyup`
// and its debounce, so they are not pushed twice.
this.keysDown = 0
this.onKeydownTrack = () => { this.keysDown += 1 }
this.onKeyupTrack = () => { this.keysDown = Math.max(0, this.keysDown - 1) }
this.onBlurTrack = () => { this.keysDown = 0 }
this.onKeylessInput = () => {
if (this.keysDown > 0) return
this.pushEventTo(this.el, "query", { value: input.value, cursor: String(input.selectionStart) })
}
input.addEventListener("keyup", this.onKeyupCursor)
input.addEventListener("click", this.onClickOrSelectCursor)
input.addEventListener("select", this.onClickOrSelectCursor)
input.addEventListener("keydown", this.onKeydownTrack)
input.addEventListener("keyup", this.onKeyupTrack)
input.addEventListener("blur", this.onBlurTrack)
input.addEventListener("input", this.onKeylessInput)
},
detachCursorReporting() {
if (!this.cursorInput) return
this.cursorInput.removeEventListener("keyup", this.onKeyupCursor)
this.cursorInput.removeEventListener("click", this.onClickOrSelectCursor)
this.cursorInput.removeEventListener("select", this.onClickOrSelectCursor)
this.cursorInput.removeEventListener("keydown", this.onKeydownTrack)
this.cursorInput.removeEventListener("keyup", this.onKeyupTrack)
this.cursorInput.removeEventListener("blur", this.onBlurTrack)
this.cursorInput.removeEventListener("input", this.onKeylessInput)
this.cursorInput = null
},
registerChord(chord) {
Expand Down
123 changes: 123 additions & 0 deletions test/flicker/select_field_aria_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
defmodule Flicker.SelectFieldAriaTest do
@moduledoc """
Covers the attrs a host form field uses to own the combobox's accessible
name and description (`aria_labelledby`, `aria_describedby`,
`aria_invalid`), and the keyless "query" push the colocated hook sends for
pasted, dropped, or autofilled text.

The hook's `input` listener itself is client-side JS. ExUnit can only send
the event it pushes and check the server runs a search for it.
"""

use Flicker.Test.ConnCase, async: true

import Phoenix.LiveViewTest

defmodule FieldHostLive do
@moduledoc false
use Phoenix.LiveView

@results [
%Flicker.Result{value: "1", label: "Casey Cassidy", sublabel: "Bass"},
%Flicker.Result{value: "2", label: "Alex Rivers", sublabel: "Drums"}
]

@impl true
def mount(_params, session, socket) do
socket =
socket
|> Phoenix.Component.assign(:aria, Map.get(session, "aria", %{}))
|> Phoenix.Component.assign(:dispatch, Map.get(session, "dispatch", :immediate))

{:ok, socket}
end

@impl true
def render(assigns) do
~H"""
<label id="host-label" for="picker-input">Language</label>
<Flicker.select
id="picker"
source={{Flicker.Providers.Static, results: results()}}
on_select={:picked}
dispatch={@dispatch}
aria_labelledby={@aria["labelledby"]}
aria_describedby={@aria["describedby"]}
aria_invalid={@aria["invalid"] || false}
/>
"""
end

defp results, do: @results
end

defp mount_host(conn, aria, dispatch \\ :immediate) do
live_isolated(conn, FieldHostLive, session: %{"aria" => aria, "dispatch" => dispatch})
end

describe "aria_labelledby" do
test "without it, Flicker's own sr-only label names the input and listbox", %{conn: conn} do
{:ok, view, html} = mount_host(conn, %{})

assert html =~ ~s(id="picker-input-label")
refute has_element?(view, "#picker-input[aria-labelledby]")

view |> element("#picker-input") |> render_focus()
assert has_element?(view, "[role=listbox][aria-labelledby='picker-input-label']")
end

test "with it, the sr-only label is dropped and the host's id names input and listbox", %{conn: conn} do
{:ok, view, html} = mount_host(conn, %{"labelledby" => "host-label"})

refute html =~ ~s(id="picker-input-label")
assert has_element?(view, "#picker-input[aria-labelledby='host-label']")

view |> element("#picker-input") |> render_focus()
assert has_element?(view, "[role=listbox][aria-labelledby='host-label']")
end
end

describe "aria_describedby" do
test "is absent by default", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{})
refute has_element?(view, "#picker-input[aria-describedby]")
end

test "carries the host's ids", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{"describedby" => "field-help field-error"})
assert has_element?(view, "#picker-input[aria-describedby='field-help field-error']")
end

test "follows Flicker's own hint ids", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{"describedby" => "field-help"}, :enter)

view |> element("#picker-input") |> render_keyup(%{"value" => "cas"})

assert has_element?(view, "#picker-input[aria-describedby='picker-input-dispatch-hint field-help']")
end
end

describe "aria_invalid" do
test "is not rendered when false", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{})
refute has_element?(view, "#picker-input[aria-invalid]")
end

test "renders aria-invalid=true when true", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{"invalid" => true})
assert has_element?(view, "#picker-input[aria-invalid='true']")
end
end

describe "keyless input" do
test "a hook-pushed query with no key runs a search", %{conn: conn} do
{:ok, view, _html} = mount_host(conn, %{})

view |> element("#picker") |> render_hook("query", %{"value" => "cas", "cursor" => "3"})
html = render_async(view)

assert html =~ "Casey Cassidy"
refute html =~ "Alex Rivers"
end
end
end
Loading