@clarusjs/ui-primitives 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/src/accordion-item.tscl +9 -3
- package/src/autocomplete.tscl +115 -0
- package/src/checkbox-group-key.ts +5 -0
- package/src/checkbox-group.tscl +96 -0
- package/src/checkbox-item.tscl +18 -0
- package/src/checkbox.tscl +15 -7
- package/src/chip-input.tscl +24 -15
- package/src/combobox.tscl +44 -35
- package/src/context-menu.tscl +100 -0
- package/src/date-field.tscl +140 -0
- package/src/date-segments.ts +55 -0
- package/src/dialog-close.tscl +7 -2
- package/src/dialog-trigger.tscl +7 -2
- package/src/dialog.tscl +10 -4
- package/src/{dismiss.js → dismiss.ts} +15 -18
- package/src/drag-drop.ts +116 -0
- package/src/field.tscl +42 -27
- package/src/fieldset.tscl +37 -0
- package/src/{focus-return.js → focus-return.ts} +2 -4
- package/src/form-errors.tscl +40 -0
- package/src/{index.js → index.ts} +8 -8
- package/src/menu-item.tscl +9 -3
- package/src/menu-list.tscl +1 -1
- package/src/menu.tscl +8 -5
- package/src/menubar.tscl +69 -0
- package/src/nav-menu-key.ts +5 -0
- package/src/nav-menu-panel.tscl +11 -0
- package/src/nav-menu-trigger.tscl +9 -0
- package/src/nav-menu.tscl +78 -0
- package/src/number-field.tscl +139 -0
- package/src/popover-trigger.tscl +7 -2
- package/src/popover.tscl +21 -14
- package/src/{position.js → position.ts} +21 -17
- package/src/preview-card-trigger.tscl +48 -0
- package/src/preview-card.tscl +27 -0
- package/src/radio-group.tscl +12 -7
- package/src/radio.tscl +13 -6
- package/src/{resize-column.js → resize-column.ts} +12 -16
- package/src/{roving-focus.js → roving-focus.ts} +19 -20
- package/src/select.tscl +27 -17
- package/src/sidebar-trigger.tscl +9 -6
- package/src/{sidebar.js → sidebar.ts} +5 -3
- package/src/sidebar.tscl +11 -4
- package/src/sortable-list.tscl +50 -0
- package/src/swipe.ts +79 -0
- package/src/switch.tscl +15 -7
- package/src/tab-panel.tscl +9 -4
- package/src/tab.tscl +11 -6
- package/src/tabs.tscl +12 -7
- package/src/time-field.tscl +139 -0
- package/src/toast-region.tscl +10 -4
- package/src/toast.ts +61 -0
- package/src/toggle-group.tscl +15 -9
- package/src/toggle-item.tscl +13 -7
- package/src/toggle.tscl +4 -1
- package/src/toolbar-separator.tscl +9 -0
- package/src/toolbar.tscl +30 -0
- package/src/tooltip-trigger.tscl +10 -6
- package/src/tooltip.tscl +11 -5
- package/src/{transition.js → transition.ts} +3 -6
- package/src/{typeahead.js → typeahead.ts} +20 -20
- package/src/virtual-grid.ts +100 -0
- package/src/virtual-keyboard.ts +36 -0
- package/src/virtual-list.ts +82 -0
- package/src/dismiss.d.ts +0 -27
- package/src/focus-return.d.ts +0 -10
- package/src/index.d.ts +0 -8
- package/src/position.d.ts +0 -36
- package/src/resize-column.d.ts +0 -18
- package/src/roving-focus.d.ts +0 -39
- package/src/sidebar.d.ts +0 -8
- package/src/toast.d.ts +0 -48
- package/src/toast.js +0 -56
- package/src/transition.d.ts +0 -18
- package/src/typeahead.d.ts +0 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/ui-primitives",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Accessible behaviours for clarusjs components: roving focus, typeahead, dismiss, transitions, focus return and positioning, as small DOM functions for [use].",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,14 +15,15 @@
|
|
|
15
15
|
"prefix": "ui"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
|
-
".": "./src/index.
|
|
18
|
+
".": "./src/index.ts",
|
|
19
|
+
"./*.js": "./src/*.ts",
|
|
19
20
|
"./*": "./src/*"
|
|
20
21
|
},
|
|
21
22
|
"publishConfig": {
|
|
22
23
|
"access": "public"
|
|
23
24
|
},
|
|
24
25
|
"peerDependencies": {
|
|
25
|
-
"clarusjs": "^0.
|
|
26
|
+
"clarusjs": "^0.21.0"
|
|
26
27
|
},
|
|
27
28
|
"engines": {
|
|
28
29
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
@@ -45,15 +46,14 @@
|
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"@types/node": "^26.6.11",
|
|
48
|
-
"clarusjs": "^0.
|
|
49
|
+
"clarusjs": "^0.21.0",
|
|
49
50
|
"happy-dom": "^21.0.4",
|
|
50
51
|
"typescript": "^7.0.2",
|
|
51
52
|
"vite": "^8.4.0",
|
|
52
53
|
"vitest": "^5.1.2"
|
|
53
54
|
},
|
|
54
55
|
"scripts": {
|
|
55
|
-
"types": "node scripts/types.mjs",
|
|
56
56
|
"test": "vitest run",
|
|
57
|
-
"typecheck": "
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/src/accordion-item.tscl
CHANGED
|
@@ -7,10 +7,16 @@
|
|
|
7
7
|
Yes.
|
|
8
8
|
</accordion-item>
|
|
9
9
|
--}}
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
const props = props<{
|
|
12
|
+
/** Items with the same group are exclusive: opening one closes the others. */
|
|
13
|
+
group?: string
|
|
14
|
+
/** Opens it. */
|
|
15
|
+
open?: boolean
|
|
16
|
+
}>(() => ({ group: '', open: false }))
|
|
17
|
+
</script>
|
|
12
18
|
|
|
13
|
-
<details [name]="group || false" [open]="open" data-part="accordion-item">
|
|
19
|
+
<details [name]="props.group || false" [open]="props.open" data-part="accordion-item">
|
|
14
20
|
<summary data-part="trigger"><slot name="trigger" /></summary>
|
|
15
21
|
<div data-part="panel"><slot /></div>
|
|
16
22
|
</details>
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Autocomplete (#680): a text field with suggestions. What's typed is the value (unlike the combobox, which makes you
|
|
3
|
+
pick one of its options); the suggestions that contain it are listed below, ↓ ↑ move through them, Enter or a click
|
|
4
|
+
takes one, Escape closes the list, Tab leaves with the text as it is. [inline] also completes in place: the first
|
|
5
|
+
suggestion that starts with what's typed fills in, the rest selected so typing on replaces it. WAI-ARIA combobox
|
|
6
|
+
with list autocomplete (aria-autocomplete "list", or "both" with [inline]).
|
|
7
|
+
|
|
8
|
+
<autocomplete id="city" label="City" name="city" [suggestions]="cities" [(value)]="address.city" />
|
|
9
|
+
|
|
10
|
+
For suggestions from a server, fetch them when value changes and pass the new list in.
|
|
11
|
+
--}}
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { effect } from 'clarusjs/runtime'
|
|
14
|
+
const { id, label, name = '', placeholder = '', suggestions = [], inline = false } = props<{
|
|
15
|
+
/** Its id, unique on the page: its input and list ids are made from it. */
|
|
16
|
+
id: string
|
|
17
|
+
/** The field's label. */
|
|
18
|
+
label: string
|
|
19
|
+
/** Sends the text with a form. */
|
|
20
|
+
name?: string
|
|
21
|
+
/** Shown while the field is empty: an example of what goes in it. */
|
|
22
|
+
placeholder?: string
|
|
23
|
+
/** What to suggest: those that contain what's typed are listed. */
|
|
24
|
+
suggestions?: readonly string[]
|
|
25
|
+
/** Complete in place with the first suggestion that starts with what's typed. */
|
|
26
|
+
inline?: boolean
|
|
27
|
+
}>()
|
|
28
|
+
/** The text in the field. */
|
|
29
|
+
let value = model<string>(() => '')
|
|
30
|
+
const initial = value
|
|
31
|
+
|
|
32
|
+
let query = state('') // what was typed, before any inline completion
|
|
33
|
+
let open = state(false)
|
|
34
|
+
let active = state(-1)
|
|
35
|
+
const matches = derived(() => {
|
|
36
|
+
const q = query.trim().toLowerCase()
|
|
37
|
+
return q ? suggestions.filter((s) => s.toLowerCase().includes(q) && s.toLowerCase() !== q) : []
|
|
38
|
+
})
|
|
39
|
+
const shown = derived(() => open && matches.length > 0)
|
|
40
|
+
|
|
41
|
+
function take(text: string, input: HTMLInputElement): void {
|
|
42
|
+
input.value = text
|
|
43
|
+
value = text
|
|
44
|
+
query = text
|
|
45
|
+
open = false
|
|
46
|
+
active = -1
|
|
47
|
+
}
|
|
48
|
+
function typed(event: Event): void {
|
|
49
|
+
const input = event.target as HTMLInputElement
|
|
50
|
+
query = input.value
|
|
51
|
+
active = -1
|
|
52
|
+
open = true
|
|
53
|
+
const inserting = (event as InputEvent).inputType?.startsWith('insert')
|
|
54
|
+
const first = inline && inserting && input.value ? suggestions.find((s) => s.toLowerCase().startsWith(input.value.toLowerCase())) : undefined
|
|
55
|
+
if (first && first.length > input.value.length) {
|
|
56
|
+
const start = input.value.length
|
|
57
|
+
input.value = input.value + first.slice(start) // the typed part as typed, the rest from the suggestion
|
|
58
|
+
input.setSelectionRange(start, input.value.length)
|
|
59
|
+
}
|
|
60
|
+
value = input.value
|
|
61
|
+
}
|
|
62
|
+
function key(event: KeyboardEvent): void {
|
|
63
|
+
const input = event.target as HTMLInputElement
|
|
64
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
65
|
+
if (!matches.length) return
|
|
66
|
+
event.preventDefault()
|
|
67
|
+
open = true
|
|
68
|
+
const n = matches.length
|
|
69
|
+
active = event.key === 'ArrowDown' ? (active + 1) % n : (active - 1 + n) % n
|
|
70
|
+
} else if (event.key === 'Enter' && shown && active >= 0) {
|
|
71
|
+
event.preventDefault()
|
|
72
|
+
take(matches[active]!, input)
|
|
73
|
+
} else if (event.key === 'Escape' && shown) {
|
|
74
|
+
event.preventDefault()
|
|
75
|
+
open = false
|
|
76
|
+
active = -1
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const close = () => { open = false; active = -1 }
|
|
80
|
+
function choose(event: MouseEvent, text: string): void {
|
|
81
|
+
event.preventDefault() // keep focus in the field
|
|
82
|
+
take(text, (event.currentTarget as Element).closest('[data-part="autocomplete"]')!.querySelector('input')!)
|
|
83
|
+
}
|
|
84
|
+
// The parent's new value into the field, only when it differs from what's there (typing isn't written over)
|
|
85
|
+
function wire(input: HTMLInputElement): () => void {
|
|
86
|
+
return effect(() => { if (input.value !== value) { input.value = value; query = value } })
|
|
87
|
+
}
|
|
88
|
+
</script>
|
|
89
|
+
|
|
90
|
+
<div data-part="autocomplete" [data-open]="shown ? '' : false">
|
|
91
|
+
<label [for]="id" data-part="label">{{ label }}</label>
|
|
92
|
+
<input
|
|
93
|
+
[id]="id"
|
|
94
|
+
type="text"
|
|
95
|
+
role="combobox"
|
|
96
|
+
autocomplete="off"
|
|
97
|
+
[aria-autocomplete]="inline ? 'both' : 'list'"
|
|
98
|
+
[aria-expanded]="String(shown)"
|
|
99
|
+
[aria-controls]="id + '-listbox'"
|
|
100
|
+
[aria-activedescendant]="shown && active >= 0 ? id + '-option-' + active : undefined"
|
|
101
|
+
data-part="control"
|
|
102
|
+
[name]="name || undefined"
|
|
103
|
+
[placeholder]="placeholder || undefined"
|
|
104
|
+
[value]="initial || undefined"
|
|
105
|
+
(input)="typed($event)"
|
|
106
|
+
(keydown)="key($event)"
|
|
107
|
+
(blur)="close()"
|
|
108
|
+
[use]="wire"
|
|
109
|
+
/>
|
|
110
|
+
<ul [id]="id + '-listbox'" role="listbox" [aria-label]="label" data-part="listbox" [hidden]="!shown">
|
|
111
|
+
@for (s of matches; track s; let i = $index) {
|
|
112
|
+
<li [id]="id + '-option-' + i" role="option" data-part="option" [data-highlighted]="i === active ? '' : false" (mousedown)="choose($event, s)">{{ s }}</li>
|
|
113
|
+
}
|
|
114
|
+
</ul>
|
|
115
|
+
</div>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The checkbox group's context (#674): its name and value, read by each <checkbox-item> as the server renders it.
|
|
2
|
+
import { contextKey, type ContextKey } from 'clarusjs/runtime'
|
|
3
|
+
|
|
4
|
+
/** The checkbox group's context: its name and checked values, for the checkboxes inside it. */
|
|
5
|
+
export const checkboxGroupKey: ContextKey<{ name: string; value: readonly string[] }> = contextKey('checkbox-group')
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Checkbox group (#674): native checkboxes in a fieldset, one name, their value one string array. Tab goes to each box
|
|
3
|
+
and Space toggles it (the browser's); with [name], a form sends each checked one. The group is a small island;
|
|
4
|
+
<checkbox-item>s have no script and read the group's name and value from context, so the server writes which are
|
|
5
|
+
checked. Bind the array with [(value)]; (change) comes first with the new array ($info.reason: 'toggle' or 'all'),
|
|
6
|
+
and $info.cancel() keeps the current one.
|
|
7
|
+
|
|
8
|
+
An "all" box: [all] is its label and [allValues] what it checks. It's checked when every one of those is, and mixed
|
|
9
|
+
(the native indeterminate state, which screen readers hear as "mixed"; data-indeterminate for styling) when some are.
|
|
10
|
+
|
|
11
|
+
<checkbox-group name="topping" label="Toppings" all="All toppings" [allValues]="['cheese', 'ham']" [(value)]="toppings">
|
|
12
|
+
<checkbox-item value="cheese" label="Cheese" />
|
|
13
|
+
<checkbox-item value="ham" label="Ham" />
|
|
14
|
+
</checkbox-group>
|
|
15
|
+
--}}
|
|
16
|
+
<script lang="ts">
|
|
17
|
+
import { effect } from 'clarusjs/runtime'
|
|
18
|
+
import { checkboxGroupKey } from './checkbox-group-key.ts'
|
|
19
|
+
const { name, label, all = '', allValues = [] } = props<{
|
|
20
|
+
/** The checkboxes' name, for forms. */
|
|
21
|
+
name: string
|
|
22
|
+
/** The group's label (its legend). */
|
|
23
|
+
label: string
|
|
24
|
+
/** The "all" box's label; none without it. */
|
|
25
|
+
all?: string
|
|
26
|
+
/** What the "all" box checks: every enabled item's value. */
|
|
27
|
+
allValues?: string[]
|
|
28
|
+
}>()
|
|
29
|
+
/** The checked items' values, in the order they were checked. */
|
|
30
|
+
let value = model<string[]>(() => [])
|
|
31
|
+
const emit = events<{ change: string[] }>()
|
|
32
|
+
provide(checkboxGroupKey, { name, value })
|
|
33
|
+
|
|
34
|
+
const every = derived(() => allValues.length > 0 && allValues.every((v) => value.includes(v)))
|
|
35
|
+
const some = derived(() => allValues.some((v) => value.includes(v)))
|
|
36
|
+
|
|
37
|
+
// The "all" box's native mixed state, from the moment it's there (it appears when [all] is set) and after each change
|
|
38
|
+
function mixed(box: HTMLInputElement): () => void {
|
|
39
|
+
return effect(() => { box.indeterminate = some && !every })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The items are the page's server HTML: kept in step with value here, and their changes go through (change).
|
|
43
|
+
function wire(el: HTMLFieldSetElement): () => void {
|
|
44
|
+
const own = (node: Element) => node.closest('[data-part="checkbox-group"]') === el
|
|
45
|
+
const allBox = () => el.querySelector<HTMLInputElement>(':scope > [data-all] > input')
|
|
46
|
+
const sync = (checked: readonly string[]) => {
|
|
47
|
+
for (const input of el.querySelectorAll<HTMLInputElement>('[data-part="checkbox"]:not([data-all]) > input[type="checkbox"]')) {
|
|
48
|
+
if (!own(input)) continue
|
|
49
|
+
input.checked = checked.includes(input.value)
|
|
50
|
+
input.parentElement?.toggleAttribute('data-checked', input.checked)
|
|
51
|
+
}
|
|
52
|
+
const box = allBox()
|
|
53
|
+
if (box) {
|
|
54
|
+
const on = allValues.length > 0 && allValues.every((v) => checked.includes(v))
|
|
55
|
+
box.checked = on
|
|
56
|
+
box.indeterminate = !on && allValues.some((v) => checked.includes(v))
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const change = (event: Event) => {
|
|
60
|
+
const input = event.target as HTMLInputElement
|
|
61
|
+
if (input.type !== 'checkbox' || !own(input)) return
|
|
62
|
+
const isAll = input === allBox()
|
|
63
|
+
const next = isAll
|
|
64
|
+
? (input.checked ? [...value, ...allValues.filter((v) => !value.includes(v))] : value.filter((v) => !allValues.includes(v)))
|
|
65
|
+
: (input.checked ? [...value, input.value] : value.filter((v) => v !== input.value))
|
|
66
|
+
if (emit('change', next, { reason: isAll ? 'all' : 'toggle', event })) value = next
|
|
67
|
+
else sync(value) // cancelled: back as it was
|
|
68
|
+
}
|
|
69
|
+
// A form reset checks the boxes it started with, without a change event: read them back.
|
|
70
|
+
const reset = (event: Event) => {
|
|
71
|
+
if (event.target !== el.form) return
|
|
72
|
+
setTimeout(() => {
|
|
73
|
+
value = [...el.querySelectorAll<HTMLInputElement>('[data-part="checkbox"]:not([data-all]) > input:checked')].filter(own).map((i) => i.value)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
el.addEventListener('change', change)
|
|
77
|
+
document.addEventListener('reset', reset)
|
|
78
|
+
const stop = effect(() => sync(value))
|
|
79
|
+
return () => {
|
|
80
|
+
el.removeEventListener('change', change)
|
|
81
|
+
document.removeEventListener('reset', reset)
|
|
82
|
+
stop()
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
</script>
|
|
86
|
+
|
|
87
|
+
<fieldset data-part="checkbox-group" [use]="wire">
|
|
88
|
+
<legend data-part="legend">{{ label }}</legend>
|
|
89
|
+
@if (all) {
|
|
90
|
+
<label data-part="checkbox" data-all="" [data-checked]="every ? '' : false" [data-indeterminate]="some && !every ? '' : false">
|
|
91
|
+
<input type="checkbox" [checked]="every" [use]="mixed" />
|
|
92
|
+
<span data-part="label">{{ all }}</span>
|
|
93
|
+
</label>
|
|
94
|
+
}
|
|
95
|
+
<slot />
|
|
96
|
+
</fieldset>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{{-- Checkbox item (#674): one native checkbox and its label, inside <checkbox-group>, which names it and says whether it's checked. --}}
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { checkboxGroupKey } from './checkbox-group-key.ts'
|
|
4
|
+
const { value, label, disabled = false } = props<{
|
|
5
|
+
/** What it adds to the group's value when checked. */
|
|
6
|
+
value: string
|
|
7
|
+
/** The checkbox's label. */
|
|
8
|
+
label: string
|
|
9
|
+
/** Can't be used or focused. */
|
|
10
|
+
disabled?: boolean
|
|
11
|
+
}>()
|
|
12
|
+
const group = context(checkboxGroupKey)
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<label data-part="checkbox" [data-checked]="group.value.includes(value) ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
16
|
+
<input type="checkbox" [name]="group.name" [value]="value" [checked]="group.value.includes(value)" [disabled]="disabled" />
|
|
17
|
+
<span data-part="label">{{ label }}</span>
|
|
18
|
+
</label>
|
package/src/checkbox.tscl
CHANGED
|
@@ -2,12 +2,20 @@
|
|
|
2
2
|
Checkbox (#465): a native checkbox inside its label. Bind it two ways: <checkbox label="I agree" [(checked)]="agreed" />.
|
|
3
3
|
data-checked and data-disabled on the label are for styling.
|
|
4
4
|
--}}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
<script lang="ts">
|
|
6
|
+
const props = props<{
|
|
7
|
+
/** The checkbox's label. */
|
|
8
|
+
label: string
|
|
9
|
+
/** Sends the value with a form, under this name. */
|
|
10
|
+
name?: string
|
|
11
|
+
/** Can't be used or focused. */
|
|
12
|
+
disabled?: boolean
|
|
13
|
+
}>(() => ({ name: '', disabled: false }))
|
|
14
|
+
/** Whether it's checked, bound two ways with [(checked)]. */
|
|
15
|
+
let checked = model<boolean>(() => false)
|
|
16
|
+
</script>
|
|
9
17
|
|
|
10
|
-
<label data-part="checkbox" [data-checked]="checked ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
11
|
-
<input type="checkbox" [name]="name || false" [disabled]="disabled" [(checked)]="checked" />
|
|
12
|
-
<span data-part="label">{{ label }}</span>
|
|
18
|
+
<label data-part="checkbox" [data-checked]="checked ? '' : false" [data-disabled]="props.disabled ? '' : false">
|
|
19
|
+
<input type="checkbox" [name]="props.name || false" [disabled]="props.disabled" [(checked)]="checked" />
|
|
20
|
+
<span data-part="label">{{ props.label }}</span>
|
|
13
21
|
</label>
|
package/src/chip-input.tscl
CHANGED
|
@@ -9,15 +9,24 @@
|
|
|
9
9
|
(change) first, with the value and whether it's now in; $info.reason is 'add' or 'remove', and $info.cancel() keeps it
|
|
10
10
|
from happening: <chip-input … (change)="if ($event.added && !valid($event.value)) $info.cancel()" />
|
|
11
11
|
--}}
|
|
12
|
-
@input id: string = 'chips'
|
|
13
|
-
@input label: string = 'Tags'
|
|
14
|
-
@input name: string = ''
|
|
15
|
-
@input placeholder: string = 'Add…'
|
|
16
|
-
@input hint: string = 'Enter or comma adds; Backspace removes the last.'
|
|
17
|
-
@model values: string[] = []
|
|
18
|
-
@event change: { value: string; added: boolean }
|
|
19
12
|
|
|
20
|
-
<script>
|
|
13
|
+
<script lang="ts">
|
|
14
|
+
const props = props<{
|
|
15
|
+
/** Its id, unique on the page: its parts' ids are made from it. */
|
|
16
|
+
id?: string
|
|
17
|
+
/** The field's label. */
|
|
18
|
+
label?: string
|
|
19
|
+
/** Sends each chip with a form, under this name. */
|
|
20
|
+
name?: string
|
|
21
|
+
/** Shown while the input is empty: an example of what goes in. */
|
|
22
|
+
placeholder?: string
|
|
23
|
+
/** Help text under the field, linked to the input (aria-describedby). */
|
|
24
|
+
hint?: string
|
|
25
|
+
}>(() => ({ id: 'chips', label: 'Tags', name: '', placeholder: 'Add…', hint: 'Enter or comma adds; Backspace removes the last.' }))
|
|
26
|
+
/** The chips' values, bound two ways with [(values)]. */
|
|
27
|
+
let values = model<string[]>(() => [])
|
|
28
|
+
const emit = events<{ change: { value: string; added: boolean } }>()
|
|
29
|
+
|
|
21
30
|
let text = state('')
|
|
22
31
|
|
|
23
32
|
function add(raw) {
|
|
@@ -49,7 +58,7 @@
|
|
|
49
58
|
</script>
|
|
50
59
|
|
|
51
60
|
<div data-part="chip-input">
|
|
52
|
-
<label [for]="id + '-input'" data-part="label">{{ label }}</label>
|
|
61
|
+
<label [for]="props.id + '-input'" data-part="label">{{ props.label }}</label>
|
|
53
62
|
<div data-part="control">
|
|
54
63
|
@for (v of values; track v) {
|
|
55
64
|
<span data-part="chip"
|
|
@@ -57,21 +66,21 @@
|
|
|
57
66
|
>
|
|
58
67
|
}
|
|
59
68
|
<input
|
|
60
|
-
[id]="id + '-input'"
|
|
69
|
+
[id]="props.id + '-input'"
|
|
61
70
|
data-part="input"
|
|
62
71
|
autocomplete="off"
|
|
63
|
-
[aria-describedby]="id + '-hint'"
|
|
64
|
-
[placeholder]="placeholder"
|
|
72
|
+
[aria-describedby]="props.id + '-hint'"
|
|
73
|
+
[placeholder]="props.placeholder"
|
|
65
74
|
[value]="text"
|
|
66
75
|
(input)="type($event.target.value)"
|
|
67
76
|
(keydown)="key($event)"
|
|
68
77
|
(blur)="add(text)"
|
|
69
78
|
/>
|
|
70
79
|
</div>
|
|
71
|
-
<p [id]="id + '-hint'" data-part="hint">{{ hint }}</p>
|
|
72
|
-
@if (name) {
|
|
80
|
+
<p [id]="props.id + '-hint'" data-part="hint">{{ props.hint }}</p>
|
|
81
|
+
@if (props.name) {
|
|
73
82
|
@for (v of values; track v) {
|
|
74
|
-
<input type="hidden" [name]="name" [value]="v" />
|
|
83
|
+
<input type="hidden" [name]="props.name" [value]="v" />
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
86
|
</div>
|
package/src/combobox.tscl
CHANGED
|
@@ -23,10 +23,24 @@
|
|
|
23
23
|
<combobox … (change)="if (tooMany && $event.selected) $info.cancel()" />
|
|
24
24
|
Give each one its own id: its listbox and options are named after it.
|
|
25
25
|
--}}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
|
|
27
|
+
<script lang="ts">
|
|
28
|
+
import { json, resource } from 'clarusjs/fetch'
|
|
29
|
+
type Option = { value: string; label: string }
|
|
30
|
+
const props = props<{
|
|
31
|
+
/** Its id, unique on the page: its parts' ids are made from it. */
|
|
32
|
+
id?: string
|
|
33
|
+
/** The field's label. */
|
|
34
|
+
label?: string
|
|
35
|
+
/** What can be picked: each has a value and a label shown to the user. */
|
|
36
|
+
options?: Option[]
|
|
37
|
+
/** Allows several picks, kept as chips (bind [(values)] instead of [(value)]). */
|
|
38
|
+
multiple?: boolean
|
|
39
|
+
/** Shown while nothing is picked or typed: an example of what goes in. */
|
|
40
|
+
placeholder?: string
|
|
41
|
+
/** A URL asked for options as `src?q=<search>` (JSON list of { value, label }) instead of options. */
|
|
42
|
+
src?: string
|
|
43
|
+
}>(() => ({ id: 'combobox', label: 'Fruit', options: [
|
|
30
44
|
{ value: 'apple', label: 'Apple' },
|
|
31
45
|
{ value: 'apricot', label: 'Apricot' },
|
|
32
46
|
{ value: 'banana', label: 'Banana' },
|
|
@@ -35,17 +49,12 @@
|
|
|
35
49
|
{ value: 'mango', label: 'Mango' },
|
|
36
50
|
{ value: 'orange', label: 'Orange' },
|
|
37
51
|
{ value: 'pear', label: 'Pear' },
|
|
38
|
-
]
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@event change: { value: string; label: string; selected: boolean }
|
|
45
|
-
@event search: { term: string; signal: AbortSignal; respond: (options: Option[] | Promise<Option[]>) => void }
|
|
46
|
-
|
|
47
|
-
<script>
|
|
48
|
-
import { json, resource } from 'clarusjs/fetch'
|
|
52
|
+
], multiple: false, placeholder: 'Search…', src: '' }))
|
|
53
|
+
/** The picked option's value, bound two ways with [(value)] (single mode). */
|
|
54
|
+
let value = model<string>(() => '')
|
|
55
|
+
/** The picked options' values, bound two ways with [(values)] (with multiple). */
|
|
56
|
+
let values = model<string[]>(() => [])
|
|
57
|
+
const emit = events<{ change: { value: string; label: string; selected: boolean }; search: { term: string; signal: AbortSignal; respond: (options: Option[] | Promise<Option[]>) => void } }>()
|
|
49
58
|
let query = state('')
|
|
50
59
|
let term = state('') // the search sent to src: the query, 200 ms after the last key
|
|
51
60
|
let known = state({}) // labels of remote options picked, by value (the next search may not list them)
|
|
@@ -53,12 +62,12 @@
|
|
|
53
62
|
let open = state(false)
|
|
54
63
|
let active = state(0)
|
|
55
64
|
// What's chosen, as a list in both modes.
|
|
56
|
-
const chosen = derived(() => (multiple ? values : value ? [value] : []))
|
|
65
|
+
const chosen = derived(() => (props.multiple ? values : value ? [value] : []))
|
|
57
66
|
// Answered by a (search) listener (known once one responds) or by src: the answer for the term, cached by it;
|
|
58
67
|
// nothing is asked until the list opens.
|
|
59
68
|
// A plain flag, not state: the fetcher runs while the options are read, where state can't be written.
|
|
60
69
|
let answered = false
|
|
61
|
-
const remotely = () => answered || !!src
|
|
70
|
+
const remotely = () => answered || !!props.src
|
|
62
71
|
const remote = resource(
|
|
63
72
|
({ signal }) => {
|
|
64
73
|
if (!open) return false
|
|
@@ -68,18 +77,18 @@
|
|
|
68
77
|
answered = true
|
|
69
78
|
return Promise.resolve(answer)
|
|
70
79
|
}
|
|
71
|
-
return src ? json(src + (src.includes('?') ? '&' : '?') + 'q=' + encodeURIComponent(term), { signal }) : false
|
|
80
|
+
return props.src ? json(props.src + (props.src.includes('?') ? '&' : '?') + 'q=' + encodeURIComponent(term), { signal }) : false
|
|
72
81
|
},
|
|
73
|
-
{ key: () => (open ? ['combobox', src || '#' + id, term] : false) },
|
|
82
|
+
{ key: () => (open ? ['combobox', props.src || '#' + props.id, term] : false) },
|
|
74
83
|
)
|
|
75
84
|
// The options matching what's typed (by label, ignoring case), or the server's.
|
|
76
85
|
const shown = derived(() => {
|
|
77
86
|
const answer = remote.value // read first, always: the first (search) answer arrives after this runs
|
|
78
87
|
if (remotely()) return answer ?? []
|
|
79
88
|
const q = query.trim().toLowerCase()
|
|
80
|
-
return q ? options.filter((o) => o.label.toLowerCase().includes(q)) : options
|
|
89
|
+
return q ? props.options.filter((o) => o.label.toLowerCase().includes(q)) : props.options
|
|
81
90
|
})
|
|
82
|
-
const labelOf = (v) => known[v] ?? options.find((o) => o.value === v)?.label ?? v
|
|
91
|
+
const labelOf = (v) => known[v] ?? props.options.find((o) => o.value === v)?.label ?? v
|
|
83
92
|
const status = derived(() => {
|
|
84
93
|
if (!open) return ''
|
|
85
94
|
const { loading, error } = remote
|
|
@@ -96,7 +105,7 @@
|
|
|
96
105
|
)
|
|
97
106
|
}
|
|
98
107
|
function pick(option, event) {
|
|
99
|
-
const selected = !(multiple && chosen.includes(option.value))
|
|
108
|
+
const selected = !(props.multiple && chosen.includes(option.value))
|
|
100
109
|
if (
|
|
101
110
|
!emit(
|
|
102
111
|
'change',
|
|
@@ -106,7 +115,7 @@
|
|
|
106
115
|
)
|
|
107
116
|
return
|
|
108
117
|
if (remotely()) known = { ...known, [option.value]: option.label }
|
|
109
|
-
if (multiple) {
|
|
118
|
+
if (props.multiple) {
|
|
110
119
|
values = chosen.includes(option.value) ? chosen.filter((v) => v !== option.value) : [...chosen, option.value]
|
|
111
120
|
} else {
|
|
112
121
|
value = option.value
|
|
@@ -142,7 +151,7 @@
|
|
|
142
151
|
event.preventDefault()
|
|
143
152
|
open = false
|
|
144
153
|
query = '' // back to what's chosen
|
|
145
|
-
} else if (event.key === 'Backspace' && multiple && !query && chosen.length) {
|
|
154
|
+
} else if (event.key === 'Backspace' && props.multiple && !query && chosen.length) {
|
|
146
155
|
remove(chosen[chosen.length - 1], event)
|
|
147
156
|
}
|
|
148
157
|
}
|
|
@@ -156,9 +165,9 @@
|
|
|
156
165
|
</script>
|
|
157
166
|
|
|
158
167
|
<div data-part="combobox" (focusout)="leave($event)">
|
|
159
|
-
<label [for]="id + '-input'" data-part="label">{{ label }}</label>
|
|
168
|
+
<label [for]="props.id + '-input'" data-part="label">{{ props.label }}</label>
|
|
160
169
|
<div data-part="control">
|
|
161
|
-
@if (multiple) {
|
|
170
|
+
@if (props.multiple) {
|
|
162
171
|
@for (v of chosen; track v) {
|
|
163
172
|
<span data-part="chip"
|
|
164
173
|
>{{ labelOf(v) }}
|
|
@@ -167,33 +176,33 @@
|
|
|
167
176
|
}
|
|
168
177
|
}
|
|
169
178
|
<input
|
|
170
|
-
[id]="id + '-input'"
|
|
179
|
+
[id]="props.id + '-input'"
|
|
171
180
|
role="combobox"
|
|
172
181
|
autocomplete="off"
|
|
173
182
|
data-part="input"
|
|
174
183
|
aria-autocomplete="list"
|
|
175
184
|
[aria-expanded]="open ? 'true' : 'false'"
|
|
176
|
-
[aria-controls]="id + '-list'"
|
|
177
|
-
[aria-activedescendant]="open && shown[active] ? id + '-' + shown[active].value : false"
|
|
178
|
-
[placeholder]="multiple || !chosen.length ? placeholder : false"
|
|
179
|
-
[value]="open || multiple ? query : labelOf(value)"
|
|
185
|
+
[aria-controls]="props.id + '-list'"
|
|
186
|
+
[aria-activedescendant]="open && shown[active] ? props.id + '-' + shown[active].value : false"
|
|
187
|
+
[placeholder]="props.multiple || !chosen.length ? props.placeholder : false"
|
|
188
|
+
[value]="open || props.multiple ? query : labelOf(value)"
|
|
180
189
|
(input)="type($event.target.value)"
|
|
181
190
|
(click)="show()"
|
|
182
191
|
(keydown)="key($event)"
|
|
183
192
|
/>
|
|
184
193
|
</div>
|
|
185
194
|
<ul
|
|
186
|
-
[id]="id + '-list'"
|
|
195
|
+
[id]="props.id + '-list'"
|
|
187
196
|
role="listbox"
|
|
188
|
-
[aria-label]="label"
|
|
189
|
-
[aria-multiselectable]="multiple ? 'true' : false"
|
|
197
|
+
[aria-label]="props.label"
|
|
198
|
+
[aria-multiselectable]="props.multiple ? 'true' : false"
|
|
190
199
|
[hidden]="!open"
|
|
191
200
|
(mousedown)="$event.preventDefault()"
|
|
192
201
|
data-part="listbox"
|
|
193
202
|
>
|
|
194
203
|
@for (option of shown; track option.value) {
|
|
195
204
|
<li
|
|
196
|
-
[id]="id + '-' + option.value"
|
|
205
|
+
[id]="props.id + '-' + option.value"
|
|
197
206
|
role="option"
|
|
198
207
|
[aria-selected]="chosen.includes(option.value) ? 'true' : 'false'"
|
|
199
208
|
[data-highlighted]="$index === active ? '' : false"
|