diff --git a/CHANGELOG.md b/CHANGELOG.md index 06959dff1..67191b615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index d82b0214e..9063fa289 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -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), diff --git a/frontend/src/tomselect.ts b/frontend/src/tomselect.ts index fc97a66e3..37e252aa7 100644 --- a/frontend/src/tomselect.ts +++ b/frontend/src/tomselect.ts @@ -16,6 +16,8 @@ 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 @@ -23,6 +25,14 @@ function sqlpage_select_dropdown_individual(s: HTMLSelectElement) { // 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", @@ -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, + ) => + `