@speles7172/controls 0.1.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/LICENSE +21 -0
- package/README.md +119 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/SearchableMultiSelect.d.ts +48 -0
- package/dist/SearchableMultiSelect.d.ts.map +1 -0
- package/dist/SearchableMultiSelect.js +183 -0
- package/dist/SearchableMultiSelect.js.map +1 -0
- package/dist/SearchableSelect.d.ts +19 -0
- package/dist/SearchableSelect.d.ts.map +1 -0
- package/dist/SearchableSelect.js +17 -0
- package/dist/SearchableSelect.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +369 -0
- package/dist/types.d.ts +43 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/useOptionSelect.d.ts +126 -0
- package/dist/useOptionSelect.d.ts.map +1 -0
- package/dist/useOptionSelect.js +299 -0
- package/dist/useOptionSelect.js.map +1 -0
- package/package.json +50 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@speles7172/controls` — the form controls every Peles.us frontend was
|
|
3
|
+
* re-implementing.
|
|
4
|
+
*
|
|
5
|
+
* One package that grows: a control that turns out to be needed in two places
|
|
6
|
+
* belongs here rather than being copied into the second one. Today that is a
|
|
7
|
+
* searchable multi-select and its single-select sibling.
|
|
8
|
+
*
|
|
9
|
+
* Each control takes either a fixed array of options or an
|
|
10
|
+
* {@link OptionTransport}, and behaves the same either way — the difference
|
|
11
|
+
* between "filter these eight statuses" and "search forty thousand vendors" is
|
|
12
|
+
* one prop. The backend half of that transport is
|
|
13
|
+
* `@speles7172/controls-client`.
|
|
14
|
+
*
|
|
15
|
+
* Browser-only. Everything it takes from `controls-client` is either a type or
|
|
16
|
+
* comes from that package's dependency-free `/core` entry — see
|
|
17
|
+
* `boundary.test.ts`, which fails the build if that stops being true.
|
|
18
|
+
*
|
|
19
|
+
* ```tsx
|
|
20
|
+
* import { SearchableMultiSelect } from '@speles7172/controls';
|
|
21
|
+
* import '@speles7172/controls/styles.css';
|
|
22
|
+
*
|
|
23
|
+
* <SearchableMultiSelect
|
|
24
|
+
* label="Vendor"
|
|
25
|
+
* transport={{
|
|
26
|
+
* search: (query) => fetch(`/api/options/vendors?${new URLSearchParams(query as never)}`).then((r) => r.json()),
|
|
27
|
+
* resolve: (values) => fetch(`/api/options/vendors/resolve?values=${values.join(',')}`).then((r) => r.json()).then((r) => r.options),
|
|
28
|
+
* }}
|
|
29
|
+
* value={vendorIds}
|
|
30
|
+
* onChange={setVendorIds}
|
|
31
|
+
* />
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export { SearchableMultiSelect, } from './SearchableMultiSelect.js';
|
|
35
|
+
export { SearchableSelect } from './SearchableSelect.js';
|
|
36
|
+
export { useOptionSelect, } from './useOptionSelect.js';
|
|
37
|
+
/**
|
|
38
|
+
* Re-exported from `@speles7172/controls-client/core` so a frontend that only
|
|
39
|
+
* renders controls does not need a second dependency to type its options or to
|
|
40
|
+
* filter a static list.
|
|
41
|
+
*/
|
|
42
|
+
export { groupOptions, highlightParts, matchOptions, matchRanges, scoreOption, } from '@speles7172/controls-client/core';
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,EACL,qBAAqB,GAEtB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,gBAAgB,EAA8B,MAAM,uBAAuB,CAAC;AAErF,OAAO,EACL,eAAe,GAGhB,MAAM,sBAAsB,CAAC;AAE9B;;;;GAIG;AACH,OAAO,EACL,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,WAAW,EACX,WAAW,GACZ,MAAM,kCAAkC,CAAC"}
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Default styling for @speles7172/controls.
|
|
3
|
+
*
|
|
4
|
+
* Entirely optional — the controls render semantic HTML with `ctl-` class names
|
|
5
|
+
* and no styling dependency, so an application with its own design system can
|
|
6
|
+
* skip this file and target the same classes.
|
|
7
|
+
*
|
|
8
|
+
* Everything is driven by the custom properties below. Override them on `.ctl`
|
|
9
|
+
* to re-theme without touching a rule — which is how `@speles7172/audit-console`
|
|
10
|
+
* makes the control look native to the console it sits in.
|
|
11
|
+
*
|
|
12
|
+
* The theme selectors are wrapped in `:where()` so they add no specificity: a
|
|
13
|
+
* bare `.ctl[data-ctl-theme="dark"]` is (0,2,0) and would outrank a host's own
|
|
14
|
+
* `.my-app .ctl { --ctl-accent: … }`, meaning the control ignored the palette it
|
|
15
|
+
* was handed. Same rule `log-console`'s stylesheet follows, and its
|
|
16
|
+
* `styles.test.ts` is what named it.
|
|
17
|
+
*
|
|
18
|
+
* Light is the default because a control is embedded in someone else's page,
|
|
19
|
+
* and the host decides what that page looks like. `theme="dark"` picks the dark
|
|
20
|
+
* palette outright; `theme="auto"` defers to `prefers-color-scheme`, which is
|
|
21
|
+
* right only when the host does the same.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
.ctl {
|
|
25
|
+
--ctl-bg: #ffffff;
|
|
26
|
+
--ctl-surface: #f8fafc;
|
|
27
|
+
--ctl-border: #e2e8f0;
|
|
28
|
+
--ctl-text: #0f172a;
|
|
29
|
+
--ctl-muted: #64748b;
|
|
30
|
+
--ctl-accent: #2563eb;
|
|
31
|
+
--ctl-accent-soft: #eff6ff;
|
|
32
|
+
--ctl-mark-bg: #fef08a;
|
|
33
|
+
--ctl-mark-text: #422006;
|
|
34
|
+
--ctl-error-text: #b91c1c;
|
|
35
|
+
--ctl-radius: 8px;
|
|
36
|
+
--ctl-shadow: 0 8px 24px rgb(15 23 42 / 12%);
|
|
37
|
+
|
|
38
|
+
color: var(--ctl-text);
|
|
39
|
+
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
40
|
+
font-size: 14px;
|
|
41
|
+
position: relative;
|
|
42
|
+
display: flex;
|
|
43
|
+
flex-direction: column;
|
|
44
|
+
gap: 4px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.ctl:where([data-ctl-theme="dark"]) {
|
|
48
|
+
--ctl-bg: #0f172a;
|
|
49
|
+
--ctl-surface: #1e293b;
|
|
50
|
+
--ctl-border: #334155;
|
|
51
|
+
--ctl-text: #e2e8f0;
|
|
52
|
+
--ctl-muted: #94a3b8;
|
|
53
|
+
--ctl-accent: #60a5fa;
|
|
54
|
+
--ctl-accent-soft: #1e3a5f;
|
|
55
|
+
--ctl-mark-bg: #854d0e;
|
|
56
|
+
--ctl-mark-text: #fef9c3;
|
|
57
|
+
--ctl-error-text: #fca5a5;
|
|
58
|
+
--ctl-shadow: 0 8px 24px rgb(0 0 0 / 45%);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@media (prefers-color-scheme: dark) {
|
|
62
|
+
.ctl:where([data-ctl-theme="auto"]) {
|
|
63
|
+
--ctl-bg: #0f172a;
|
|
64
|
+
--ctl-surface: #1e293b;
|
|
65
|
+
--ctl-border: #334155;
|
|
66
|
+
--ctl-text: #e2e8f0;
|
|
67
|
+
--ctl-muted: #94a3b8;
|
|
68
|
+
--ctl-accent: #60a5fa;
|
|
69
|
+
--ctl-accent-soft: #1e3a5f;
|
|
70
|
+
--ctl-mark-bg: #854d0e;
|
|
71
|
+
--ctl-mark-text: #fef9c3;
|
|
72
|
+
--ctl-error-text: #fca5a5;
|
|
73
|
+
--ctl-shadow: 0 8px 24px rgb(0 0 0 / 45%);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.ctl *,
|
|
78
|
+
.ctl *::before,
|
|
79
|
+
.ctl *::after {
|
|
80
|
+
box-sizing: border-box;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
* The block modifier for a select-shaped control. A sensible minimum width
|
|
85
|
+
* belongs here rather than on every caller: the trigger holds a summary like
|
|
86
|
+
* "3 selected", and a filter bar that lays its fields out with `flex` will
|
|
87
|
+
* otherwise squash it to the width of the word "All".
|
|
88
|
+
*/
|
|
89
|
+
.ctl-select {
|
|
90
|
+
min-width: 160px;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.ctl-label {
|
|
94
|
+
font-size: 12px;
|
|
95
|
+
font-weight: 600;
|
|
96
|
+
color: var(--ctl-muted);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.ctl-muted {
|
|
100
|
+
color: var(--ctl-muted);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* The trigger reads as the selection. */
|
|
104
|
+
.ctl-trigger {
|
|
105
|
+
display: flex;
|
|
106
|
+
align-items: center;
|
|
107
|
+
justify-content: space-between;
|
|
108
|
+
gap: 8px;
|
|
109
|
+
width: 100%;
|
|
110
|
+
min-height: 34px;
|
|
111
|
+
padding: 6px 10px;
|
|
112
|
+
font: inherit;
|
|
113
|
+
color: inherit;
|
|
114
|
+
text-align: left;
|
|
115
|
+
background: var(--ctl-bg);
|
|
116
|
+
border: 1px solid var(--ctl-border);
|
|
117
|
+
border-radius: var(--ctl-radius);
|
|
118
|
+
cursor: pointer;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.ctl-trigger:hover:not(:disabled) {
|
|
122
|
+
border-color: var(--ctl-accent);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.ctl-trigger:focus-visible {
|
|
126
|
+
outline: 2px solid var(--ctl-accent);
|
|
127
|
+
outline-offset: 1px;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.ctl-trigger:disabled {
|
|
131
|
+
cursor: not-allowed;
|
|
132
|
+
opacity: 55%;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.ctl-value {
|
|
136
|
+
overflow: hidden;
|
|
137
|
+
text-overflow: ellipsis;
|
|
138
|
+
white-space: nowrap;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.ctl-trigger-icons {
|
|
142
|
+
display: flex;
|
|
143
|
+
flex-shrink: 0;
|
|
144
|
+
gap: 2px;
|
|
145
|
+
align-items: center;
|
|
146
|
+
color: var(--ctl-muted);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.ctl-icon-button {
|
|
150
|
+
padding: 0 3px;
|
|
151
|
+
font-size: 12px;
|
|
152
|
+
line-height: 1;
|
|
153
|
+
cursor: pointer;
|
|
154
|
+
border-radius: 4px;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.ctl-icon-button:hover {
|
|
158
|
+
color: var(--ctl-text);
|
|
159
|
+
background: var(--ctl-surface);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.ctl-caret {
|
|
163
|
+
font-size: 11px;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/* Selected values, so a multi-select says what is in it. */
|
|
167
|
+
.ctl-chips {
|
|
168
|
+
display: flex;
|
|
169
|
+
flex-wrap: wrap;
|
|
170
|
+
gap: 4px;
|
|
171
|
+
padding: 0;
|
|
172
|
+
margin: 0;
|
|
173
|
+
list-style: none;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.ctl-chip {
|
|
177
|
+
display: inline-flex;
|
|
178
|
+
gap: 4px;
|
|
179
|
+
align-items: center;
|
|
180
|
+
max-width: 100%;
|
|
181
|
+
padding: 1px 4px 1px 8px;
|
|
182
|
+
font-size: 12px;
|
|
183
|
+
color: var(--ctl-accent);
|
|
184
|
+
background: var(--ctl-accent-soft);
|
|
185
|
+
border-radius: 999px;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.ctl-chip-label {
|
|
189
|
+
overflow: hidden;
|
|
190
|
+
text-overflow: ellipsis;
|
|
191
|
+
white-space: nowrap;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.ctl-chip-remove {
|
|
195
|
+
padding: 0 2px;
|
|
196
|
+
font: inherit;
|
|
197
|
+
font-size: 10px;
|
|
198
|
+
line-height: 1;
|
|
199
|
+
color: inherit;
|
|
200
|
+
cursor: pointer;
|
|
201
|
+
background: none;
|
|
202
|
+
border: none;
|
|
203
|
+
border-radius: 999px;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
.ctl-chip-remove:hover:not(:disabled) {
|
|
207
|
+
background: rgb(0 0 0 / 10%);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/*
|
|
211
|
+
* The popover.
|
|
212
|
+
*
|
|
213
|
+
* Absolutely positioned rather than portalled, which keeps the control a single
|
|
214
|
+
* self-contained element with no z-index registry to coordinate — at the cost
|
|
215
|
+
* of being clipped by an ancestor with `overflow: hidden`. If a host has one,
|
|
216
|
+
* the fix is on that ancestor, not here.
|
|
217
|
+
*
|
|
218
|
+
* This is not theoretical: `sql-console`'s `.sqlc-panel` clipped the database
|
|
219
|
+
* picker's list partway down and made it unusable, and no jsdom test could see
|
|
220
|
+
* it. A host putting this control inside a panel it clips has to stop clipping.
|
|
221
|
+
*/
|
|
222
|
+
.ctl-popover {
|
|
223
|
+
position: absolute;
|
|
224
|
+
top: 100%;
|
|
225
|
+
z-index: 30;
|
|
226
|
+
display: flex;
|
|
227
|
+
flex-direction: column;
|
|
228
|
+
width: 100%;
|
|
229
|
+
min-width: 220px;
|
|
230
|
+
margin-top: 4px;
|
|
231
|
+
background: var(--ctl-bg);
|
|
232
|
+
border: 1px solid var(--ctl-border);
|
|
233
|
+
border-radius: var(--ctl-radius);
|
|
234
|
+
box-shadow: var(--ctl-shadow);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
.ctl-search {
|
|
238
|
+
padding: 6px;
|
|
239
|
+
border-bottom: 1px solid var(--ctl-border);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
.ctl-search-input {
|
|
243
|
+
width: 100%;
|
|
244
|
+
padding: 5px 8px;
|
|
245
|
+
font: inherit;
|
|
246
|
+
color: inherit;
|
|
247
|
+
background: var(--ctl-bg);
|
|
248
|
+
border: 1px solid var(--ctl-border);
|
|
249
|
+
border-radius: 6px;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.ctl-search-input:focus-visible {
|
|
253
|
+
outline: 2px solid var(--ctl-accent);
|
|
254
|
+
outline-offset: -1px;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.ctl-list,
|
|
258
|
+
.ctl-group-list {
|
|
259
|
+
padding: 4px;
|
|
260
|
+
margin: 0;
|
|
261
|
+
list-style: none;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.ctl-list {
|
|
265
|
+
max-height: 260px;
|
|
266
|
+
overflow-y: auto;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.ctl-group-list {
|
|
270
|
+
padding: 0;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
.ctl-group-label {
|
|
274
|
+
padding: 6px 8px 2px;
|
|
275
|
+
margin: 0;
|
|
276
|
+
font-size: 11px;
|
|
277
|
+
font-weight: 600;
|
|
278
|
+
text-transform: uppercase;
|
|
279
|
+
letter-spacing: 0.04em;
|
|
280
|
+
color: var(--ctl-muted);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.ctl-option {
|
|
284
|
+
display: flex;
|
|
285
|
+
gap: 8px;
|
|
286
|
+
align-items: flex-start;
|
|
287
|
+
padding: 5px 8px;
|
|
288
|
+
cursor: pointer;
|
|
289
|
+
border-radius: 6px;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/*
|
|
293
|
+
* Hover is not styled: the active option follows the pointer, so styling both
|
|
294
|
+
* would light up two rows whenever the mouse sits over one and the keyboard is
|
|
295
|
+
* on another.
|
|
296
|
+
*/
|
|
297
|
+
.ctl-option-active {
|
|
298
|
+
background: var(--ctl-surface);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
.ctl-option-selected {
|
|
302
|
+
color: var(--ctl-accent);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
.ctl-option-disabled {
|
|
306
|
+
cursor: not-allowed;
|
|
307
|
+
opacity: 50%;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
.ctl-check {
|
|
311
|
+
flex-shrink: 0;
|
|
312
|
+
line-height: 1.4;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
.ctl-option-text {
|
|
316
|
+
display: flex;
|
|
317
|
+
flex-direction: column;
|
|
318
|
+
min-width: 0;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
.ctl-option-label,
|
|
322
|
+
.ctl-option-description {
|
|
323
|
+
overflow: hidden;
|
|
324
|
+
text-overflow: ellipsis;
|
|
325
|
+
white-space: nowrap;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
.ctl-option-description {
|
|
329
|
+
font-size: 12px;
|
|
330
|
+
color: var(--ctl-muted);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
.ctl-mark {
|
|
334
|
+
color: var(--ctl-mark-text);
|
|
335
|
+
background: var(--ctl-mark-bg);
|
|
336
|
+
border-radius: 2px;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
.ctl-note {
|
|
340
|
+
padding: 10px;
|
|
341
|
+
margin: 0;
|
|
342
|
+
font-size: 13px;
|
|
343
|
+
color: var(--ctl-muted);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
.ctl-error {
|
|
347
|
+
color: var(--ctl-error-text);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
.ctl-popover-footer:not(:empty) {
|
|
351
|
+
display: flex;
|
|
352
|
+
gap: 10px;
|
|
353
|
+
padding: 6px 8px;
|
|
354
|
+
border-top: 1px solid var(--ctl-border);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.ctl-link {
|
|
358
|
+
padding: 0;
|
|
359
|
+
font: inherit;
|
|
360
|
+
font-size: 12px;
|
|
361
|
+
color: var(--ctl-accent);
|
|
362
|
+
cursor: pointer;
|
|
363
|
+
background: none;
|
|
364
|
+
border: none;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
.ctl-link:hover {
|
|
368
|
+
text-decoration: underline;
|
|
369
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Option, OptionPage, OptionQuery } from '@speles7172/controls-client/core';
|
|
2
|
+
export type { Option, OptionPage, OptionQuery };
|
|
3
|
+
/**
|
|
4
|
+
* How a control reaches an option source.
|
|
5
|
+
*
|
|
6
|
+
* A browser cannot query Postgres — it has no credentials, and any it were
|
|
7
|
+
* given would be readable by everyone the page is served to. So the control
|
|
8
|
+
* queries nothing itself: the consuming application exposes an endpoint backed
|
|
9
|
+
* by `@speles7172/controls-client`, and supplies this.
|
|
10
|
+
*
|
|
11
|
+
* That endpoint is the security seam. Note what crosses it: an
|
|
12
|
+
* {@link OptionQuery}, which names no table, no column and no ordering — the
|
|
13
|
+
* source on the far side decides all of those. A control cannot be talked into
|
|
14
|
+
* reading something else.
|
|
15
|
+
*/
|
|
16
|
+
export interface OptionTransport {
|
|
17
|
+
/** One page of options matching what has been typed. */
|
|
18
|
+
search(query: OptionQuery): Promise<OptionPage>;
|
|
19
|
+
/**
|
|
20
|
+
* The options behind values that are already selected.
|
|
21
|
+
*
|
|
22
|
+
* The difference between a control that shows "Acme Corp, Beta Ltd" when a
|
|
23
|
+
* form loads and one that shows two raw uuids. Worth implementing for any
|
|
24
|
+
* source whose options do not all fit in one page — without it, a selected
|
|
25
|
+
* value that is not in the current page has no label to render.
|
|
26
|
+
*
|
|
27
|
+
* Omit it and the control falls back to displaying the value itself, which
|
|
28
|
+
* is right for a small list where every option is loaded anyway.
|
|
29
|
+
*/
|
|
30
|
+
resolve?(values: string[]): Promise<Option[]>;
|
|
31
|
+
}
|
|
32
|
+
/** What a control is doing, for the parts of the UI that say so. */
|
|
33
|
+
export type OptionSelectStatus = 'idle' | 'loading' | 'ready' | 'error';
|
|
34
|
+
/**
|
|
35
|
+
* Which palette the default stylesheet uses.
|
|
36
|
+
*
|
|
37
|
+
* Defaults to `light` for the same reason the consoles do: a control is
|
|
38
|
+
* embedded in someone else's page, and the host decides what that page looks
|
|
39
|
+
* like — not the operating system. Pass `auto` to follow
|
|
40
|
+
* `prefers-color-scheme`, which is right only when the host follows it too.
|
|
41
|
+
*/
|
|
42
|
+
export type ControlTheme = 'light' | 'dark' | 'auto';
|
|
43
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,UAAU,EACV,WAAW,EACZ,MAAM,kCAAkC,CAAC;AAE1C,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC;AAEhD;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,eAAe;IAC9B,wDAAwD;IACxD,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAChD;;;;;;;;;;OAUG;IACH,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CAC/C;AAED,oEAAoE;AACpE,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAC;AAExE;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Option, OptionSelectStatus, OptionTransport } from './types.js';
|
|
2
|
+
export interface UseOptionSelectOptions {
|
|
3
|
+
/**
|
|
4
|
+
* A fixed list of options.
|
|
5
|
+
*
|
|
6
|
+
* The common case, and the one people forget is allowed: a status, a country,
|
|
7
|
+
* a set of tags. Putting a network request between a keystroke and a list of
|
|
8
|
+
* eight things is absurd, so a static list is filtered and ranked in the
|
|
9
|
+
* browser by the same rules the SQL side uses.
|
|
10
|
+
*
|
|
11
|
+
* Pass this or {@link transport}. Passing both uses the transport and treats
|
|
12
|
+
* the static list as options already known — which is how a caller seeds the
|
|
13
|
+
* labels of the current selection without a `resolve` round trip.
|
|
14
|
+
*/
|
|
15
|
+
options?: readonly Option[];
|
|
16
|
+
/** An endpoint to search. See {@link OptionTransport}. */
|
|
17
|
+
transport?: OptionTransport;
|
|
18
|
+
/** The selected values. Controlled — the control never holds its own. */
|
|
19
|
+
value: readonly string[];
|
|
20
|
+
onChange(values: string[]): void;
|
|
21
|
+
/** One value at a time. Default false. */
|
|
22
|
+
single?: boolean;
|
|
23
|
+
/** How many options a page holds, and how many "Show more" adds. */
|
|
24
|
+
pageSize?: number;
|
|
25
|
+
/**
|
|
26
|
+
* How long to wait after a keystroke before searching.
|
|
27
|
+
*
|
|
28
|
+
* Only applies to a transport; a static list filters instantly, because there
|
|
29
|
+
* is nothing to wait for. Default 200ms — long enough that typing "vendor"
|
|
30
|
+
* is one request rather than six, short enough not to feel laggy.
|
|
31
|
+
*/
|
|
32
|
+
debounceMs?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Search nothing until this many characters have been typed.
|
|
35
|
+
*
|
|
36
|
+
* For a source too large to have a meaningful first page — every contact in
|
|
37
|
+
* the system, say. Default 0, which loads the first page as soon as the
|
|
38
|
+
* control opens.
|
|
39
|
+
*/
|
|
40
|
+
minSearchLength?: number;
|
|
41
|
+
/** Refuse to select more than this many. Ignored when `single`. */
|
|
42
|
+
maxSelected?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Let a term that matches nothing be chosen as itself.
|
|
45
|
+
*
|
|
46
|
+
* For a field whose options are a *convenience* rather than the permitted
|
|
47
|
+
* set. A log console's source filter is the case that asked for it: the
|
|
48
|
+
* suggestions come from the values present in the log data, and a list that
|
|
49
|
+
* is sampled or capped would otherwise make a rarer source unreachable —
|
|
50
|
+
* turning a filter people could always type into one they cannot.
|
|
51
|
+
*
|
|
52
|
+
* Off by default. A picker over real records should not invent ids.
|
|
53
|
+
*/
|
|
54
|
+
allowCustomValue?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Whether the selection may be emptied.
|
|
57
|
+
*
|
|
58
|
+
* Default true. Set false for a field that must always hold something — a
|
|
59
|
+
* console's current database, where an empty selection would leave the editor
|
|
60
|
+
* pointed at nothing and every query failing.
|
|
61
|
+
*
|
|
62
|
+
* It hides the clear affordances *and* stops the underlying actions, because
|
|
63
|
+
* a control that offers a button doing nothing is worse than one that offers
|
|
64
|
+
* no button: hiding alone would still let a click on the already-selected
|
|
65
|
+
* option empty it.
|
|
66
|
+
*/
|
|
67
|
+
clearable?: boolean;
|
|
68
|
+
disabled?: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface OptionSelectState {
|
|
71
|
+
open: boolean;
|
|
72
|
+
setOpen(open: boolean): void;
|
|
73
|
+
/** What is in the search box, updated on every keystroke. */
|
|
74
|
+
term: string;
|
|
75
|
+
setTerm(term: string): void;
|
|
76
|
+
/** The term the visible options were actually searched with. */
|
|
77
|
+
appliedTerm: string;
|
|
78
|
+
/** The options to render, best match first. */
|
|
79
|
+
options: Option[];
|
|
80
|
+
status: OptionSelectStatus;
|
|
81
|
+
error: string | null;
|
|
82
|
+
/** More options match than are shown. */
|
|
83
|
+
hasMore: boolean;
|
|
84
|
+
/** Add another page to what is shown. */
|
|
85
|
+
showMore(): void;
|
|
86
|
+
/** Run the current search again. */
|
|
87
|
+
refresh(): void;
|
|
88
|
+
/** True while the term is short of `minSearchLength`. */
|
|
89
|
+
needsMoreInput: boolean;
|
|
90
|
+
minSearchLength: number;
|
|
91
|
+
/**
|
|
92
|
+
* The selection, as options.
|
|
93
|
+
*
|
|
94
|
+
* Every selected value appears, in selection order, whether or not its label
|
|
95
|
+
* is known — an unresolved value renders as itself rather than disappearing,
|
|
96
|
+
* because a chip that vanishes reads as "it was removed".
|
|
97
|
+
*/
|
|
98
|
+
selected: Option[];
|
|
99
|
+
isSelected(value: string): boolean;
|
|
100
|
+
toggle(value: string): void;
|
|
101
|
+
/** Deselect everything. Does nothing when the control is not clearable. */
|
|
102
|
+
clear(): void;
|
|
103
|
+
/** Whether emptying the selection is allowed at all. */
|
|
104
|
+
clearable: boolean;
|
|
105
|
+
/** Select every option currently listed, up to `maxSelected`. */
|
|
106
|
+
selectListed(): void;
|
|
107
|
+
/** The selection is at `maxSelected` and nothing further can be added. */
|
|
108
|
+
atLimit: boolean;
|
|
109
|
+
/** Which listed option the keyboard is on. -1 when none. */
|
|
110
|
+
activeIndex: number;
|
|
111
|
+
setActiveIndex(index: number): void;
|
|
112
|
+
/** Move by a step, or to an end. Wraps. */
|
|
113
|
+
moveActive(to: number | 'first' | 'last'): void;
|
|
114
|
+
disabled: boolean;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* A searchable selection, without any markup.
|
|
118
|
+
*
|
|
119
|
+
* Split out for the same reason `useAuditConsole` is: an application with its
|
|
120
|
+
* own design system wants this state machine and none of the HTML below it.
|
|
121
|
+
* Everything hard lives here — the debounce, discarding stale responses,
|
|
122
|
+
* paging, and turning selected values back into labels — and none of it is
|
|
123
|
+
* specific to how the thing looks.
|
|
124
|
+
*/
|
|
125
|
+
export declare function useOptionSelect(settings: UseOptionSelectOptions): OptionSelectState;
|
|
126
|
+
//# sourceMappingURL=useOptionSelect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useOptionSelect.d.ts","sourceRoot":"","sources":["../src/useOptionSelect.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,kBAAkB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