inertia-bootstrap-forms 3.0.1 → 4.0.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.
Files changed (56) hide show
  1. package/README.fa.md +86 -6
  2. package/README.md +95 -8
  3. package/dist/chunks/UppyInput-Dt8CPfgC.js +210 -0
  4. package/dist/chunks/UppyInput-ldc834tZ.cjs +1 -0
  5. package/dist/chunks/_plugin-vue_export-helper-13TPlHFF.js +81 -0
  6. package/dist/chunks/_plugin-vue_export-helper-BHyUve4F.cjs +1 -0
  7. package/dist/chunks/http-CDPNmbjF.cjs +1 -0
  8. package/dist/chunks/http-DV3dRBe5.js +158 -0
  9. package/dist/country-codes.cjs +1 -0
  10. package/dist/country-codes.js +245 -0
  11. package/dist/dropzone.cjs +1 -0
  12. package/dist/dropzone.js +81 -0
  13. package/dist/editor.cjs +1 -0
  14. package/dist/editor.js +52 -0
  15. package/dist/index.cjs +1 -0
  16. package/dist/index.js +3128 -0
  17. package/dist/location.cjs +1 -0
  18. package/dist/location.js +84 -0
  19. package/dist/persian-date.cjs +1 -0
  20. package/dist/persian-date.js +73 -0
  21. package/dist/tel.cjs +1 -0
  22. package/dist/tel.js +85 -0
  23. package/dist/uppy-dashboard.cjs +1 -0
  24. package/dist/uppy-dashboard.js +31 -0
  25. package/dist/uppy.cjs +1 -0
  26. package/dist/uppy.js +4 -0
  27. package/index.d.ts +44 -30
  28. package/package.json +99 -16
  29. package/src/CheckboxToggle.vue +7 -1
  30. package/src/ComboboxInput.vue +495 -496
  31. package/src/PasswordInput.vue +124 -123
  32. package/src/StarRatingInput.vue +1 -1
  33. package/src/UppyDashboardInput.vue +45 -0
  34. package/src/UppyInput.vue +36 -3
  35. package/src/VoiceRecorderInput.vue +657 -0
  36. package/src/entries/country-codes.js +1 -0
  37. package/src/entries/dropzone.js +1 -0
  38. package/src/entries/editor.js +1 -0
  39. package/src/entries/location.js +1 -0
  40. package/src/entries/persian-date.js +1 -0
  41. package/src/entries/tel.js +1 -0
  42. package/src/entries/uppy-dashboard.js +1 -0
  43. package/src/entries/uppy.js +1 -0
  44. package/src/index.js +3 -19
  45. package/types/country-codes.d.ts +1 -0
  46. package/types/dropzone.d.ts +2 -0
  47. package/types/editor.d.ts +2 -0
  48. package/types/location.d.ts +2 -0
  49. package/types/persian-date.d.ts +2 -0
  50. package/types/tel.d.ts +2 -0
  51. package/types/uppy-dashboard.d.ts +12 -0
  52. package/types/uppy.d.ts +10 -0
  53. package/dist/inertia-bootstrap-forms.cjs.js +0 -1
  54. package/dist/inertia-bootstrap-forms.es.js +0 -3529
  55. package/src/RangeSliderInput.vue +0 -103
  56. package/src/Select2Input.vue +0 -251
@@ -9,9 +9,9 @@ const isObject = (v) => v !== null && typeof v === 'object';
9
9
  const isEmpty = (v) => v === null || v === undefined || v === '';
10
10
 
11
11
  function readXsrf() {
12
- if (typeof document === 'undefined') return null;
13
- const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]+)/);
14
- return match ? decodeURIComponent(match[1]) : null;
12
+ if (typeof document === 'undefined') return null;
13
+ const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]+)/);
14
+ return match ? decodeURIComponent(match[1]) : null;
15
15
  }
16
16
 
17
17
  /**
@@ -19,443 +19,442 @@ function readXsrf() {
19
19
  *
20
20
  * - single / multiple selection, client side search or remote (AJAX) search
21
21
  * - the value lives in the FormContainer's v-model, like every other field
22
- * - slots: #option (menu items) and #selection (value shown in the toggle)
23
22
  */
