adding cancelable custom event on item removal

This commit is contained in:
Ferdinand Kuhl 2024-10-09 12:23:38 +02:00
parent 73cbd36cd1
commit 1aa62b036f
2 changed files with 14 additions and 4 deletions

View file

@ -15,16 +15,26 @@ export function formStamp(element, options = {})
const itemSelector = '.' + template.content.firstElementChild.className.replace(/ /g, '.'); const itemSelector = '.' + template.content.firstElementChild.className.replace(/ /g, '.');
const initialItemCount = container.querySelectorAll(itemSelector).length; const initialItemCount = container.querySelectorAll(itemSelector).length;
let created = 0; let created = 0;
const removeRow = (element) => () => { const removeRow = (removeItem) => () => {
const rowToDelete = element.closest(itemSelector); const rowToDelete = removeItem.closest(itemSelector);
// Flow renders identity fields directly at the form tag, so we need to look // Flow renders identity fields directly at the form tag, so we need to look
if (rowToDelete.dataset.stampRemoveWithMe) { if (rowToDelete.dataset.stampRemoveWithMe) {
container.closest('form') container.closest('form')
.querySelectorAll(rowToDelete.dataset.stampRemoveWithMe) .querySelectorAll(rowToDelete.dataset.stampRemoveWithMe)
.forEach(child => child.remove()) .forEach(child => child.remove())
} }
if (element.dispatchEvent(new CustomEvent(
'beforeStampRemoval',
{
cancelable: true,
detail: {
toBeRemoved: rowToDelete
}
}
))) {
rowToDelete.remove(); rowToDelete.remove();
} }
}
element.addEventListener('click', () => { element.addEventListener('click', () => {
const clone = template.content.cloneNode(true); const clone = template.content.cloneNode(true);

View file

@ -1,6 +1,6 @@
{ {
"name": "@digicomp/form-stamp", "name": "@digicomp/form-stamp",
"version": "0.0.2", "version": "0.0.3",
"description": "duplicates an entry form field group", "description": "duplicates an entry form field group",
"main": "form-stamp.js", "main": "form-stamp.js",
"scripts": { "scripts": {