Skip to content

Commit

Permalink
add reinitialization
Browse files Browse the repository at this point in the history
  • Loading branch information
awidener3 committed Feb 19, 2024
1 parent 08b276d commit 6545cc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Next version

- Put your changes here...
- Added a reinitialization method to re-scan existing forms. Called with `window.semanticForms.reinitialize(formName)`.
- Logic to enhance forms now ignores inputs that have already been enhanced.

## 3.1.0

Expand Down
8 changes: 7 additions & 1 deletion semanticForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ window.semanticForms = () => {
const clearfieldVerticalOffset = parseInt(form.getAttribute('data-clearfield-vertical-offset')) || 5
const inputs = Array.prototype.slice.call(form.getElementsByTagName('input')).concat(Array.prototype.slice.call(form.getElementsByTagName('textarea'))).concat(Array.prototype.slice.call(form.getElementsByTagName('select')))
for (const input of inputs) {
if (input.classList.contains('semanticform')) continue
if (input.id) {
const nodeName = input.nodeName
const type = input.getAttribute('type')
Expand All @@ -22,6 +23,7 @@ window.semanticForms = () => {
let label
if (input.parentNode.parentNode.id && (type === 'checkbox' || type === 'radio')) label = document.querySelector('label[data-for=' + input.parentNode.parentNode.id.replace(/\./g, '\\.') + ']')
else label = document.querySelector('label[for=' + input.id.replace(/\./g, '\\.') + ']')
input.classList.add('semanticform')
if (type === 'checkbox' || type === 'radio') {
dl = input.parentNode
while (dl && dl.nodeName !== 'DD') dl = dl.parentNode
Expand All @@ -47,7 +49,6 @@ window.semanticForms = () => {
if (nodeName !== 'SELECT' && type !== 'checkbox' && type !== 'radio') {
// if it doesn't have a placeholder, add a blank one
if (!input.getAttribute('placeholder')) input.setAttribute('placeholder', ' ')
input.classList.add('semanticform')
inputHandler(input) // force x to appear on inputs with prefilled value
}
input.addEventListener('input', inputHandler)
Expand Down Expand Up @@ -129,3 +130,8 @@ window.semanticForms = () => {
window.semanticFormsObserver.observe(document.body, { attributes: false, childList: true, characterData: false, subtree: true })
}
}

window.semanticForms.reinitialize = (form) => {
form.classList.remove('semanticFormsActive')
window.semanticForms()
}

0 comments on commit 6545cc4

Please sign in to comment.