-
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.
dev: Prepare groundwork for custom dialog boxes
(These will be used as replacements for native prompt and alert popups)
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export const createModal = (title = 'Modal Title', inputFields = ['Input A', 'Input B']) => { | ||
document.body | ||
.insertAdjacentHTML("afterbegin", ` | ||
<div id="modalContainer" class="modalContainer"> | ||
<form action="" class="modal"> | ||
<h3 id="modalTitle" class="modalTitle">${title}</h3> | ||
<div id="modalButtonsBar" class="modalButtonsBar"> | ||
<button id="modalSubmitButton" class="mainButton" onclick="" type="button">Submit</button> | ||
<button id="modalCancelButton" class="" onclick="" type="button">Cancel</button> | ||
</div> | ||
<form> | ||
</div> | ||
`) | ||
inputFields.forEach((e) => { | ||
let id = e.replaceAll(' ', '-') | ||
document.getElementById('modalButtonsBar').insertAdjacentHTML("beforebegin", ` | ||
<label class="label" for="${id}">${e}</label> | ||
<input class="inputField" autocomplete="off" type="text" id="${id}"> | ||
`) | ||
}) | ||
|
||
let promise = new Promise((resolve, reject) => { | ||
let modalContainer = document.getElementById('modalContainer') | ||
let modalSubmitButton = document.getElementById('modalSubmitButton') | ||
let modalCancelButton = document.getElementById('modalCancelButton') | ||
let inputFields = document.getElementsByClassName('inputField') | ||
inputFields[0].focus() | ||
|
||
const rejectModal = () => { | ||
modalCancelButton.removeEventListener('click', rejectModal) | ||
modalContainer.remove() | ||
reject('Dialog was closed by the user') | ||
} | ||
|
||
const resolveModal = () => { | ||
modalSubmitButton.removeEventListener('click', resolveModal) | ||
let result = [] | ||
for (const e of inputFields) { | ||
result.push(e.value) | ||
} | ||
modalContainer.remove() | ||
resolve(result) | ||
} | ||
modalCancelButton.addEventListener('click', rejectModal) | ||
modalSubmitButton.addEventListener('click', resolveModal) | ||
}) | ||
|
||
return promise | ||
} |
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