-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestion.html
527 lines (481 loc) · 23.5 KB
/
Suggestion.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
523
524
525
526
527
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<link rel="stylesheet" href="dist/css/SourcePage.css" />
<link rel="stylesheet" href="dist/css/A11yWrapper.css" />
<title>Accessible and Responsive Carousel</title>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">Auto suggestion box</h1>
<p>
It's a JS based component which requires less coding and satisfy project need efficiently.
Create your own setting by following the hooks provided from this component and point out the input box
which will act as Suggestion box and then Voila !!
</p>
</div>
</div>
<div class="container" role="main">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item active" aria-current="page">Suggestion box</li>
</ol>
</nav>
<div class="row mt-4">
<nav class="col-md-3 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#init">Initial setup</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#configuration">Configuration</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#multiple_data_source">Various data source</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#multiple">Multiple use</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#accessibility">Accessibility</a>
</li>
</ul>
</div>
</nav>
<div class="col-sm-12 col-md-9">
<h2>Demo</h2>
<!-- Search input -->
<div class="a11y_search_field">
<form name="a11ySuggestionForm" class="fund-finder-form">
<div class="a11y_input_wrapper">
<label for="a11y_input_box">Search by word</label>
<input id="a11y_input_box" name="a11y_input_box" type="text" class="form-control"
autocomplete="off" aria-owns="res" aria-autocomplete="both"
placeholder="Search by word">
</div>
</form>
</div>
<div id="init" class="mt-5">
<h2>Initial setup</h2>
<p>
Paste below HTML structure at your code where you want Suggestion box to appear. Make sure
you use "a11y_input_wrapper" class to the parent DIV element. This class holds styling for
suggestion box. Any custom class can be declared afterward.
</p>
<pre>
<code>
<span>
<div class="a11y_input_wrapper">
<label class="sr-only" for="a11y_input_box">Search by word</label>
<input id="a11y_input_box" name="a11y_input_box" type="text"
class="form-control"
autocomplete="off" aria-owns="res" aria-autocomplete="both"
placeholder="Search by word">
</div>
</span>
</code>
</pre>
<p>
Call the autosuggestion component from Wrapper library at initial Load event. It can be
<strong>$(document).ready(...)</strong> or <strong>Window.onload</strong> or
<strong>Window.eventListener('load', ...)</strong>. Then Suggestion box will
load with default settings.
<strong>
Check out how to add <a href="#multiple">multiple</a> suggestion box on the page
</strong>
</p>
<pre>
<code>
<span>
var suggestionOption = {
dataCanvas: {
source: ['Apple', 'Mango', 'Argentina', 'Monkey', 'Bellisimo', 'Ball', 'Cat', 'Belgium']
},
minimumType: 1,
noOfItems: 10,
searchType: 'contains',
SRText: {
instruction: "When autocomplete results are available use up and down arrows to review and enter to select. Touch device users, explore by touch or with swipe gestures.",
announceCount: "%number% suggestions found. Use up and down arrows to navigate",
suggestionBoxLabel: "List of suggestions",
closeBtn: "Close button"
}
};
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(suggestionOption);
});
OR
// JavaScript Users - ID name for one single slider
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(...);
});
OR
//jQuery Users
$( document ).ready(function() {
window.A11yWrapper('#a11y_input_box').autosuggestion(...);
});
</span>
</code>
</pre>
</div>
<div id="configuration">
<h2>Configuration</h2>
<p>
All sort of customization need to be passed as an Object. Following are the available settings which can be customized
</p>
<div class="table-responsive">
<table class="table table-bordered">
<thead class="thead-inverse">
<tr>
<th>Key name</th>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>dataCanvas</td>
<td>Object</td>
<td>
This is a data source object and it contains three keys
<ul>
<li>
<strong>source</strong> - This is a required key and accepts
<ul>
<li><strong>string</strong> - Only URL type string is allowed. If URL is passed then library will make a service call and fetch result</li>
<li><strong>array</strong> - plain array with any sort of items or an array of objects.</li>
</ul>
</li>
<li>
<strong>hooks</strong> - This is an optional key and will be effective if <strong>source</strong> key holds array of
objects. For an example, if developer wants to retrieve other keys from the object related to searched result,
then they have to mention the key names inside an array and pass it to <strong>hooks</strong> key.
</li>
<li>
<strong>displayKeyName</strong> - This key becomes mandatory if <strong>source</strong> key holds array of objects.
Developer needs to pass the specific key name which will be used to make the suggestion list. Please make sure this key exist on first level and not nested
</li>
</ul>
Checkout the multiple data source scenarios <a href="#multiple_data_source">here</a>
</td>
</tr>
<tr>
<td>minimumType</td>
<td>Integer. No fraction allowed</td>
<td>Determines how many characters need to be typed before showing the result. Default is 2</td>
</tr>
<tr>
<td>noOfItems</td>
<td>Integer. No fraction allowed</td>
<td>Determines number of items to be shown inside the suggestion list. Default is 10</td>
</tr>
<tr>
<td>searchType</td>
<td>String</td>
<td>
Determins the search type. Available values are
<ul>
<li><strong>contains</strong> - Empty string will be consider as "contains". This is a Default value.</li>
<li><strong>start</strong> - String starts with the typed value inside input box</li>
<li><strong>end</strong> - String ends with typed value inside input box</li>
</ul>
</td>
</tr>
<tr>
<td>SRText</td>
<td>Object</td>
<td>
This is mainly a screen reader content object and it contains three keys
<ul>
<li><strong>instruction</strong> - This is String type and define instruction on about how to use the Suggestion box</li>
<li><strong>announceCount</strong> - This is String type and defines number of available suggestions at run time</li>
<li><strong>suggestionBoxLabel</strong> - This is String type and place before suggestion box as Label.</li>
<li><strong>closeBtn</strong> - This is String type and screen reader content for close button.</li>
</ul>
</td>
</tr>
<tr>
<td>onItemSelected</td>
<td>Function</td>
<td>
This is a callback function and triggers when any item from the suggestion list is selected.
It returns an object with key-value pair.
If <strong>source</strong> key holds array of objects then return object will contain selected value and
<strong>hooks</strong> value
</td>
</tr>
<tr>
<td>onFocusIn</td>
<td>Function</td>
<td>
This is a callback function and triggers when suggestion box gain focus and return current value from the input box
</td>
</tr>
<tr>
<td>onFocusOut</td>
<td>Function</td>
<td>
This is a callback function and triggers when suggestion box loose focus and return value from the input box
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="multiple_data_source">
<h2>Different data source</h2>
<ul>
<li>
URL string
<pre>
<code>
<span>
var suggestionOption = {
dataCanvas: {
source: 'https://mocki.io/v1/d4867d8b-b5d5-4a48-a4ab-79131b5809b8',
hooks: ['city'],
displayKeyName: "name"
},
...
};
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(suggestionOption);
});
/* Return result from console log
Selected Item: {name: "Harry Potter", city: "London"}
*/
</span>
</code>
</pre>
</li>
<li>
Plain array
<pre>
<code>
<span>
var suggestionOption = {
dataCanvas: {
source: ['Apple', 'Mango', 'Argentina', 'Monkey', 'Bellisimo', 'Ball', 'Cat', 'Belgium'],
},
minimumType: 1,
noOfItems: 10,
searchType: 'contains',
SRText: {
instruction: "When autocomplete results are available use up and down arrows to review and enter to select. Touch device users, explore by touch or with swipe gestures.",
announceCount: "%number% suggestions found. Use up and down arrows to navigate",
suggestionBoxLabel: "List of suggestions",
closeBtn: "Close button"
},
onItemSelected: function (data) {
console.log('Selected Item: ', data);
},
onFocusIn: function (data) {
console.log('Focus In event from Callback. Selected item: ', data);
},
onFocusOut: function (data) {
console.log('Focus out event from Callback. Selected item: ', data);
}
};
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(suggestionOption);
});
</span>
</code>
</pre>
</li>
<li>
Array of Objects
<pre>
<code>
<span>
var suggestionOption = {
dataCanvas: {
source: [
{
id: '1',
itemID: '10',
text: 'Apple',
desc: 'Apple is a nice fruit'
},
{
id: '2',
itemID: '20',
text: 'Argentina',
desc: 'Argentina is a country'
},
{
id: '3',
itemID: '30',
text: 'Ball',
desc: 'Ball is a object'
},
{
id: '4',
itemID: '40',
text: 'Batman',
desc: 'Batman is a character'
}
],
hooks: ['itemID', 'desc'],
displayKeyName: "text"
},
...
};
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(suggestionOption);
});
/* Return result from console log
Selected Item: {text: "Argentina", itemid: "20", desc: "Argentina is a country"}
*/
</span>
</code>
</pre>
</li>
</ul>
</div>
<div id="multiple">
<h2>How to use Suggestion box component multiple times</h2>
<p>
Call the wrapper class every time for different input box by passing unique ID of the box.
</p>
<pre>
<code>
<span>
var suggestionOption = {...};
window.addEventListener('load', function () {
window.A11yWrapper('#input_box_1').autosuggestion(suggestionOption);
window.A11yWrapper('#input_box_2').autosuggestion(suggestionOption);
});
</span>
</code>
</pre>
</div>
<div id="accessibility">
<h2>Accessibility</h2>
<p class="lead mt-3">Keyboard interaction</p>
<ul>
<li>
<strong>ESC</strong> - ESC key press will close the suggestion box and Input box will gain focus
</li>
<li>
<strong>TAB</strong> - Press TAB to focus the input box. Pressing TAB again would move focus to Cross (X) icon
</li>
<li>
<strong>RETURN/ ENTER</strong> - Pressing Enter will select the item from suggeion list
</li>
<li>
<strong>UP or DOWN arrow</strong> - Use these keys to navigate between suggestion list items
</li>
</ul>
</div>
</div>
</div>
<button type="button" class="btn btn-default scroll-top" aria-label="Go to Top" id="scroll_top"
onclick="topFunction()">
<i class="fa fa-chevron-up" aria-hidden="true"></i>
</button>
<footer>
<p>
© 2021 <a href="https://www.linkedin.com/in/md-asif-ahmed-oni-57271673/">Md Asif Ahmed Oni</a> / A11yWrapper. |
Check the project in <a href="https://github.com/oni20/A11yWrapper" target="_blank">GitHub</a> |
Licensed under the MIT license.
</p>
</footer>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"
integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"
integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn"
crossorigin="anonymous"></script>
<script src="dist/js/A11yWrapper.min.js" type="text/javascript"></script>
<script type="text/javascript">
var mybutton = document.getElementById("scroll_top");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () { scrollFunction() };
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
</script>
<script type="text/javascript">
var plainArr = ['Apple', 'Mango', 'Argentina', 'Monkey', 'Bellisimo', 'Ball', 'Cat', 'Belgium'],
arrObj = [
{
id: '1',
itemID: '10',
text: 'Apple',
desc: 'Apple is a nice fruit'
},
{
id: '2',
itemID: '20',
text: 'Argentina',
desc: 'Argentina is a country'
},
{
id: '3',
itemID: '30',
text: 'Ball',
desc: 'Ball is a object'
},
{
id: '4',
itemID: '40',
text: 'Batman',
desc: 'Batman is a character'
}
],
url = 'https://mocki.io/v1/d4867d8b-b5d5-4a48-a4ab-79131b5809b8'; //'https://funds.dynamic.ca/fundprofiles/filter?locale=en-us'
var suggestionOption = {
dataCanvas: {
source: url,
hooks: ['city'],
displayKeyName: "name"
},
minimumType: 1,
noOfItems: 10,
searchType: 'contains', // other available values -> start , end || start = String starts with the value, end = String ends with
SRText: {
instruction: "When autocomplete results are available use up and down arrows to review and enter to select. Touch device users, explore by touch or with swipe gestures.",
announceCount: "%number% suggestions found. Use up and down arrows to navigate",
suggestionBoxLabel: "List of suggestions",
closeBtn: "Close button"
},
onItemSelected: function (data) {
console.log('Selected Item: ', data);
},
onFocusIn: function (data) {
console.log('Focus In event from Callback. Selected item: ', data);
},
onFocusOut: function (data) {
console.log('Focus out event from Callback. Selected item: ', data);
}
};
window.addEventListener('load', function () {
window.A11yWrapper('#a11y_input_box').autosuggestion(suggestionOption);
});
</script>
</body>
</html>