Skip to content

Commit 12cc7cf

Browse files
committed
add a dropdown form item that allows multiple selection
fixes #221 fixes #281 see #220
1 parent 4933232 commit 12cc7cf

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
225225
('max', 'The minimum value to accept for an input of type number', 'NUMBER', FALSE, TRUE),
226226
('checked', 'Used only for checkboxes and radio buttons. Indicates whether the checkbox should appear as already checked.', 'BOOL', FALSE, TRUE),
227227
('multiple', 'Used only for select elements. Indicates that multiple elements can be selected simultaneously. When using multiple, you should add square brackets after the variable name: ''my_variable[]'' as name', 'BOOL', FALSE, TRUE),
228+
('dropdown', 'For select and multiple-select elements, displays them with a nice dropdown that allows searching for options.', 'BOOL', FALSE, TRUE),
229+
('create_new', 'In a multiselect with a dropdown, this option allows the user to enter new values, that are not in the list of options.', 'BOOL', FALSE, TRUE),
228230
('step', 'The increment of values in an input of type number. Set to 1 to allow only integers.', 'NUMBER', FALSE, TRUE),
229231
('description', 'A helper text to display near the input field.', 'TEXT', FALSE, TRUE),
230232
('pattern', 'A regular expression that the value must match. For instance, [0-9]{3} will only accept 3 digits.', 'TEXT', FALSE, TRUE),
@@ -298,7 +300,7 @@ SELECT
298300
FROM fruits
299301
```
300302
', json('[{"component":"form"}, '||
301-
'{"name": "Fruit", "type": "select", "value": 1, "options": '||
303+
'{"name": "Fruit", "type": "select", "dropdown": true, "value": 1, "options": '||
302304
'"[{\"label\": \"Orange\", \"value\": 0}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3}]"}
303305
]')),
304306
('form', '### Multi-select
@@ -336,7 +338,7 @@ left join my_user_options
336338
and my_user_options.user_id = $user_id
337339
```
338340
', json('[{"component":"form"},
339-
{"name": "Fruit", "type": "select", "multiple": true, "description": "press ctrl to select multiple values", "options":
341+
{"label": "Fruits", "name": "fruits[]", "type": "select", "multiple": true, "create_new":true, "placeholder": "Good fruits...", "dropdown": true, "description": "press ctrl to select multiple values", "options":
340342
"[{\"label\": \"Orange\", \"value\": 0, \"selected\": true}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3, \"selected\": true}]"}
341343
]')),
342344
('form', 'This example illustrates the use of the `radio` type.

sqlpage/sqlpage.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ td > p {
3030
.text-secondary a {
3131
color: inherit;
3232
text-decoration: underline;
33+
}
34+
35+
/* orchidjs/tom-select#712 */
36+
.ts-wrapper.multi .ts-control>div.active {
37+
border: 1px solid transparent !important;
3338
}

sqlpage/sqlpage.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ function sqlpage_table(){
4444
}
4545
}
4646

47+
function sqlpage_select_dropdown(){
48+
const selects = document.querySelectorAll("[data-pre-init=select-dropdown]");
49+
if (!selects.length) return;
50+
const src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.popular.min.js";
51+
if (!window.TomSelect) {
52+
const script = document.createElement("script");
53+
script.src= src;
54+
script.onload = sqlpage_select_dropdown;
55+
document.head.appendChild(script);
56+
return;
57+
}
58+
for (const s of selects) {
59+
new TomSelect(s, {
60+
create: s.dataset.create_new
61+
});
62+
}
63+
}
64+
4765
let is_leaflet_injected = false;
4866
let is_leaflet_loaded = false;
4967

@@ -138,6 +156,7 @@ function init_components() {
138156
sqlpage_chart();
139157
sqlpage_map();
140158
sqlpage_card();
159+
sqlpage_select_dropdown();
141160
}
142161

143162
document.addEventListener('DOMContentLoaded', init_components);

sqlpage/templates/form.handlebars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
{{~#if required}} required="required" {{/if~}}
6464
{{~#if autofocus}} autofocus {{/if~}}
6565
{{~#if multiple}} multiple {{/if~}}
66+
{{~#if dropdown}} data-pre-init="select-dropdown" {{/if~}}
67+
{{~#if placeholder}} placeholder="{{placeholder}}" {{/if~}}
68+
{{~#if create_new}} data-create_new={{create_new}} {{/if~}}
6669
>
6770
{{#each (parse_json options)}}
6871
<option value="{{value}}" {{#if (or (eq ../value value) selected)}}selected{{/if}}>{{label}}</option>

0 commit comments

Comments
 (0)