First version of form-stamp

This commit is contained in:
Ferdinand Kuhl 2024-06-05 02:27:56 +02:00
parent 227253269b
commit d805864b4b
3 changed files with 86 additions and 0 deletions

19
LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2024 Ferdinand Kuhl <f.kuhl@digital-competence.de>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

44
form-stamp.js Normal file
View file

@ -0,0 +1,44 @@
/* jshint esversion: 6 */
const defaults = {
nameToReplace: 'idx',
removeSelector: '.remove'
}
export function formStamp(element, options = {})
{
const {
nameToReplace,
removeSelector
} = { ...defaults, ...element.dataset, ...options };
const container = element.parentElement;
const template = element.querySelector('template');
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);
// 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();
}
element.addEventListener('click', () => {
const clone = template.content.cloneNode(true);
clone.querySelectorAll('input, select, textarea').forEach(child => {
child.name = child.name.replace(nameToReplace, initialItemCount + created);
});
clone.querySelectorAll(removeSelector).forEach(child => {
child.onclick = removeRow(child);
});
element.before(clone);
created++;
});
container.querySelectorAll(removeSelector).forEach(child => {
child.onclick = removeRow(child);
});
}
export default formStamp;

23
package.json Normal file
View file

@ -0,0 +1,23 @@
{
"name": "@digicomp/form-stamp",
"version": "0.0.1",
"description": "duplicates an entry form field group",
"main": "form-stamp.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@digital-competence.de:npm/form-stamp.git"
},
"keywords": [
"form"
],
"author": "Ferdinand Kuhl",
"license": "MIT",
"dependencies": {
},
"engines": {
}
}