Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Apr 17, 2024
1 parent 7ba9579 commit 8543d78
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions assets/js/input-duplicator.js → assets/js/input-duplicator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { assertNotNull } from './utils/assert';
import { $, $$, disableEl, enableEl, removeEl } from './utils/dom';
import { delegate, leftClick } from './utils/events';

/**
* @typedef InputDuplicatorOptions
* @property {string} addButtonSelector
* @property {string} fieldSelector
* @property {string} maxInputCountSelector
* @property {string} removeButtonSelector
*/

/**
* @param {InputDuplicatorOptions} options
*/
function inputDuplicatorCreator({
export interface InputDuplicatorOptions {
addButtonSelector: string;
fieldSelector: string;
maxInputCountSelector: string;
removeButtonSelector: string;
}

export function inputDuplicatorCreator({
addButtonSelector,
fieldSelector,
maxInputCountSelector,
removeButtonSelector
}) {
const addButton = $(addButtonSelector);
}: InputDuplicatorOptions) {
const addButton = $<HTMLButtonElement>(addButtonSelector);
if (!addButton) {
return;
}

const form = addButton.closest('form');
const fieldRemover = (event, target) => {
const form = assertNotNull(addButton.closest('form'));
const fieldRemover = (event: MouseEvent, target: HTMLElement) => {
event.preventDefault();

// Prevent removing the final field element to not "brick" the form
Expand All @@ -33,7 +30,7 @@ function inputDuplicatorCreator({
return;
}

removeEl(target.closest(fieldSelector));
removeEl(assertNotNull(target.closest<HTMLElement>(fieldSelector)));
enableEl(addButton);
};

Expand All @@ -42,34 +39,40 @@ function inputDuplicatorCreator({
});


const maxOptionCount = parseInt($(maxInputCountSelector, form).innerHTML, 10);
const maxOptionCountElement = assertNotNull($(maxInputCountSelector, form));
const maxOptionCount = parseInt(maxOptionCountElement.innerHTML, 10);

addButton.addEventListener('click', e => {
e.preventDefault();

const existingFields = $$(fieldSelector, form);
const existingFields = $$<HTMLElement>(fieldSelector, form);
let existingFieldsLength = existingFields.length;

if (existingFieldsLength < maxOptionCount) {
// The last element matched by the `fieldSelector` will be the last field, make a copy
const prevField = existingFields[existingFieldsLength - 1];
const prevFieldCopy = prevField.cloneNode(true);
const prevFieldCopyInputs = $$('input', prevFieldCopy);
prevFieldCopyInputs.forEach(prevFieldCopyInput => {
const prevFieldCopy = prevField.cloneNode(true) as HTMLElement;

$$<HTMLInputElement>('input', prevFieldCopy).forEach(prevFieldCopyInput => {
// Reset new input's value
prevFieldCopyInput.value = '';
prevFieldCopyInput.removeAttribute('value');

// Increment sequential attributes of the input
['name', 'id'].forEach(attr => {
prevFieldCopyInput.setAttribute(attr, prevFieldCopyInput[attr].replace(/\d+/g, `${existingFieldsLength}`));
});
prevFieldCopyInput.setAttribute('name', prevFieldCopyInput.name.replace(/\d+/g, `${existingFieldsLength}`));
prevFieldCopyInput.setAttribute('id', prevFieldCopyInput.id.replace(/\d+/g, `${existingFieldsLength}`));
});

const parentNode = assertNotNull(prevField.parentNode);

Check failure on line 67 in assets/js/input-duplicator.ts

View workflow job for this annotation

GitHub Actions / JavaScript Linting and Unit Tests

Trailing spaces not allowed
// Insert copy before the last field's next sibling, or if none, at the end of its parent
if (prevField.nextElementSibling) {
prevField.parentNode.insertBefore(prevFieldCopy, prevField.nextElementSibling);
parentNode.insertBefore(prevFieldCopy, prevField.nextElementSibling);
}
else {
prevField.parentNode.appendChild(prevFieldCopy);
parentNode.appendChild(prevFieldCopy);
}

existingFieldsLength++;
}

Expand All @@ -79,5 +82,3 @@ function inputDuplicatorCreator({
}
});
}

export { inputDuplicatorCreator };

0 comments on commit 8543d78

Please sign in to comment.