-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle conditionally required fields (#11)
- Loading branch information
1 parent
ac28475
commit cff083b
Showing
5 changed files
with
93 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import * as Send from "/modules/send.js"; | ||
import * as HandleDependencies from "/modules/dependent-input.js"; | ||
import * as ConditionalInput from "/modules/conditional-input.js"; | ||
|
||
const form = document.querySelector("form"); | ||
const output = document.querySelector("output"); | ||
|
||
Send.initialize(form, output); | ||
HandleDependencies.initialize(form); | ||
ConditionalInput.initialize(form); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* HTML requirements: | ||
For triggers: | ||
- Must be an <input> | ||
- Must have class js-conditional | ||
- Must have a name | ||
- Must have a value | ||
For followers: | ||
- Must not be an input | ||
- Must have class js-conditional | ||
- Must have data attribute with name and value corresponding to intended trigger | ||
Triggers and followers must be in the same form. | ||
*/ | ||
|
||
let targetClass = "js-conditional"; | ||
let form; | ||
let triggers, followers; | ||
let requiredStorage = []; | ||
|
||
function initialize(f) { | ||
form = f; | ||
|
||
triggers = form.querySelectorAll(`input.${targetClass}`); | ||
followers = form.querySelectorAll(`.${targetClass}:not(input)`); | ||
|
||
triggers.forEach((trigger) => { | ||
// consider initial states | ||
if (trigger.checked) { | ||
updateFollowers(trigger.name, trigger.value); | ||
} | ||
|
||
// update on change | ||
trigger.addEventListener("change", () => { | ||
updateFollowers(trigger.name, trigger.value, trigger.checked); | ||
}); | ||
}); | ||
} | ||
|
||
function updateFollowers(key, value, checked = true) { | ||
followers.forEach((follower) => { | ||
if (follower.dataset[key]) { | ||
if (checked && follower.dataset[key] == value) { | ||
follower.removeAttribute("hidden"); | ||
recallRequired(follower); | ||
} else { | ||
follower.setAttribute("hidden", ""); | ||
storeRequired(follower); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/* | ||
hidden, required elements are still considered in validation, | ||
so we need to make them un-required when we hide them | ||
*/ | ||
function storeRequired(element) { | ||
if (element.required) { | ||
requiredStorage.push(element); | ||
element.removeAttribute("required"); | ||
} | ||
|
||
let children = Array.from(element.children); | ||
|
||
children.forEach((child) => { | ||
storeRequired(child); | ||
}); | ||
} | ||
|
||
function recallRequired(element) { | ||
if (requiredStorage.includes(element)) { | ||
element.setAttribute("required", ""); | ||
requiredStorage = requiredStorage.filter((x) => x !== element); | ||
} | ||
|
||
let children = Array.from(element.children); | ||
|
||
children.forEach((child) => { | ||
recallRequired(child); | ||
}); | ||
} | ||
|
||
export { initialize }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters