Skip to content

Commit 035fa61

Browse files
authored
Add create_new_text and no_results_text, and announce the create row of searchable selects (#1700)
* Render the create row and the no-results text of searchable selects Pass render.option_create and render.no_results to Tom Select, with the texts from data-create_new_text and data no_results_text and the current English texts as default. The create row also gets role="option" and an id: Tom Select gives both only to regular options, so screen readers did not announce the create row when it was the active option. * Pass create_new_text and no_results_text to the select dropdown * Document create_new_text and no_results_text * Add changelog entry for the select dropdown texts * Add select dropdown texts fixture page * Test the dropdown texts and the active create row
1 parent de9ed80 commit 035fa61

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## unreleased
44

5+
- 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))
56
- The `table` component can fetch and display HTML fragments in selected columns using `embed`.
67
- 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))
78
- Updated Rust and browser dependencies, including SQLite, ApexCharts, and Tabler.

‎examples/official-site/sqlpage/migrations/01_documentation.sql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
317317
('searchable', 'For select and multiple-select elements, displays them with a nice dropdown that allows searching for options.', 'BOOLEAN', FALSE, TRUE),
318318
('dropdown', 'An alias for "searchable".', 'BOOLEAN', FALSE, TRUE),
319319
('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),
320+
('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),
321+
('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),
320322
('step', 'The increment of values in an input of type number. Set to 1 to allow only integers.', 'REAL', FALSE, TRUE),
321323
('description', 'A helper text to display near the input field.', 'TEXT', FALSE, TRUE),
322324
('description_md', 'A helper text to display near the input field - formatted using markdown.', 'TEXT', FALSE, TRUE),

‎frontend/src/tomselect.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ function sqlpage_select_dropdown(root: InitRoot) {
1616
}
1717
}
1818

19+
let create_option_count = 0;
20+
1921
function sqlpage_select_dropdown_individual(s: HTMLSelectElement) {
2022
s.removeAttribute("data-pre-init");
2123
// See: https://github.com/orchidjs/tom-select/issues/716
2224
// By default, TomSelect will not retain the focus if s is already focused
2325
// This is a workaround to fix that
2426
const is_focused = s === document.activeElement;
2527

28+
const create_text = s.dataset.create_new_text ?? "Add";
29+
const no_results_text = s.dataset.no_results_text ?? "No results found";
30+
// Tom Select gives role="option" and an id only to regular options. Without
31+
// them, screen readers do not announce the row that creates a new value when
32+
// it is the active option, because aria-activedescendant has nothing to name.
33+
create_option_count += 1;
34+
const create_id = `${s.id || "sqlpage-select"}-create-${create_option_count}`;
35+
2636
const tom = new TomSelect(s, {
2737
load: sqlpage_load_options_source(s.dataset.options_source),
2838
valueField: "value",
@@ -32,6 +42,15 @@ function sqlpage_select_dropdown_individual(s: HTMLSelectElement) {
3242
maxOptions: null,
3343
closeAfterSelect: !s.multiple,
3444
clearAfterSelect: true,
45+
render: {
46+
option_create: (
47+
data: { input: string },
48+
html: (text: string) => string,
49+
) =>
50+
`<div class="create" role="option" id="${html(create_id)}">${html(create_text)} <strong>${html(data.input)}</strong>&hellip;</div>`,
51+
no_results: (_data: unknown, html: (text: string) => string) =>
52+
`<div class="no-results">${html(no_results_text)}</div>`,
53+
},
3554
});
3655
if (is_focused) tom.focus();
3756
s.form?.addEventListener("reset", async () => {

‎sqlpage/templates/form.handlebars‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
{{~#if options_source}} data-options_source="{{options_source}}" {{/if~}}
104104
{{~#if placeholder}} placeholder="{{placeholder}}" {{/if~}}
105105
{{~#if create_new}} data-create_new={{create_new}} {{/if~}}
106+
{{~#if create_new_text}} data-create_new_text="{{create_new_text}}" {{/if~}}
107+
{{~#if no_results_text}} data-no_results_text="{{no_results_text}}" {{/if~}}
106108
>
107109
{{#if empty_option}}<option value="">{{empty_option}}</option>{{/if}}
108110
{{#each (parse_json options)}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT 'form' AS component;
2+
SELECT 'translated' AS name, 'select' AS type, 1 AS searchable, 1 AS create_new,
3+
'Neu anlegen:' AS create_new_text, 'Keine Treffer' AS no_results_text,
4+
'[{"label": "Red", "value": "red"}, {"label": "Blue", "value": "blue"}]' AS options;
5+
SELECT 'default' AS name, 'select' AS type, 1 AS searchable, 1 AS create_new,
6+
'[{"label": "Red", "value": "red"}, {"label": "Blue", "value": "blue"}]' AS options;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, type Page, test } from "../../fixture.ts";
2+
3+
async function search(page: Page, name: string, text: string) {
4+
const field = page
5+
.locator("label")
6+
.filter({ has: page.locator(`select[name="${name}"]`) });
7+
const input = field.locator('input[role="combobox"]');
8+
await field.locator(".ts-control").click();
9+
await input.fill(text);
10+
return { input, dropdown: field.getByRole("listbox") };
11+
}
12+
13+
test("translated texts, and the create row is the active option", async ({
14+
page,
15+
}) => {
16+
const { input, dropdown } = await search(page, "translated", "Gruen");
17+
const create = dropdown.getByRole("option", { name: "Neu anlegen: Gruen…" });
18+
await expect(create).toBeVisible();
19+
await expect(input).toHaveAttribute(
20+
"aria-activedescendant",
21+
(await create.getAttribute("id")) ?? "",
22+
);
23+
await expect(dropdown.getByText("Keine Treffer")).toBeVisible();
24+
});
25+
26+
test("without the properties, the texts stay the same", async ({ page }) => {
27+
const { dropdown } = await search(page, "default", "Gruen");
28+
await expect(
29+
dropdown.getByRole("option", { name: "Add Gruen…" }),
30+
).toBeVisible();
31+
await expect(dropdown.getByText("No results found")).toBeVisible();
32+
});

0 commit comments

Comments
 (0)