24
23
  export default defineComponent({
25
- name: 'ComboboxInput',
26
- emits: [
27
- 'update:modelValue',
28
- 'change',
29
- 'selected',
30
- 'clear',
31
- 'search',
32
- 'search-success',
33
- 'search-error',
34
- 'show',
35
- 'shown',
36
- 'hide',
37
- 'hidden',
38
- ],
39
- props: {
40
- ...fieldProps,
41
- /** Strings, numbers or objects ({ id | value, name | label, description, disabled }). */
42
- options: { type: Array, default: () => [] },
43
- /** Property that holds the value of an option object (default: id → value → name → label). */
44
- valueKey: { type: String, default: null },
45
- /** Property that holds the label of an option object (default: name → label → title → value). */
46
- labelKey: { type: String, default: null },
47
- /** Legacy alias of `labelKey`. */
48
- label: { type: String, default: null },
49
- descriptionKey: { type: String, default: 'description' },
50
- multiple: { type: Boolean, default: false },
51
- placeholder: { type: String, default: null },
52
- /** Show a client-side search box. */
53
- searchEnabled: { type: Boolean, default: false },
54
- searchPlaceholder: { type: String, default: null },
55
- /**
56
- * Remote search: { url, method = 'POST', param = 'query', debounce = 400, minChars = 0, headers = {}, resultsKey }.
57
- * The endpoint must answer with a JSON array (or { data: [] }).
58
- */
59
- search: { type: Object, default: () => ({ url: null }) },
60
- /** Add an entry at the top of a single select that clears the value. */
61
- clearable: { type: Boolean, default: false },
62
- clearText: { type: String, default: null },
63
- noResultsText: { type: String, default: null },
64
- loadingText: { type: String, default: null },
65
- /** Text of a multiple select when more than one item is chosen. `{count}` is replaced. */
66
- selectedText: { type: String, default: null },
67
- /**
68
- * Multiple select only: show the chosen options themselves (true) instead of the
69
- * "N selected" counter (false, default).
70
- */
71
- showSelected: { type: Boolean, default: true },
72
- /** With `showSelected`: above this many chosen options the counter is shown instead. */
73
- maxVisible: { type: Number, default: null },
74
- /** Separator between the chosen labels (default: "، " for fa, ", " otherwise). */
75
- selectedSeparator: { type: String, default: null },
76
- searchNormalize: { type: Boolean, default: false },
77
- /** Floating UI placement of the menu. */
78
- placement: { type: String, default: 'bottom-start' },
79
- size: { type: String, default: null },
80
- /** Hide the caret icon. */
81
- hideDropdown: { type: Boolean, default: false },
82
- locale: { type: String, default: null },
83
- },
84
- setup(props, { emit, expose, slots }) {
85
- const field = useField(props, emit);
86
- const { t, locale } = useI18n(() => props.locale);
87
- const d = useDisposables();
88
-
89
- const root = ref(null);
90
- const toggle = ref(null);
91
- const menu = ref(null);
92
- const valueEl = ref(null);
93
- const searchInput = ref(null);
94
-
95
- const known = ref({}); // key -> option (every option ever seen, so labels survive remote searches)
96
- const remote = reactive({ results: null, loading: false, error: false });
97
-
98
- const remoteUrl = computed(() => props.search?.url || null);
99
- const hasSearch = computed(() => props.searchEnabled || !!remoteUrl.value);
100
-
101
- // ---------- option helpers ----------
102
- const valueOf = (item) => {
103
- if (!isObject(item)) return item;
104
- if (props.valueKey) return item[props.valueKey];
105
- return item.id ?? item.value ?? item.name ?? item.label;
106
- };
107
- const labelOf = (item) => {
108
- if (!isObject(item)) return item;
109
- const key = props.labelKey || props.label;
110
- return (key ? item[key] : (item.name ?? item.label ?? item.title ?? item.value)) ?? valueOf(item);
111
- };
112
- const descriptionOf = (item) => (isObject(item) ? item[props.descriptionKey] ?? null : null);
113
- const keyOf = (item) => String(valueOf(item));
114
-
115
- function register(list) {
116
- const next = { ...known.value };
117
- (list || []).forEach((o) => {
118
- next[keyOf(o)] = o;
119
- });
120
- known.value = next;
121
- }
122
- watch(() => props.options, (list) => register(list), { immediate: true, deep: true });
123
-
124
- // ---------- selection ----------
125
- const selectedRaw = computed(() => {
126
- const v = field.model.value;
127
- if (props.multiple) {
128
- if (Array.isArray(v)) return v;
129
- if (isObject(v)) return Object.values(v);
130
- return isEmpty(v) ? [] : [v];
131
- }
132
- return isEmpty(v) ? [] : [v];
133
- });
134
- const selectedKeys = computed(() => selectedRaw.value.map((v) => (isObject(v) ? keyOf(v) : String(v))));
135
-
136
- const labelForKey = (key) => {
137
- const option = known.value[key];
138
- return option !== undefined ? String(labelOf(option)) : key;
139
- };
140
-
141
- const selectedItems = computed(() =>
142
- selectedKeys.value.map((key) => {
143
- const option = known.value[key];
144
- return {
145
- key,
146
- raw: option,
147
- label: option !== undefined ? String(labelOf(option)) : key,
148
- description: option !== undefined ? descriptionOf(option) : null,
149
- };
150
- })
151
- );
152
-
153
- const separator = computed(() => props.selectedSeparator ?? (locale.value === 'fa' ? '، ' : ', '));
154
-
155
- // multiple + several chosen: list the options (showSelected) or show the counter
156
- const listsSelected = computed(
157
- () =>
158
- props.multiple &&
159
- props.showSelected &&
160
- selectedKeys.value.length > 1 &&
161
- (props.maxVisible === null || selectedKeys.value.length <= props.maxVisible)
162
- );
163
-
164
- // the #selection slot renders single values and the listed options of a multiple select
165
- const customSelection = computed(
166
- () => !!slots.selection && selectedKeys.value.length > 0 && (!props.multiple || selectedKeys.value.length === 1 || listsSelected.value)
167
- );
168
-
169
- const displayText = computed(() => {
170
- const keys = selectedKeys.value;
171
- if (!keys.length) return props.placeholder ?? t('select.placeholder');
172
-
173
- if (props.multiple && keys.length > 1) {
174
- if (listsSelected.value) return selectedItems.value.map((i) => i.label).join(separator.value);
175
- return (props.selectedText ?? t('select.selectedCount')).replace('{count}', keys.length);
176
- }
177
- return labelForKey(keys[0]);
178
- });
179
-
180
- // text that lives in `.combobox-value` (empty while the #selection slot is drawing the value)
181
- const plainText = computed(() => (customSelection.value ? '' : displayText.value));
182
- const showingPlaceholder = computed(() => selectedKeys.value.length === 0);
183
-
184
- // ---------- rendered options ----------
185
- const rendered = computed(() => {
186
- let list = remote.results ?? props.options;
187
-
188
- if (remote.results && props.multiple) {
189
- // keep the already selected items visible while searching
190
- const present = new Set(list.map(keyOf));
191
- const extra = selectedKeys.value.filter((k) => !present.has(k) && known.value[k] !== undefined).map((k) => known.value[k]);
192
- list = [...extra, ...list];
193
- }
194
-
195
- return list.map((item) => ({
196
- raw: item,
197
- key: keyOf(item),
198
- label: labelOf(item),
199
- description: descriptionOf(item),
200
- disabled: isObject(item) && !!item.disabled,
201
- }));
202
- });
203
-
204
- // ---------- DOM sync (Bootstrap owns classes of the items, Vue owns their existence) ----------
205
- function syncSelection() {
206
- if (!menu.value) return;
207
- const selected = new Set(selectedKeys.value);
208
- menu.value.querySelectorAll('.menu-item[data-bs-value]').forEach((el) => {
209
- const on = selected.has(el.dataset.bsValue) && el.dataset.bsValue !== '';
210
- el.classList.toggle('selected', on);
211
- el.setAttribute('aria-selected', on ? 'true' : 'false');
212
- });
213
- }
214
-
215
- function syncDisplay() {
216
- if (!valueEl.value) return;
217
- valueEl.value.textContent = plainText.value;
218
- valueEl.value.classList.toggle('combobox-placeholder', showingPlaceholder.value);
219
- }
220
-
221
- function syncInvalid() {
222
- toggle.value?.classList.toggle('is-invalid', field.isInvalid.value);
223
- }
224
-
225
- function sync() {
226
- syncSelection();
227
- syncDisplay();
228
- syncInvalid();
229
- }
230
-
231
- watch([() => field.model.value, rendered, plainText, field.isInvalid], () => nextTick(sync), { flush: 'post' });
232
-
233
- // ---------- Bootstrap events ----------
234
- function toRaw(key) {
235
- const option = known.value[key];
236
- return option !== undefined ? valueOf(option) : key;
237
- }
238
-
239
- function onBsChange(event) {
240
- let next;
241
- let chosen;
242
-
243
- if (props.multiple) {
244
- const keys = event.value || [];
245
- next = keys.map(toRaw);
246
- chosen = keys.map((k) => known.value[k]);
247
- } else if (event.value === '' || event.value === undefined) {
248
- next = null;
249
- chosen = null;
250
- } else {
251
- next = toRaw(event.value);
252
- chosen = known.value[event.value];
253
- }
254
-
255
- field.model.value = next;
256
- if (next === null) emit('clear');
257
- emit('selected', chosen);
258
- field.emitChange(next, event);
259
-
260
- // Bootstrap just rewrote the label in English; restore the real one
261
- nextTick(sync);
262
- syncDisplay();
263
- }
264
-
265
- // ---------- remote search ----------
266
- let timer = null;
267
- let controller = null;
268
- d.add(() => {
269
- clearTimeout(timer);
270
- controller?.abort();
271
- });
272
-
273
- function resetClientFilter() {
274
- if (!menu.value) return;
275
- menu.value.querySelectorAll('.menu-item[data-bs-value]').forEach((el) => {
276
- el.style.display = '';
277
- });
278
- menu.value.querySelector('.combobox-no-results')?.classList.toggle('d-none', rendered.value.length > 0);
279
- }
280
-
281
- async function runSearch(term) {
282
- const cfg = props.search || {};
283
- if (!cfg.url) return;
284
-
285
- controller?.abort();
286
- controller = new AbortController();
287
- remote.loading = true;
288
- remote.error = false;
289
-
290
- try {
291
- const url = new URL(cfg.url, window.location.origin);
292
- url.searchParams.set(cfg.param || 'query', term);
293
-
294
- const headers = {
295
- Accept: 'application/json',
296
- 'X-Requested-With': 'XMLHttpRequest',
297
- ...(cfg.headers || {}),
24
+ name: 'ComboboxInput',
25
+ emits: [
26
+ 'update:modelValue',
27
+ 'change',
28
+ 'selected',
29
+ 'clear',
30
+ 'search',
31
+ 'search-success',
32
+ 'search-error',
33
+ 'show',
34
+ 'shown',
35
+ 'hide',
36
+ 'hidden',
37
+ ],
38
+ props: {
39
+ ...fieldProps,
40
+ /** Strings, numbers or objects ({ id | value, name | label, description, disabled }). */
41
+ options: { type: Array, default: () => [] },
42
+ /** Property that holds the value of an option object (default: id → value → name → label). */
43
+ valueKey: { type: String, default: null },
44
+ /** Property that holds the label of an option object (default: name → label → title → value). */
45
+ labelKey: { type: String, default: null },
46
+ /** Legacy alias of `labelKey`. */
47
+ label: { type: String, default: null },
48
+ descriptionKey: { type: String, default: 'description' },
49
+ multiple: { type: Boolean, default: false },
50
+ placeholder: { type: String, default: null },
51
+ /** Show a client-side search box. */
52
+ searchEnabled: { type: Boolean, default: false },
53
+ searchPlaceholder: { type: String, default: null },
54
+ /**
55
+ * Remote search: { url, method = 'POST', param = 'query', debounce = 400, minChars = 0, headers = {}, resultsKey }.
56
+ * The endpoint must answer with a JSON array (or { data: [] }).
57
+ */
58
+ search: { type: Object, default: () => ({ url: null }) },
59
+ /** Add an entry at the top of a single select that clears the value. */
60
+ clearable: { type: Boolean, default: false },
61
+ clearText: { type: String, default: null },
62
+ noResultsText: { type: String, default: null },
63
+ loadingText: { type: String, default: null },
64
+ /** Text of a multiple select when more than one item is chosen. `{count}` is replaced. */
65
+ selectedText: { type: String, default: null },
66
+ /**
67
+ * Multiple select only: show the chosen options themselves (true) instead of the
68
+ * "N selected" counter (false, default).
69
+ */
70
+ showSelected: { type: Boolean, default: false },
71
+ /** With `showSelected`: above this many chosen options the counter is shown instead. */
72
+ maxVisible: { type: Number, default: null },
73
+ /** Separator between the chosen labels (default: "، " for fa, ", " otherwise). */
74
+ selectedSeparator: { type: String, default: null },
75
+ searchNormalize: { type: Boolean, default: false },
76
+ /** Floating UI placement of the menu. */
77
+ placement: { type: String, default: 'bottom-start' },
78
+ size: { type: String, default: null },
79
+ /** Hide the caret icon. */
80
+ hideDropdown: { type: Boolean, default: false },
81
+ locale: { type: String, default: null },
82
+ },
83
+ setup(props, { emit, expose, slots }) {
84
+ const field = useField(props, emit);
85
+ const { t, locale } = useI18n(() => props.locale);
86
+ const d = useDisposables();
87
+
88
+ const root = ref(null);
89
+ const toggle = ref(null);
90
+ const menu = ref(null);
91
+ const valueEl = ref(null);
92
+ const searchInput = ref(null);
93
+
94
+ const known = ref({}); // key -> option (every option ever seen, so labels survive remote searches)
95
+ const remote = reactive({ results: null, loading: false, error: false });
96
+
97
+ const remoteUrl = computed(() => props.search?.url || null);
98
+ const hasSearch = computed(() => props.searchEnabled || !!remoteUrl.value);
99
+
100
+ // ---------- option helpers ----------
101
+ const valueOf = (item) => {
102
+ if (!isObject(item)) return item;
103
+ if (props.valueKey) return item[props.valueKey];
104
+ return item.id ?? item.value ?? item.name ?? item.label;
298
105
  };
299
- const xsrf = readXsrf();
300
- if (xsrf && !headers['X-XSRF-TOKEN']) headers['X-XSRF-TOKEN'] = xsrf;
301
-
302
- const response = await fetch(url, {
303
- method: (cfg.method || 'POST').toUpperCase(),
304
- headers,
305
- credentials: 'same-origin',
306
- signal: controller.signal,
106
+ const labelOf = (item) => {
107
+ if (!isObject(item)) return item;
108
+ const key = props.labelKey || props.label;
109
+ return (key ? item[key] : (item.name ?? item.label ?? item.title ?? item.value)) ?? valueOf(item);
110
+ };
111
+ const descriptionOf = (item) => (isObject(item) ? item[props.descriptionKey] ?? null : null);
112
+ const keyOf = (item) => String(valueOf(item));
113
+
114
+ function register(list) {
115
+ const next = { ...known.value };
116
+ (list || []).forEach((o) => {
117
+ next[keyOf(o)] = o;
118
+ });
119
+ known.value = next;
120
+ }
121
+ watch(() => props.options, (list) => register(list), { immediate: true, deep: true });
122
+
123
+ // ---------- selection ----------
124
+ const selectedRaw = computed(() => {
125
+ const v = field.model.value;
126
+ if (props.multiple) {
127
+ if (Array.isArray(v)) return v;
128
+ if (isObject(v)) return Object.values(v);
129
+ return isEmpty(v) ? [] : [v];
130
+ }
131
+ return isEmpty(v) ? [] : [v];
307
132
  });
308
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
309
-
310
- const json = await response.json();
311
- const list = Array.isArray(json) ? json : json?.[cfg.resultsKey || 'data'] ?? json?.results ?? [];
312
-
313
- register(list);
314
- remote.results = list;
315
- remote.loading = false;
316
- emit('search-success', list);
317
-
318
- await nextTick();
319
- resetClientFilter();
320
- sync();
321
- } catch (error) {
322
- if (error?.name === 'AbortError') return;
323
- remote.loading = false;
324
- remote.error = true;
325
- remote.results = [];
326
- emit('search-error', error);
327
- await nextTick();
328
- resetClientFilter();
329
- }
330
- }
331
-
332
- function onSearchInput(event) {
333
- const term = event.target.value;
334
- emit('search', term);
335
-
336
- if (!remoteUrl.value) return;
337
- clearTimeout(timer);
338
-
339
- const minChars = props.search?.minChars ?? 0;
340
- if (term.length < minChars) {
341
- controller?.abort();
342
- remote.results = null;
343
- remote.loading = false;
344
- return;
345
- }
346
-
347
- remote.loading = true;
348
- timer = setTimeout(() => runSearch(term), props.search?.debounce ?? 400);
349
- }
350
-
351
- // ---------- Bootstrap instance ----------
352
- let offs = [];
353
- const { instance, create } = useBsInstance(
354
- 'Combobox',
355
- () => toggle.value,
356
- () => ({
357
- multiple: props.multiple,
358
- placeholder: props.placeholder ?? t('select.placeholder'),
359
- placement: props.placement,
360
- search: hasSearch.value,
361
- searchNormalize: props.searchNormalize,
362
- }),
363
- {
364
- onCreate(inst, el) {
365
- const on = (target, type, fn) => {
366
- target.addEventListener(type, fn);
367
- offs.push(() => target.removeEventListener(type, fn));
368
- };
369
-
370
- on(el, 'change.bs.combobox', onBsChange);
371
- on(el, 'show.bs.combobox', (e) => emit('show', e));
372
- on(el, 'shown.bs.combobox', (e) => emit('shown', e));
373
- on(el, 'hide.bs.combobox', (e) => emit('hide', e));
374
- on(el, 'hidden.bs.combobox', (e) => {
375
- if (remote.results !== null) {
376
- remote.results = null; // back to the initial options
377
- remote.error = false;
378
- nextTick(() => {
379
- resetClientFilter();
380
- sync();
381
- });
382
- }
383
- emit('hidden', e);
133
+ const selectedKeys = computed(() => selectedRaw.value.map((v) => (isObject(v) ? keyOf(v) : String(v))));
134
+
135
+ const labelForKey = (key) => {
136
+ const option = known.value[key];
137
+ return option !== undefined ? String(labelOf(option)) : key;
138
+ };
139
+
140
+ const selectedItems = computed(() =>
141
+ selectedKeys.value.map((key) => {
142
+ const option = known.value[key];
143
+ return {
144
+ key,
145
+ raw: option,
146
+ label: option !== undefined ? String(labelOf(option)) : key,
147
+ description: option !== undefined ? descriptionOf(option) : null,
148
+ };
149
+ })
150
+ );
151
+
152
+ const separator = computed(() => props.selectedSeparator ?? (locale.value === 'fa' ? '، ' : ', '));
153
+
154
+ // multiple + several chosen: list the options (showSelected) or show the counter
155
+ const listsSelected = computed(
156
+ () =>
157
+ props.multiple &&
158
+ props.showSelected &&
159
+ selectedKeys.value.length > 1 &&
160
+ (props.maxVisible === null || selectedKeys.value.length <= props.maxVisible)
161
+ );
162
+
163
+ // the #selection slot renders single values and the listed options of a multiple select
164
+ const customSelection = computed(
165
+ () => !!slots.selection && selectedKeys.value.length > 0 && (!props.multiple || selectedKeys.value.length === 1 || listsSelected.value)
166
+ );
167
+
168
+ const displayText = computed(() => {
169
+ const keys = selectedKeys.value;
170
+ if (!keys.length) return props.placeholder ?? t('select.placeholder');
171
+
172
+ if (props.multiple && keys.length > 1) {
173
+ if (listsSelected.value) return selectedItems.value.map((i) => i.label).join(separator.value);
174
+ return (props.selectedText ?? t('select.selectedCount')).replace('{count}', keys.length);
175
+ }
176
+ return labelForKey(keys[0]);
177
+ });
178
+
179
+ // text that lives in `.combobox-value` (empty while the #selection slot is drawing the value)
180
+ const plainText = computed(() => (customSelection.value ? '' : displayText.value));
181
+ const showingPlaceholder = computed(() => selectedKeys.value.length === 0);
182
+
183
+ // ---------- rendered options ----------
184
+ const rendered = computed(() => {
185
+ let list = remote.results ?? props.options;
186
+
187
+ if (remote.results && props.multiple) {
188
+ // keep the already selected items visible while searching
189
+ const present = new Set(list.map(keyOf));
190
+ const extra = selectedKeys.value.filter((k) => !present.has(k) && known.value[k] !== undefined).map((k) => known.value[k]);
191
+ list = [...extra, ...list];
192
+ }
193
+
194
+ return list.map((item) => ({
195
+ raw: item,
196
+ key: keyOf(item),
197
+ label: labelOf(item),
198
+ description: descriptionOf(item),
199
+ disabled: isObject(item) && !!item.disabled,
200
+ }));
201
+ });
202
+
203
+ // ---------- DOM sync (Bootstrap owns classes of the items, Vue owns their existence) ----------
204
+ function syncSelection() {
205
+ if (!menu.value) return;
206
+ const selected = new Set(selectedKeys.value);
207
+ menu.value.querySelectorAll('.menu-item[data-bs-value]').forEach((el) => {
208
+ const on = selected.has(el.dataset.bsValue) && el.dataset.bsValue !== '';
209
+ el.classList.toggle('selected', on);
210
+ el.setAttribute('aria-selected', on ? 'true' : 'false');
384
211
  });
212
+ }
213
+
214
+ function syncDisplay() {
215
+ if (!valueEl.value) return;
216
+ valueEl.value.textContent = plainText.value;
217
+ valueEl.value.classList.toggle('combobox-placeholder', showingPlaceholder.value);
218
+ }
219
+
220
+ function syncInvalid() {
221
+ toggle.value?.classList.toggle('is-invalid', field.isInvalid.value);
222
+ }
223
+
224
+ function sync() {
225
+ syncSelection();
226
+ syncDisplay();
227
+ syncInvalid();
228
+ }
229
+
230
+ watch([() => field.model.value, rendered, plainText, field.isInvalid], () => nextTick(sync), { flush: 'post' });
385
231
 
386
- if (searchInput.value) {
387
- on(searchInput.value, 'input', onSearchInput);
388
- on(searchInput.value, 'keydown', (e) => e.key === 'Enter' && e.preventDefault());
232
+ // ---------- Bootstrap events ----------
233
+ function toRaw(key) {
234
+ const option = known.value[key];
235
+ return option !== undefined ? valueOf(option) : key;
236
+ }
237
+
238
+ function onBsChange(event) {
239
+ let next;
240
+ let chosen;
241
+
242
+ if (props.multiple) {
243
+ const keys = event.value || [];
244
+ next = keys.map(toRaw);
245
+ chosen = keys.map((k) => known.value[k]);
246
+ } else if (event.value === '' || event.value === undefined) {
247
+ next = null;
248
+ chosen = null;
249
+ } else {
250
+ next = toRaw(event.value);
251
+ chosen = known.value[event.value];
389
252
  }
390
253
 
391
- sync();
392
- },
393
- onDestroy() {
394
- offs.forEach((off) => off());
395
- offs = [];
396
- },
254
+ field.model.value = next;
255
+ if (next === null) emit('clear');
256
+ emit('selected', chosen);
257
+ field.emitChange(next, event);
258
+
259
+ // Bootstrap just rewrote the label in English; restore the real one
260
+ nextTick(sync);
261
+ syncDisplay();
397
262
  }
398
- );
399
-
400
- // Options that change what the plugin reads at construction time
401
- watch(
402
- () => [props.multiple, hasSearch.value, props.placement, props.searchNormalize],
403
- async () => {
404
- await nextTick();
405
- create();
263
+
264
+ // ---------- remote search ----------
265
+ let timer = null;
266
+ let controller = null;
267
+ d.add(() => {
268
+ clearTimeout(timer);
269
+ controller?.abort();
270
+ });
271
+
272
+ function resetClientFilter() {
273
+ if (!menu.value) return;
274
+ menu.value.querySelectorAll('.menu-item[data-bs-value]').forEach((el) => {
275
+ el.style.display = '';
276
+ });
277
+ menu.value.querySelector('.combobox-no-results')?.classList.toggle('d-none', rendered.value.length > 0);
278
+ }
279
+
280
+ async function runSearch(term) {
281
+ const cfg = props.search || {};
282
+ if (!cfg.url) return;
283
+
284
+ controller?.abort();
285
+ controller = new AbortController();
286
+ remote.loading = true;
287
+ remote.error = false;
288
+
289
+ try {
290
+ const url = new URL(cfg.url, window.location.origin);
291
+ url.searchParams.set(cfg.param || 'query', term);
292
+
293
+ const headers = {
294
+ Accept: 'application/json',
295
+ 'X-Requested-With': 'XMLHttpRequest',
296
+ ...(cfg.headers || {}),
297
+ };
298
+ const xsrf = readXsrf();
299
+ if (xsrf && !headers['X-XSRF-TOKEN']) headers['X-XSRF-TOKEN'] = xsrf;
300
+
301
+ const response = await fetch(url, {
302
+ method: (cfg.method || 'POST').toUpperCase(),
303
+ headers,
304
+ credentials: 'same-origin',
305
+ signal: controller.signal,
306
+ });
307
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
308
+
309
+ const json = await response.json();
310
+ const list = Array.isArray(json) ? json : json?.[cfg.resultsKey || 'data'] ?? json?.results ?? [];
311
+
312
+ register(list);
313
+ remote.results = list;
314
+ remote.loading = false;
315
+ emit('search-success', list);
316
+
317
+ await nextTick();
318
+ resetClientFilter();
319
+ sync();
320
+ } catch (error) {
321
+ if (error?.name === 'AbortError') return;
322
+ remote.loading = false;
323
+ remote.error = true;
324
+ remote.results = [];
325
+ emit('search-error', error);
326
+ await nextTick();
327
+ resetClientFilter();
328
+ }
329
+ }
330
+
331
+ function onSearchInput(event) {
332
+ const term = event.target.value;
333
+ emit('search', term);
334
+
335
+ if (!remoteUrl.value) return;
336
+ clearTimeout(timer);
337
+
338
+ const minChars = props.search?.minChars ?? 0;
339
+ if (term.length < minChars) {
340
+ controller?.abort();
341
+ remote.results = null;
342
+ remote.loading = false;
343
+ return;
344
+ }
345
+
346
+ remote.loading = true;
347
+ timer = setTimeout(() => runSearch(term), props.search?.debounce ?? 400);
406
348
  }
407
- );
408
-
409
- expose({
410
- show: () => instance.value?.show(),
411
- hide: () => instance.value?.hide(),
412
- toggle: () => instance.value?.toggle(),
413
- clear: () => {
414
- field.model.value = props.multiple ? [] : null;
415
- emit('clear');
416
- field.emitChange(field.model.value);
417
- },
418
- focus: () => toggle.value?.focus(),
419
- });
420
-
421
- return {
422
- ...field,
423
- t,
424
- root,
425
- toggle,
426
- menu,
427
- valueEl,
428
- searchInput,
429
- remote,
430
- hasSearch,
431
- rendered,
432
- displayText,
433
- plainText,
434
- selectedItems,
435
- customSelection,
436
- separator,
437
- selectedSet: computed(() => new Set(selectedKeys.value)),
438
- showingPlaceholder,
439
- sizeClass: computed(() => (props.size ? `form-control-${props.size}` : null)),
440
- };
441
- },
349
+
350
+ // ---------- Bootstrap instance ----------
351
+ let offs = [];
352
+ const { instance, create } = useBsInstance(
353
+ 'Combobox',
354
+ () => toggle.value,
355
+ () => ({
356
+ multiple: props.multiple,
357
+ placeholder: props.placeholder ?? t('select.placeholder'),
358
+ placement: props.placement,
359
+ search: hasSearch.value,
360
+ searchNormalize: props.searchNormalize,
361
+ }),
362
+ {
363
+ onCreate(inst, el) {
364
+ const on = (target, type, fn) => {
365
+ target.addEventListener(type, fn);
366
+ offs.push(() => target.removeEventListener(type, fn));
367
+ };
368
+
369
+ on(el, 'change.bs.combobox', onBsChange);
370
+ on(el, 'show.bs.combobox', (e) => emit('show', e));
371
+ on(el, 'shown.bs.combobox', (e) => emit('shown', e));
372
+ on(el, 'hide.bs.combobox', (e) => emit('hide', e));
373
+ on(el, 'hidden.bs.combobox', (e) => {
374
+ if (remote.results !== null) {
375
+ remote.results = null; // back to the initial options
376
+ remote.error = false;
377
+ nextTick(() => {
378
+ resetClientFilter();
379
+ sync();
380
+ });
381
+ }
382
+ emit('hidden', e);
383
+ });
384
+
385
+ if (searchInput.value) {
386
+ on(searchInput.value, 'input', onSearchInput);
387
+ on(searchInput.value, 'keydown', (e) => e.key === 'Enter' && e.preventDefault());
388
+ }
389
+
390
+ sync();
391
+ },
392
+ onDestroy() {
393
+ offs.forEach((off) => off());
394
+ offs = [];
395
+ },
396
+ }
397
+ );
398
+
399
+ // Options that change what the plugin reads at construction time
400
+ watch(
401
+ () => [props.multiple, hasSearch.value, props.placement, props.searchNormalize],
402
+ async () => {
403
+ await nextTick();
404
+ create();
405
+ }
406
+ );
407
+
408
+ expose({
409
+ show: () => instance.value?.show(),
410
+ hide: () => instance.value?.hide(),
411
+ toggle: () => instance.value?.toggle(),
412
+ clear: () => {
413
+ field.model.value = props.multiple ? [] : null;
414
+ emit('clear');
415
+ field.emitChange(field.model.value);
416
+ },
417
+ focus: () => toggle.value?.focus(),
418
+ });
419
+
420
+ return {
421
+ ...field,
422
+ t,
423
+ root,
424
+ toggle,
425
+ menu,
426
+ valueEl,
427
+ searchInput,
428
+ remote,
429
+ hasSearch,
430
+ rendered,
431
+ displayText,
432
+ plainText,
433
+ selectedItems,
434
+ customSelection,
435
+ separator,
436
+ selectedSet: computed(() => new Set(selectedKeys.value)),
437
+ showingPlaceholder,
438
+ sizeClass: computed(() => (props.size ? `form-control-${props.size}` : null)),
439
+ };
440
+ },
442
441
  });
443
442
  </script>
444
443
 
445
444
  <template>
446
- <div ref="root" class="combobox">
447
- <button
448
- ref="toggle"
449
- type="button"
450
- class="combobox-toggle form-control"
451
- :class="sizeClass"
452
- data-bs-toggle="combobox"
453
- :id="inputId"
454
- :disabled="isDisabled || readonly"
455
- aria-haspopup="listbox"
456
- :aria-invalid="isInvalid || undefined"
457
- :aria-required="required || undefined"
458
- >
445
+ <div ref="root" class="combobox">
446
+ <button
447
+ ref="toggle"
448
+ type="button"
449
+ class="combobox-toggle form-control"
450
+ :class="sizeClass"
451
+ data-bs-toggle="combobox"
452
+ :id="inputId"
453
+ :disabled="isDisabled || readonly"
454
+ aria-haspopup="listbox"
455
+ :aria-invalid="isInvalid || undefined"
456
+ :aria-required="required || undefined"
457
+ >
459
458
  <span v-if="customSelection" class="combobox-selection d-flex align-items-center flex-wrap gap-2 text-start">
460
459
  <template v-for="(item, index) in selectedItems" :key="item.key">
461
460
  <slot
@@ -469,57 +468,57 @@ export default defineComponent({
469
468
  >{{ item.label }}</slot>
470
469
  </template>
471
470
  </span>
472
- <span ref="valueEl" class="combobox-value" :class="{ 'combobox-placeholder': showingPlaceholder }" v-text="plainText"></span>
473
- <svg
474
- v-if="!hideDropdown"
475
- class="combobox-caret"
476
- width="16"
477
- height="16"
478
- viewBox="0 0 16 16"
479
- fill="none"
480
- stroke="currentColor"
481
- stroke-width="1.5"
482
- stroke-linecap="round"
483
- stroke-linejoin="round"
484
- aria-hidden="true"
485
- >
486
- <path d="m3 6 5 5 5-5" />
487
- </svg>
488
- </button>
489
-
490
- <div ref="menu" class="menu" role="listbox" :aria-multiselectable="multiple || undefined">
491
- <div v-if="hasSearch" class="combobox-search">
492
- <input
493
- ref="searchInput"
494
- type="search"
495
- class="form-control combobox-search-input"
496
- autocomplete="off"
497
- :placeholder="searchPlaceholder ?? t('select.search')"
498
- />
499
- </div>
500
-
501
- <div v-if="remote.loading" class="menu-header" role="status">{{ loadingText ?? t('select.loading') }}</div>
502
- <div v-else-if="remote.error" class="menu-header" role="alert">{{ t('select.error') }}</div>
503
-
504
- <button
505
- v-if="clearable && !multiple"
506
- type="button"
507
- class="menu-item"
508
- data-bs-value=""
509
- role="option"
510
- >
511
- <span class="menu-item-content"><span>{{ clearText ?? t('select.clear') }}</span></span>
512
- </button>
513
-
514
- <button
515
- v-for="item in rendered"
516
- :key="item.key"
517
- type="button"
518
- class="menu-item"
519
- role="option"
520
- :data-bs-value="item.key"
521
- :disabled="item.disabled || undefined"
522
- >
471
+ <span ref="valueEl" class="combobox-value" :class="{ 'combobox-placeholder': showingPlaceholder }" v-text="plainText"></span>
472
+ <svg
473
+ v-if="!hideDropdown"
474
+ class="combobox-caret"
475
+ width="16"
476
+ height="16"
477
+ viewBox="0 0 16 16"
478
+ fill="none"
479
+ stroke="currentColor"
480
+ stroke-width="1.5"
481
+ stroke-linecap="round"
482
+ stroke-linejoin="round"
483
+ aria-hidden="true"
484
+ >
485
+ <path d="m3 6 5 5 5-5" />
486
+ </svg>
487
+ </button>
488
+
489
+ <div ref="menu" class="menu" role="listbox" :aria-multiselectable="multiple || undefined">
490
+ <div v-if="hasSearch" class="combobox-search">
491
+ <input
492
+ ref="searchInput"
493
+ type="search"
494
+ class="form-control combobox-search-input"
495
+ autocomplete="off"
496
+ :placeholder="searchPlaceholder ?? t('select.search')"
497
+ />
498
+ </div>
499
+
500
+ <div v-if="remote.loading" class="menu-header" role="status">{{ loadingText ?? t('select.loading') }}</div>
501
+ <div v-else-if="remote.error" class="menu-header" role="alert">{{ t('select.error') }}</div>
502
+
503
+ <button
504
+ v-if="clearable && !multiple"
505
+ type="button"
506
+ class="menu-item"
507
+ data-bs-value=""
508
+ role="option"
509
+ >
510
+ <span class="menu-item-content"><span>{{ clearText ?? t('select.clear') }}</span></span>
511
+ </button>
512
+
513
+ <button
514
+ v-for="item in rendered"
515
+ :key="item.key"
516
+ type="button"
517
+ class="menu-item"
518
+ role="option"
519
+ :data-bs-value="item.key"
520
+ :disabled="item.disabled || undefined"
521
+ >
523
522
  <span class="menu-item-content">
524
523
  <slot
525
524
  name="option"
@@ -534,23 +533,23 @@ export default defineComponent({
534
533
  <span v-if="item.description" class="menu-item-description">{{ item.description }}</span>
535
534
  </slot>
536
535
  </span>
537
- <svg
538
- class="menu-item-check"
539
- width="16"
540
- height="16"
541
- viewBox="0 0 16 16"
542
- fill="none"
543
- stroke="currentColor"
544
- stroke-width="2"
545
- stroke-linecap="round"
546
- stroke-linejoin="round"
547
- aria-hidden="true"
548
- >
549
- <path d="m3.5 8.5 3 3 6-7" />
550
- </svg>
551
- </button>
552
-
553
- <div class="combobox-no-results d-none">{{ noResultsText ?? t('select.noResults') }}</div>
536
+ <svg
537
+ class="menu-item-check"
538
+ width="16"
539
+ height="16"
540
+ viewBox="0 0 16 16"
541
+ fill="none"
542
+ stroke="currentColor"
543
+ stroke-width="2"
544
+ stroke-linecap="round"
545
+ stroke-linejoin="round"
546
+ aria-hidden="true"
547
+ >
548
+ <path d="m3.5 8.5 3 3 6-7" />
549
+ </svg>
550
+ </button>
551
+
552
+ <div class="combobox-no-results d-none">{{ noResultsText ?? t('select.noResults') }}</div>
553
+ </div>
554
554
  </div>
555
- </div>
556
- </template>
555
+ </template>