semantic-forms 3.2.0 → 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.md +183 -23
- package/package.json +23 -20
- package/semanticForms.css +624 -376
- package/semanticForms.js +142 -93
- package/semanticForms.scss +986 -0
- package/semanticForms.less +0 -638
package/semanticForms.js
CHANGED
|
@@ -2,112 +2,161 @@ window.semanticForms = () => {
|
|
|
2
2
|
// do some feature detection so none of the JS executes if the browser is too old
|
|
3
3
|
if (typeof document.getElementsByClassName !== 'function' || typeof document.querySelector !== 'function' || !document.body.classList || !window.MutationObserver) return
|
|
4
4
|
|
|
5
|
+
const nodeNameLookup = ['TEXTAREA', 'SELECT']
|
|
6
|
+
const inputTypeLookup = ['checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'image', 'month', 'number', 'password', 'radio', 'range', 'search', 'tel', 'text', 'time', 'url', 'week']
|
|
7
|
+
|
|
5
8
|
// progressively enhance form elements that have the semanticForms class
|
|
6
9
|
const forms = document.querySelectorAll('form.semanticForms:not(.semanticFormsActive)')
|
|
10
|
+
|
|
7
11
|
for (const form of forms) {
|
|
8
12
|
form.classList.add('semanticFormsActive')
|
|
9
|
-
if (
|
|
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
|
-
label.setAttribute('hidden', 'hidden')
|
|
48
|
-
}
|
|
49
|
-
if (nodeName !== 'SELECT' && type !== 'checkbox' && type !== 'radio') {
|
|
50
|
-
// if it doesn't have a placeholder, add a blank one
|
|
51
|
-
if (!input.getAttribute('placeholder')) input.setAttribute('placeholder', ' ')
|
|
52
|
-
inputHandler(input) // force x to appear on inputs with prefilled value
|
|
53
|
-
}
|
|
54
|
-
input.addEventListener('input', inputHandler)
|
|
55
|
-
input.addEventListener('mousemove', event => {
|
|
56
|
-
const el = event.target
|
|
57
|
-
const nodeName = el.nodeName
|
|
58
|
-
const type = el.getAttribute('type')
|
|
59
|
-
if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
|
|
60
|
-
inputHandler(event)
|
|
61
|
-
if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left && clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
|
|
62
|
-
) {
|
|
63
|
-
if (!el.classList.contains('onX')) {
|
|
64
|
-
el.classList.add('onX')
|
|
65
|
-
}
|
|
66
|
-
} else {
|
|
67
|
-
el.classList.remove('onX')
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
input.addEventListener('click', event => {
|
|
72
|
-
const el = event.target
|
|
73
|
-
const nodeName = el.nodeName
|
|
74
|
-
const type = el.getAttribute('type')
|
|
75
|
-
if (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week') {
|
|
76
|
-
if (el.offsetWidth - clearfieldHorizontalOffset < event.clientX - el.getBoundingClientRect().left &&
|
|
77
|
-
clearfieldHorizontalOffset + clearfieldVerticalOffset > event.clientY - el.getBoundingClientRect().top
|
|
78
|
-
) {
|
|
79
|
-
el.value = ''
|
|
80
|
-
el.dispatchEvent(new Event('input'))
|
|
81
|
-
el.form.dispatchEvent(new Event('input'))
|
|
82
|
-
el.classList.remove('x')
|
|
83
|
-
el.classList.remove('onX')
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
})
|
|
13
|
+
if (form.classList.contains('lowFlow')) continue
|
|
14
|
+
|
|
15
|
+
// update each input in the semantic form
|
|
16
|
+
const inputs = Array.from(form.querySelectorAll('input, textarea, select'))
|
|
17
|
+
for (const input of inputs) {
|
|
18
|
+
// ignore input if it has previously been formatted
|
|
19
|
+
if (input.classList.contains('semanticform') || !input.id) continue
|
|
20
|
+
|
|
21
|
+
const type = input.getAttribute('type')
|
|
22
|
+
if (nodeNameLookup.includes(input.nodeName) || inputTypeLookup.includes(type)) {
|
|
23
|
+
// recursively find <dl> element
|
|
24
|
+
let dl = input.parentNode
|
|
25
|
+
while (dl && dl.nodeName !== 'DL') dl = dl.parentNode
|
|
26
|
+
|
|
27
|
+
if (!dl) continue
|
|
28
|
+
if (!dl.classList.contains('floatLabelForm')) dl.classList.add('floatLabelForm')
|
|
29
|
+
|
|
30
|
+
const label = input.parentNode.parentNode.id && (type === 'checkbox' || type === 'radio')
|
|
31
|
+
? document.querySelector('label[data-for=' + input.parentNode.parentNode.id.replace(/\./g, '\\.') + ']')
|
|
32
|
+
: document.querySelector('label[for=' + input.id.replace(/\./g, '\\.') + ']')
|
|
33
|
+
|
|
34
|
+
input.classList.add('semanticform')
|
|
35
|
+
|
|
36
|
+
// #region create labels
|
|
37
|
+
if (type === 'checkbox' || type === 'radio') {
|
|
38
|
+
// recursively find <dd> element
|
|
39
|
+
let dd = input.parentNode
|
|
40
|
+
while (dd && dd.nodeName !== 'DD') dd = dd.parentNode
|
|
41
|
+
|
|
42
|
+
if (dd.firstChild.nodeName !== 'LABEL') {
|
|
43
|
+
const newLabel = document.createElement('label')
|
|
44
|
+
newLabel.className = 'floatLabelFormAnimatedLabel'
|
|
45
|
+
|
|
46
|
+
if (type === 'checkbox' && input.parentNode.nodeName === 'DD') {
|
|
47
|
+
newLabel.setAttribute('for', input.id)
|
|
48
|
+
input.parentNode.classList.add('singleCheckbox')
|
|
49
|
+
newLabel.className = ''
|
|
50
|
+
label.setAttribute('hidden', 'hidden')
|
|
87
51
|
}
|
|
52
|
+
|
|
53
|
+
newLabel.innerHTML = label.innerHTML
|
|
54
|
+
|
|
55
|
+
if (dd.querySelector(':required') && label.getAttribute('data-no-asterisk') === null && !label.querySelector('span')) {
|
|
56
|
+
const text = label.getAttribute('data-asterisk-text') || 'This field is required.'
|
|
57
|
+
label.innerHTML += `<span title="${text}">*</span>`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!dd.querySelector('label')) dd.append(newLabel)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// removes old div that a radio or checkbox may have been added to
|
|
64
|
+
if (dd.parentElement.nodeName === 'DIV') dd.parentElement.remove()
|
|
65
|
+
|
|
66
|
+
const div = document.createElement('div')
|
|
67
|
+
div.append(label.closest('dt'), dd)
|
|
68
|
+
dl.append(div)
|
|
69
|
+
} else {
|
|
70
|
+
const newLabel = document.createElement('label')
|
|
71
|
+
newLabel.setAttribute('for', input.id)
|
|
72
|
+
newLabel.className = 'floatLabelFormAnimatedLabel'
|
|
73
|
+
|
|
74
|
+
newLabel.innerHTML = label.innerHTML
|
|
75
|
+
|
|
76
|
+
if (input.hasAttribute('required') && label.getAttribute('data-no-asterisk') === null && !label.querySelector('span')) {
|
|
77
|
+
const text = label.getAttribute('data-asterisk-text') || 'This field is required.'
|
|
78
|
+
newLabel.innerHTML += ` <span title="${text}">*</span>`
|
|
88
79
|
}
|
|
80
|
+
|
|
81
|
+
label.setAttribute('hidden', 'hidden')
|
|
82
|
+
|
|
83
|
+
insertAfter(newLabel, input)
|
|
89
84
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
// #endregion
|
|
86
|
+
|
|
87
|
+
// standard inputs
|
|
88
|
+
if (type !== 'checkbox' && type !== 'radio') {
|
|
89
|
+
if (!input.getAttribute('placeholder')) {
|
|
90
|
+
input.setAttribute('placeholder', ' ')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const div = document.createElement('div')
|
|
94
|
+
const dt = label.closest('dt')
|
|
95
|
+
const dd = input.closest('dd')
|
|
96
|
+
|
|
97
|
+
// #region clear button
|
|
98
|
+
if (input.nodeName !== 'SELECT' && type !== 'range') {
|
|
99
|
+
const clearBtn = document.createElement('button')
|
|
100
|
+
clearBtn.type = 'button'
|
|
101
|
+
clearBtn.ariaLabel = 'Clear input'
|
|
102
|
+
clearBtn.tabIndex = -1
|
|
103
|
+
clearBtn.innerHTML = '<svg viewBox="0 0 16 16" width="18" height="18"><path d="M 1 1 L 15 15 M 1 15 L 15 1" fill="none" stroke-width="2" stroke="currentColor" />'
|
|
104
|
+
clearBtn.classList.add('clear')
|
|
105
|
+
clearBtn.addEventListener('click', () => {
|
|
106
|
+
input.value = ''
|
|
107
|
+
input.focus()
|
|
108
|
+
})
|
|
109
|
+
insertAfter(clearBtn, dd.querySelector('label'))
|
|
110
|
+
}
|
|
111
|
+
// #endregion
|
|
112
|
+
|
|
113
|
+
// check for colspan- utility class
|
|
114
|
+
if (/colspan-/.test(dd.className)) {
|
|
115
|
+
const match = dd.className.match(/colspan-([0-9]|full)/)[0]
|
|
116
|
+
dd.classList.remove(match)
|
|
117
|
+
div.classList.add(match)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
div.append(dt, dd)
|
|
121
|
+
dl.append(div)
|
|
122
|
+
|
|
123
|
+
// determine visibility of newly created <div>
|
|
124
|
+
if (dt.style.display === 'none' && dd.style.display === 'none') div.style.display = 'none'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// handle file input clear btn, cannot be handled with CSS
|
|
128
|
+
if (type === 'file') {
|
|
129
|
+
const clearBtn = input.parentElement.querySelector('.clear')
|
|
130
|
+
input.addEventListener('input', e => {
|
|
131
|
+
clearBtn.style.display = e.target.files.length ? 'flex' : 'none'
|
|
132
|
+
})
|
|
133
|
+
clearBtn.addEventListener('click', () => {
|
|
134
|
+
clearBtn.style.display = 'none'
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// add listener to shift clear button when scrollbar present
|
|
139
|
+
for (const textarea of document.querySelectorAll('textarea')) {
|
|
140
|
+
// shifts the close button to the right if a scrollbar is present
|
|
141
|
+
const shiftCloseBtn = () => {
|
|
142
|
+
const clearBtn = textarea.parentElement?.querySelector('button.clear')
|
|
143
|
+
if (clearBtn) {
|
|
144
|
+
clearBtn.style.marginRight = textarea.clientHeight < textarea.scrollHeight ? '15px' : ''
|
|
145
|
+
}
|
|
146
|
+
}
|
|
93
147
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const el = event.target || event
|
|
97
|
-
const nodeName = el.nodeName
|
|
98
|
-
const type = el.getAttribute('type') || nodeName === 'TEXTAREA'
|
|
99
|
-
if ((nodeName && type) && (nodeName === 'TEXTAREA' || type === 'checkbox' || type === 'color' || type === 'date' || type === 'datetime-local' || type === 'email' || type === 'file' || type === 'month' || type === 'number' || type === 'password' || type === 'radio' || type === 'range' || type === 'search' || type === 'tel' || type === 'text' || type === 'time' || type === 'url' || type === 'week')) {
|
|
100
|
-
if (el.value) {
|
|
101
|
-
if (!el.classList.contains('x')) {
|
|
102
|
-
el.classList.add('x')
|
|
148
|
+
textarea.addEventListener('input', shiftCloseBtn)
|
|
149
|
+
textarea.addEventListener('mouseup', shiftCloseBtn)
|
|
103
150
|
}
|
|
104
|
-
} else {
|
|
105
|
-
el.classList.remove('x')
|
|
106
151
|
}
|
|
107
152
|
}
|
|
108
153
|
}
|
|
109
154
|
|
|
110
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Places an element immediately after another element
|
|
157
|
+
* @param {Object} newNode element being placed after the reference node
|
|
158
|
+
* @param {*} referenceNode element to be used as reference for new node
|
|
159
|
+
*/
|
|
111
160
|
function insertAfter (newNode, referenceNode) {
|
|
112
161
|
if (referenceNode.nextSibling) referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling)
|
|
113
162
|
else referenceNode.parentNode.appendChild(newNode)
|