diff --git a/index.html b/index.html index 718db7c..78821fc 100644 --- a/index.html +++ b/index.html @@ -204,6 +204,86 @@

+
+ + + + + + + + + + + + + + + + + + +
LabelInput
Your todo + + + + +
+ + + + +
+ diff --git a/spec/index.test.ts b/spec/index.test.ts index 9c0ab07..9e0dec6 100644 --- a/spec/index.test.ts +++ b/spec/index.test.ts @@ -44,3 +44,46 @@ describe('#nestedForm', (): void => { expect(target.previousElementSibling.innerHTML).toContain('New todo') }) }) + +describe('#nestedForm tables', (): void => { + beforeEach((): void => { + startStimulus() + + document.body.innerHTML = ` +
+ + + + + + + + + + + + + +
Todo
Your todo
+ + +
+ ` + }) + + it('should add new todo to end of table body', (): void => { + const target: HTMLElement = document.querySelector("[data-nested-form-target='target']") + const addButton: HTMLButtonElement = document.querySelector("[data-action='nested-form#add']") + + // @ts-ignore + expect(target.lastElementChild.innerHTML.trim()).toContain('Your todo') + + addButton.click() + + expect(target.lastElementChild.innerHTML).toContain('New todo') + }) +}) diff --git a/src/index.ts b/src/index.ts index ff9ee77..ca58b89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,12 +4,17 @@ export default class extends Controller { targetTarget: HTMLElement templateTarget: HTMLElement wrapperSelectorValue: string + positionValue: InsertPosition static targets = ['target', 'template'] static values = { wrapperSelector: { type: String, default: '.nested-form-wrapper' + }, + position: { + type: String, + default: 'beforebegin' } } @@ -17,7 +22,7 @@ export default class extends Controller { e.preventDefault() const content: string = this.templateTarget.innerHTML.replace(/NEW_RECORD/g, new Date().getTime().toString()) - this.targetTarget.insertAdjacentHTML('beforebegin', content) + this.targetTarget.insertAdjacentHTML(this.positionValue, content) } remove (e: Event): void {