forked from lekoala/bootstrap5-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
356 lines (325 loc) · 13.5 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<title>Bootstrap 5 autocomplete demo</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="light dark" />
<meta name="theme-color" content="#111111" media="(prefers-color-scheme: light)" />
<meta name="theme-color" content="#eeeeee" media="(prefers-color-scheme: dark)" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/last-icon@2/last-icon.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-dark-5@1/dist/css/bootstrap-dark.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js" type="module"></script>
<!-- <link href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.bundle.min.js" type="module"></script> -->
<script type="module">
import Autocomplete from "./autocomplete.min.js";
// Example custom element
class BsAutocomplete extends HTMLElement {
constructor() {
super();
this.el = this.querySelector("input");
const opts = {};
const datalist = this.querySelector("datalist");
if (datalist) {
opts.datalist = datalist.getAttribute("id");
}
this.inst = new Autocomplete(this.el, opts);
}
}
customElements.define("bs-autocomplete", BsAutocomplete);
// End example element
const opts = {
onSelectItem: console.log,
};
var src = [];
for (let i = 0; i < 50; i++) {
src.push({
title: "Option " + i,
id: "opt" + i,
data: {
key: i,
},
});
}
Autocomplete.init("input.autocomplete", {
items: src,
valueField: "id",
labelField: "title",
highlightTyped: true,
onSelectItem: console.log,
});
document.getElementById("enableButton").addEventListener("click", (e) => {
e.preventDefault();
const el = document.getElementById("autocompleteInput");
const inst = Autocomplete.getInstance(el);
if (inst.isDisabled()) {
inst.enable();
} else {
inst.disable();
}
});
// We can use regular objects as source and customize label
new Autocomplete(document.getElementById("autocompleteRegularInput"), {
items: { opt_some: "Some", opt_value: "Value", opt_here: "Here is a very long element that should be truncated", opt_dia: "çaça" },
onRenderItem: (item, label) => {
return label + " (" + item.value + ")";
},
});
// we can provide items through a function
function suggest(query, populateResults) {
const results = ["France", "Germany", "United Kingdom"];
const filteredResults = results.filter((result) => result.indexOf(query) !== -1);
populateResults(filteredResults);
}
const optsSource = Object.assign({}, opts, {
source: suggest,
});
new Autocomplete(document.getElementById("autocompleteSourceInput"), optsSource);
new Autocomplete(document.getElementById("autocompleteDatalist"), opts);
new Autocomplete(document.getElementById("autocompleteRemote"), opts);
new Autocomplete(document.getElementById("autocompleteLiveRemote"), opts);
const optsCountry = Object.assign({}, opts, {
onRenderItem: (item, label) => {
return `<l-i set="fl" name="${item.value.toLowerCase()}" size="16"></l-i> ${label}`;
},
});
new Autocomplete(document.getElementById("autocompleteCountry"), optsCountry);
</script>
<style>
l-i {
--size: 1em;
display: inline-flex;
width: var(--size);
height: var(--size);
vertical-align: middle;
}
l-i svg {
display: block;
width: 100%;
height: 100%;
}
p l-i,
button l-i,
a l-i,
span l-i {
vertical-align: -0.125em;
}
/* highlightTyped use mark */
.autocomplete-menu mark {
text-decoration: underline;
background: none;
color: currentColor;
padding: 0;
}
/* Optional nicer scrollbars */
.autocomplete-menu {
--scroller-color: 0, 0%;
--scroller-color-lightness: 80%;
--scroller-bg-lightness: 90%;
--scroller-hover-factor: 0.8;
--scroller-thumb: hsl(var(--scroller-color), var(--scroller-color-lightness));
/* Replicate hover for webkit */
--scroller-thumb-hover: hsl(var(--scroller-color), calc(var(--scroller-color-lightness) * var(--scroller-hover-factor)));
--scroller-background: hsl(var(--scroller-color), calc(var(--scroller-bg-lightness)));
scrollbar-color: var(--scroller-thumb) var(--scroller-background);
scrollbar-width: thin;
}
.autocomplete-menu::-webkit-scrollbar {
width: 8px;
}
.autocomplete-menu::-webkit-scrollbar-track {
background: var(--scroller-background);
}
.autocomplete-menu::-webkit-scrollbar-thumb {
background: var(--scroller-thumb);
}
.autocomplete-menu::-webkit-scrollbar-thumb:hover {
background: var(--scroller-thumb-hover);
}
</style>
</head>
<body>
<div class="container">
<h1>Demo</h1>
<form class="needs-validation" novalidate method="get" action="https://vercel-dumper.vercel.app/">
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteInput" class="form-label">Type something</label>
<input type="text" class="form-control autocomplete" id="autocompleteInput" placeholder="Type something" />
</div>
<div class="col-md-4">
<button id="enableButton">Enable/disable</button>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteInputZero" class="form-label">Type something (zero threshold)</label>
<input
type="text"
class="form-control autocomplete"
id="autocompleteInputZero"
data-suggestions-threshold="0"
placeholder="Type something"
/>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteInputUpdate" class="form-label">Type something (update on select)</label>
<input
type="text"
class="form-control autocomplete"
id="autocompleteInputUpdate"
data-update-on-select="1"
data-autoselect-first="0"
placeholder="Type something"
/>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteInputDisabled" class="form-label">Type something (disabled)</label>
<input type="text" class="form-control autocomplete" id="autocompleteInputDisabled" placeholder="Type something" disabled />
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteInputReadonly" class="form-label">Type something (readonly)</label>
<input type="text" class="form-control autocomplete" id="autocompleteInputReadonly" placeholder="Type something" readonly />
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteRegularInput" class="form-label">Regular array</label>
<input type="text" class="form-control" id="autocompleteRegularInput" placeholder="Type something" />
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteHideIfNoMatch" class="form-label">Show message if no matches found</label>
<input
type="text"
class="form-control autocomplete"
id="autocompleteHideIfNoMatch"
placeholder="Type something"
data-not-found-message="No results found"
/>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteFullWidthInput" class="form-label">Full width</label>
<input
type="text"
class="form-control autocomplete"
id="autocompleteFullWidthInput"
data-full-width="true"
placeholder="Type something"
/>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteDatalist" class="form-label">Pick a time zone from datalist</label>
<input type="text" class="form-control" id="autocompleteDatalist" data-datalist="list-timezone" placeholder="Type something" />
<datalist id="list-timezone">
<option value="asia_aden">Asia/Aden</option>
<!-- we can use the value attribute -->
<option>Asia/Aqtau</option>
<option>Asia/Baghdad</option>
<option>Asia/Barnaul</option>
<option>Asia/Chita</option>
<option>Asia/Dhaka</option>
<option>Asia/Famagusta</option>
<option>Asia/Hong_Kong</option>
<option>Asia/Jayapura</option>
<option>Asia/Kuala_Lumpur</option>
<option>Asia/Jakarta</option>
</datalist>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteRemote" class="form-label">Pick from remote</label>
<input type="text" class="form-control" id="autocompleteRemote" data-server="demo.json" placeholder="Type something" />
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<select name="" id="related_field">
<option value="Choose">Choose</option>
<option value="Me">Me</option>
<option value="Or">Or</option>
<option value="Not">Not</option>
</select>
<label for="autocompleteLiveRemote" class="form-label">Pick from remote (live + related)</label>
<input
type="text"
class="form-control"
id="autocompleteLiveRemote"
data-server="https://jsonplaceholder.typicode.com/users"
data-live-server="true"
data-value-field="id"
data-label-field="name"
data-server-params='{"related":"related_field"}'
placeholder="Type something"
/>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteSourceInput" class="form-label">Pick a country (custom source)</label>
<input type="text" class="form-control" id="autocompleteSourceInput" placeholder="Type something" />
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<label for="autocompleteCountry" class="form-label">Pick a country (custom layout)</label>
<input type="text" class="form-control" id="autocompleteCountry" placeholder="Type something" data-datalist="list-countries" />
<datalist id="list-countries">
<option value="FR">France</option>
<option value="BE">Belgique</option>
<option value="LU">Luxembourg</option>
<option value="NL">Netherlands</option>
</datalist>
</div>
</div>
<div class="row mb-3 g-3">
<div class="col-md-4">
<bs-autocomplete>
<label for="autocompleteComponentInput" class="form-label">From a custom element</label>
<input type="text" class="form-control" id="autocompleteComponentInput" placeholder="Type something" />
<datalist id="list-component">
<option>Love</option>
<option>Custom</option>
<option>Element</option>
</datalist>
</bs-autocomplete>
</div>
</div>
<input type="reset" value="Reset" class="btn btn-outline-dark""/>
<button class="btn btn-primary" type="submit">Submit form</button>
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
<br /><!-- add a bit of space -->
</form>
</div>
<footer style="position: fixed; bottom: 0; left: 0; right: 0; padding: 1em; background: #eee">
<div class="container">
<input type="search" class="form-control autocomplete" id="autocompleteBottomInput" placeholder="Type something" />
</div>
</footer>
</body>
</html>