inertia-bootstrap-forms 3.0.2 → 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.
- package/README.fa.md +86 -6
- package/README.md +95 -8
- package/dist/chunks/UppyInput-Dt8CPfgC.js +210 -0
- package/dist/chunks/UppyInput-ldc834tZ.cjs +1 -0
- package/dist/chunks/_plugin-vue_export-helper-13TPlHFF.js +81 -0
- package/dist/chunks/_plugin-vue_export-helper-BHyUve4F.cjs +1 -0
- package/dist/chunks/http-CDPNmbjF.cjs +1 -0
- package/dist/chunks/http-DV3dRBe5.js +158 -0
- package/dist/country-codes.cjs +1 -0
- package/dist/country-codes.js +245 -0
- package/dist/dropzone.cjs +1 -0
- package/dist/dropzone.js +81 -0
- package/dist/editor.cjs +1 -0
- package/dist/editor.js +52 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +3128 -0
- package/dist/location.cjs +1 -0
- package/dist/location.js +84 -0
- package/dist/persian-date.cjs +1 -0
- package/dist/persian-date.js +73 -0
- package/dist/tel.cjs +1 -0
- package/dist/tel.js +85 -0
- package/dist/uppy-dashboard.cjs +1 -0
- package/dist/uppy-dashboard.js +31 -0
- package/dist/uppy.cjs +1 -0
- package/dist/uppy.js +4 -0
- package/index.d.ts +44 -30
- package/package.json +99 -16
- package/src/CheckboxToggle.vue +1 -1
- package/src/ComboboxInput.vue +495 -496
- package/src/PasswordInput.vue +124 -123
- package/src/StarRatingInput.vue +1 -1
- package/src/UppyDashboardInput.vue +45 -0
- package/src/UppyInput.vue +36 -3
- package/src/VoiceRecorderInput.vue +657 -0
- package/src/entries/country-codes.js +1 -0
- package/src/entries/dropzone.js +1 -0
- package/src/entries/editor.js +1 -0
- package/src/entries/location.js +1 -0
- package/src/entries/persian-date.js +1 -0
- package/src/entries/tel.js +1 -0
- package/src/entries/uppy-dashboard.js +1 -0
- package/src/entries/uppy.js +1 -0
- package/src/index.js +3 -19
- package/types/country-codes.d.ts +1 -0
- package/types/dropzone.d.ts +2 -0
- package/types/editor.d.ts +2 -0
- package/types/location.d.ts +2 -0
- package/types/persian-date.d.ts +2 -0
- package/types/tel.d.ts +2 -0
- package/types/uppy-dashboard.d.ts +12 -0
- package/types/uppy.d.ts +10 -0
- package/dist/inertia-bootstrap-forms.cjs.js +0 -1
- package/dist/inertia-bootstrap-forms.es.js +0 -3536
- package/src/RangeSliderInput.vue +0 -103
- package/src/Select2Input.vue +0 -251
package/src/ComboboxInput.vue
CHANGED
|
@@ -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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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
|
-
|
|
309
|
-
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
() =>
|
|
356
|
-
() =>
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
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
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
() =>
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
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
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
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
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
-
|
|
556
|
-
</template>
|
|
555
|
+
</template>
|