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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

- New `create_new_text` and `no_results_text` properties on `select` fields translate the dropdown texts "Add …" and "No results found". The row that creates a new value is now announced by screen readers when it is the active option. ([#1699](https://github.com/sqlpage/SQLPage/issues/1699))
- The `table` component can fetch and display HTML fragments in selected columns using `embed`.
- New `navbar_toggle_label` property on the `shell` and `close_label` property on the `alert` component, to translate the accessible names of the menu toggle button and of the alert close button. ([#1632](https://github.com/sqlpage/SQLPage/issues/1632))
- Updated Rust and browser dependencies, including SQLite, ApexCharts, and Tabler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
('searchable', 'For select and multiple-select elements, displays them with a nice dropdown that allows searching for options.', 'BOOLEAN', FALSE, TRUE),
('dropdown', 'An alias for "searchable".', 'BOOLEAN', FALSE, TRUE),
('create_new', 'In a select with a dropdown (`searchable`, `dropdown` or `options_source`), this option allows the user to enter new values, that are not in the list of options. Works for single selects and for multiselects.', 'BOOLEAN', FALSE, TRUE),
('create_new_text', 'Text shown before the typed value in the dropdown row that creates a new value, when `create_new` is set. Replaces the default "Add", for instance to translate it.', 'TEXT', FALSE, TRUE),
('no_results_text', 'Text shown in a select with a dropdown when the search matches no option. Replaces the default "No results found", for instance to translate it.', 'TEXT', FALSE, TRUE),
('step', 'The increment of values in an input of type number. Set to 1 to allow only integers.', 'REAL', FALSE, TRUE),
('description', 'A helper text to display near the input field.', 'TEXT', FALSE, TRUE),
('description_md', 'A helper text to display near the input field - formatted using markdown.', 'TEXT', FALSE, TRUE),
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/tomselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ function sqlpage_select_dropdown(root: InitRoot) {
}
}

let create_option_count = 0;

function sqlpage_select_dropdown_individual(s: HTMLSelectElement) {
s.removeAttribute("data-pre-init");
// See: https://github.com/orchidjs/tom-select/issues/716
// By default, TomSelect will not retain the focus if s is already focused
// This is a workaround to fix that
const is_focused = s === document.activeElement;

const create_text = s.dataset.create_new_text ?? "Add";
const no_results_text = s.dataset.no_results_text ?? "No results found";
// Tom Select gives role="option" and an id only to regular options. Without
// them, screen readers do not announce the row that creates a new value when
// it is the active option, because aria-activedescendant has nothing to name.
create_option_count += 1;
const create_id = `${s.id || "sqlpage-select"}-create-${create_option_count}`;

const tom = new TomSelect(s, {
load: sqlpage_load_options_source(s.dataset.options_source),
valueField: "value",
Expand All @@ -32,6 +42,15 @@ function sqlpage_select_dropdown_individual(s: HTMLSelectElement) {
maxOptions: null,
closeAfterSelect: !s.multiple,
clearAfterSelect: true,
render: {
option_create: (
data: { input: string },
html: (text: string) => string,
) =>
`<div class="create" role="option" id="${html(create_id)}">${html(create_text)} <strong>${html(data.input)}</strong>&hellip;</div>`,
no_results: (_data: unknown, html: (text: string) => string) =>
`<div class="no-results">${html(no_results_text)}</div>`,
},
});
if (is_focused) tom.focus();
s.form?.addEventListener("reset", async () => {
Expand Down
2 changes: 2 additions & 0 deletions sqlpage/templates/form.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
{{~#if options_source}} data-options_source="{{options_source}}" {{/if~}}
{{~#if placeholder}} placeholder="{{placeholder}}" {{/if~}}
{{~#if create_new}} data-create_new={{create_new}} {{/if~}}
{{~#if create_new_text}} data-create_new_text="{{create_new_text}}" {{/if~}}
{{~#if no_results_text}} data-no_results_text="{{no_results_text}}" {{/if~}}
>
{{#if empty_option}}<option value="">{{empty_option}}</option>{{/if}}
{{#each (parse_json options)}}
Expand Down
6 changes: 6 additions & 0 deletions tests/end-to-end/fixtures/select-create-text/index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT 'form' AS component;
SELECT 'translated' AS name, 'select' AS type, 1 AS searchable, 1 AS create_new,
'Neu anlegen:' AS create_new_text, 'Keine Treffer' AS no_results_text,
'[{"label": "Red", "value": "red"}, {"label": "Blue", "value": "blue"}]' AS options;
SELECT 'default' AS name, 'select' AS type, 1 AS searchable, 1 AS create_new,
'[{"label": "Red", "value": "red"}, {"label": "Blue", "value": "blue"}]' AS options;
32 changes: 32 additions & 0 deletions tests/end-to-end/fixtures/select-create-text/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, type Page, test } from "../../fixture.ts";

async function search(page: Page, name: string, text: string) {
const field = page
.locator("label")
.filter({ has: page.locator(`select[name="${name}"]`) });
const input = field.locator('input[role="combobox"]');
await field.locator(".ts-control").click();
await input.fill(text);
return { input, dropdown: field.getByRole("listbox") };
}

test("translated texts, and the create row is the active option", async ({
page,
}) => {
const { input, dropdown } = await search(page, "translated", "Gruen");
const create = dropdown.getByRole("option", { name: "Neu anlegen: Gruen…" });
await expect(create).toBeVisible();
await expect(input).toHaveAttribute(
"aria-activedescendant",
(await create.getAttribute("id")) ?? "",
);
await expect(dropdown.getByText("Keine Treffer")).toBeVisible();
});

test("without the properties, the texts stay the same", async ({ page }) => {
const { dropdown } = await search(page, "default", "Gruen");
await expect(
dropdown.getByRole("option", { name: "Add Gruen…" }),
).toBeVisible();
await expect(dropdown.getByText("No results found")).toBeVisible();
});
Loading