From 1aa62b036f222025a6667dfdc96a963b048da14a Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Wed, 9 Oct 2024 12:23:38 +0200 Subject: [PATCH] adding cancelable custom event on item removal --- form-stamp.js | 16 +++++++++++++--- package.json | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/form-stamp.js b/form-stamp.js index 481d737..34d1992 100644 --- a/form-stamp.js +++ b/form-stamp.js @@ -15,15 +15,25 @@ export function formStamp(element, options = {}) const itemSelector = '.' + template.content.firstElementChild.className.replace(/ /g, '.'); const initialItemCount = container.querySelectorAll(itemSelector).length; let created = 0; - const removeRow = (element) => () => { - const rowToDelete = element.closest(itemSelector); + const removeRow = (removeItem) => () => { + const rowToDelete = removeItem.closest(itemSelector); // Flow renders identity fields directly at the form tag, so we need to look if (rowToDelete.dataset.stampRemoveWithMe) { container.closest('form') .querySelectorAll(rowToDelete.dataset.stampRemoveWithMe) .forEach(child => child.remove()) } - rowToDelete.remove(); + if (element.dispatchEvent(new CustomEvent( + 'beforeStampRemoval', + { + cancelable: true, + detail: { + toBeRemoved: rowToDelete + } + } + ))) { + rowToDelete.remove(); + } } element.addEventListener('click', () => { diff --git a/package.json b/package.json index 6b5727e..8c44091 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@digicomp/form-stamp", - "version": "0.0.2", + "version": "0.0.3", "description": "duplicates an entry form field group", "main": "form-stamp.js", "scripts": {