-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (56 loc) · 1.72 KB
/
index.js
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
/**
* Custom Textfield clear buttons
*
* Author: Scott O'Hara
* Version: 1.0.1
* License: https://github.com/scottaohara/clear-text-field-button/blob/main/LICENSE
*/
const customClearOptions = {
cssClass: 'clr-btn',
accName: 'clear entry'
};
const customClear = function ( inst, options ) {
const _options = Object.assign(customClearOptions, options);
const el = inst;
// TODO - could make this work with textarea, or roles textbox and searchbox
const input = el.querySelector('input');
let clrBtn;
const init = () => {
setupBtn();
clrBtn.addEventListener('click', clearInput);
el.addEventListener('focusout', hideClrBtn);
input.addEventListener('focus', checkValue);
input.addEventListener('keyup', checkValue);
input.addEventListener('mouseout', hideClrBtn);
input.addEventListener('mouseover', checkValue);
};
const setupBtn = () => {
const makeBtn = document.createElement('button');
makeBtn.classList.add(_options.cssClass);
makeBtn.type = 'button';
makeBtn.setAttribute('aria-label', _options.accName);
makeBtn.tabIndex = -1;
makeBtn.innerHTML = '<span aria-hidden=true>×</span>';
makeBtn.hidden = true;
el.append(makeBtn);
clrBtn = makeBtn;
};
const checkValue = () => {
if ( input.value.length
&& !input.readOnly
&& input.getAttribute('aria-disabled') !== 'true'
&& input.getAttribute('aria-readonly') !== 'true' )
{
clrBtn.hidden = false;
}
else clrBtn.hidden = true;
};
const hideClrBtn = function ( e ) {
if ( !el.contains(e.relatedTarget) ) clrBtn.hidden = true;
};
const clearInput = () => {
input.value = '';
input.focus();
};
init.call( this );
}; // customClear()