Skip to content

Commit a065bb8

Browse files
authored
Fix form options source query parameters (#1356)
1 parent 2177025 commit a065bb8

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## unreleased
44

55
- `sqlpage.send_mail` now supports rich email bodies. Use `body_html` for a caller-provided HTML alternative, or `body_md` to render Markdown as HTML. Messages retain a plain-text alternative; `body` may be omitted when `body_md` is used, and `body_md` and `body_html` cannot be combined.
6+
- Form `options_source` URLs now preserve existing query parameters when adding the dynamic `search` parameter.
67

78
## v0.45
89

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ We''ll write a second SQL file, `options_source.sql`, that will receive the user
442442
443443
When both `options` and `options_source` are set, the local `options` are loaded first. Search results from `options_source` are loaded into the same option list; a result with the same `value` updates the existing option. This is useful to set a default preselected value with `options_source`.
444444
445+
`options_source` may already contain query parameters. SQLPage preserves them and adds or replaces the `search` parameter when loading options.
446+
445447
##### `options_source.sql`
446448
447449
```sql
@@ -458,7 +460,7 @@ where label like $search || ''%'';
458460
{"name": "component", "type": "select",
459461
"value": "form",
460462
"options": [{"label": "Form", "value": "form"}],
461-
"options_source": "examples/from_component_options_source.sql",
463+
"options_source": "examples/from_component_options_source.sql?category=component",
462464
"description": "Start typing the name of a component like ''map'' or ''form''..."
463465
}]')),
464466
('form', 'This example illustrates the use of the `radio` type.

‎sqlpage/tomselect.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ function sqlpage_load_options_source(options_source) {
5353
if (!options_source) return;
5454
return async (query, callback) => {
5555
const err = (label) => callback([{ label, value: "" }]);
56-
const resp = await fetch(
57-
`${options_source}?search=${encodeURIComponent(query)}`,
58-
);
56+
const options_url = new URL(options_source, document.baseURI);
57+
options_url.searchParams.set("search", query);
58+
const resp = await fetch(options_url);
5959
if (!resp.ok) {
6060
return err(
6161
`Error loading options from "${options_source}": ${resp.status} ${resp.statusText}`,

‎tests/end-to-end/official-site.spec.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ test("form select combines initial options with remote search results", async ({
213213

214214
const select = page
215215
.locator(
216-
'select[data-options_source="examples/from_component_options_source.sql"]',
216+
'select[data-options_source="examples/from_component_options_source.sql?category=component"]',
217217
)
218218
.first();
219219
await expect(select).toBeAttached();
220220
await page.waitForFunction(
221221
() =>
222222
!!document.querySelector<HTMLSelectElement>(
223-
'select[data-options_source="examples/from_component_options_source.sql"]',
223+
'select[data-options_source="examples/from_component_options_source.sql?category=component"]',
224224
)?.tomselect,
225225
);
226226

@@ -246,7 +246,9 @@ test("form select combines initial options with remote search results", async ({
246246
await page.waitForResponse((response) =>
247247
response
248248
.url()
249-
.includes("examples/from_component_options_source.sql?search=form"),
249+
.includes(
250+
"examples/from_component_options_source.sql?category=component&search=form",
251+
),
250252
);
251253
await expect
252254
.poll(async () =>
@@ -265,7 +267,9 @@ test("form select combines initial options with remote search results", async ({
265267
await page.waitForResponse((response) =>
266268
response
267269
.url()
268-
.includes("examples/from_component_options_source.sql?search=map"),
270+
.includes(
271+
"examples/from_component_options_source.sql?category=component&search=map",
272+
),
269273
);
270274
await expect
271275
.poll(async () =>

0 commit comments

Comments
 (0)