Skip to content

Commit 6fa7a59

Browse files
committed
Fixed a bug where select dropdown fields would retain their state when their form is reset.
fixes #672
1 parent c830f04 commit 6fa7a59

File tree

4 files changed

+14
-33
lines changed

4 files changed

+14
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Avoid generating file names that contain spaces in `sqlpage.persist_uploaded_file`. This makes it easier to use the file name in URLs without URL-encoding it.
1111
- Fixed a bug with REAL value decoding in Microsoft SQL Server.
1212
- Support preserving the timezone of `DATETIMEOFFSET` columns in Microsoft SQL Server and `TIMESTAMPTZ` columns in Postgres. Previously, all datetime columns were converted to UTC.
13+
- Fixed a bug where select dropdown fields would retain their state when their form is reset.
1314

1415
## 0.30.1 (2024-10-31)
1516
- fix a bug where table sorting would break if table search was not also enabled.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ left join my_user_options
368368
on my_options.id = my_user_options.option_id
369369
and my_user_options.user_id = $user_id
370370
```
371-
', json('[{"component":"form", "action":"examples/show_variables.sql"},
371+
', json('[{"component":"form", "action":"examples/show_variables.sql", "reset": "Reset"},
372372
{"label": "Fruits", "name": "fruits[]", "type": "select", "multiple": true, "create_new":true, "placeholder": "Good fruits...", "searchable": true, "description": "press ctrl to select multiple values", "options":
373373
"[{\"label\": \"Orange\", \"value\": 0, \"selected\": true}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3, \"selected\": true}]"}
374374
]')),

sqlpage/sqlpage.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ function sqlpage_table() {
7070
}
7171
}
7272

73-
function sqlpage_select_dropdown() {
74-
const selects = document.querySelectorAll("[data-pre-init=select-dropdown]");
75-
if (!selects.length) return;
76-
const src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.popular.min.js";
77-
if (!window.TomSelect) {
78-
const script = document.createElement("script");
79-
script.src = src;
80-
script.integrity = "sha384-aAqv9vleUwO75zAk1sGKd5VvRqXamBXwdxhtihEUPSeq1HtxwmZqQG/HxQnq7zaE";
81-
script.crossOrigin = "anonymous";
82-
script.nonce = nonce;
83-
script.onload = sqlpage_select_dropdown;
84-
document.head.appendChild(script);
85-
return;
86-
}
87-
for (const s of selects) {
88-
new TomSelect(s, {
89-
create: s.dataset.create_new
90-
});
91-
}
92-
}
93-
9473
let is_leaflet_injected = false;
9574
let is_leaflet_loaded = false;
9675

sqlpage/tomselect.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/* !include https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.popular.min.js */
22

33
function sqlpage_select_dropdown() {
4-
for (const s of document.querySelectorAll("[data-pre-init=select-dropdown]")) {
5-
// See: https://github.com/orchidjs/tom-select/issues/716
6-
// By default, TomSelect will not retain the focus if s is already focused
7-
// This is a workaround to fix that
8-
const is_focused = s === document.activeElement;
9-
const tom = new TomSelect(s, {
10-
create: s.dataset.create_new,
11-
maxOptions: null,
12-
});
13-
if (is_focused) tom.focus();
14-
}
4+
for (const s of document.querySelectorAll("[data-pre-init=select-dropdown]")) {
5+
// See: https://github.com/orchidjs/tom-select/issues/716
6+
// By default, TomSelect will not retain the focus if s is already focused
7+
// This is a workaround to fix that
8+
const is_focused = s === document.activeElement;
9+
const tom = new TomSelect(s, {
10+
create: s.dataset.create_new,
11+
maxOptions: null,
12+
});
13+
if (is_focused) tom.focus();
14+
s.form?.addEventListener("reset", () => setTimeout(tom.sync.bind(tom), 0));
15+
}
1516
}
1617

1718
add_init_fn(sqlpage_select_dropdown);

0 commit comments

Comments
 (0)