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
26 changes: 26 additions & 0 deletions playwright/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,29 @@ test("options selected", async ({ page }) => {
// Assert the first option is now selected
await expect(firstOption).toHaveAttribute("aria-selected", "true");
});

test("down arrow selects first element", async ({ page }) => {
await page.goto("http://127.0.0.1:8080/component/?name=select&");
// Find Select a fruit...
let selectTrigger = page.locator(".select-trigger");
const selectMenu = page.locator(".select-list");
await selectTrigger.focus();

// Select the first option
await page.keyboard.press("ArrowDown");
const firstOption = selectMenu.getByRole("option", { name: "apple" });
await expect(firstOption).toBeFocused();
});

test("up arrow selects last element", async ({ page }) => {
await page.goto("http://127.0.0.1:8080/component/?name=select&");
// Find Select a fruit...
let selectTrigger = page.locator(".select-trigger");
const selectMenu = page.locator(".select-list");
await selectTrigger.focus();

// Select the first option
await page.keyboard.press("ArrowUp");
const firstOption = selectMenu.getByRole("option", { name: "other" });
await expect(firstOption).toBeFocused();
});
4 changes: 4 additions & 0 deletions primitives/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl FocusState {
self.item_count += 1;
}

pub(crate) fn item_count(&self) -> usize {
self.item_count.cloned()
}

pub(crate) fn remove_item(&mut self, index: usize) {
self.item_count -= 1;
if (self.current_focus)() == Some(index) {
Expand Down
8 changes: 8 additions & 0 deletions primitives/src/select/components/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ pub fn SelectList(props: SelectListProps) -> Element {
render: render.into(),
});

use_effect(move || {
if render() {
ctx.focus_state.set_focus(ctx.initial_focus.cloned());
} else {
ctx.initial_focus.set(None);
}
});

rsx! {
if render() {
div {
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/select/components/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub fn Select<T: Clone + PartialEq + 'static>(props: SelectProps<T>) -> Element
typeahead_buffer.take();
}
});
let initial_focus = use_signal(|| None);

use_context_provider(|| SelectContext {
typeahead_buffer,
Expand All @@ -157,6 +158,7 @@ pub fn Select<T: Clone + PartialEq + 'static>(props: SelectProps<T>) -> Element
placeholder: props.placeholder,
typeahead_clear_task,
typeahead_timeout: props.typeahead_timeout,
initial_focus,
});

rsx! {
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/select/components/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ pub fn SelectTrigger(props: SelectTriggerProps) -> Element {
match event.key() {
Key::ArrowUp => {
open.set(true);
ctx.focus_state.focus_last();
ctx.initial_focus.set(ctx.focus_state.item_count().checked_sub(1));
event.prevent_default();
event.stop_propagation();
}
Key::ArrowDown => {
open.set(true);
ctx.focus_state.focus_first();
ctx.initial_focus.set((ctx.focus_state.item_count() > 0).then_some(0));
event.prevent_default();
event.stop_propagation();
}
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/select/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub(super) struct SelectContext {
pub typeahead_clear_task: Signal<Option<Task>>,
/// Timeout before clearing typeahead buffer
pub typeahead_timeout: ReadSignal<Duration>,
/// The initial element to focus once the list is rendered
pub initial_focus: Signal<Option<usize>>,
}

impl SelectContext {
Expand Down
Loading