From d101d4a2c21f82a48dc47983d3dd3239e1735e06 Mon Sep 17 00:00:00 2001 From: jholt1 Date: Tue, 6 Feb 2024 12:25:31 +0000 Subject: [PATCH 1/2] fix: switch to loop --- .../components/form/src/form-component.js | 19 ++++++++++------ .../form/__snapshots__/form.test.snap.js | 6 +++++ .../muon/tests/components/form/form.test.js | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/muon/components/form/src/form-component.js b/packages/muon/components/form/src/form-component.js index 69511a07..c7c715b6 100644 --- a/packages/muon/components/form/src/form-component.js +++ b/packages/muon/components/form/src/form-component.js @@ -140,15 +140,20 @@ export class Form extends MuonElement { } _findInputElement(element) { - if (element.parentElement._inputElement) { - return element.parentElement; - } - // Due to any layout container elements - @TODO - need better logic - if (element.parentElement.parentElement._inputElement) { - return element.parentElement.parentElement; + const limit = 10; + let count = 0; + let parentElement = element.parentElement; + + while (parentElement && !parentElement?._inputElement) { + parentElement = parentElement.parentElement; + count += 1; + + if (count >= limit) { + break; + } } - return element; + return parentElement?._inputElement ? parentElement : element; } validate() { diff --git a/packages/muon/tests/components/form/__snapshots__/form.test.snap.js b/packages/muon/tests/components/form/__snapshots__/form.test.snap.js index 3c6ad554..8ffe6833 100644 --- a/packages/muon/tests/components/form/__snapshots__/form.test.snap.js +++ b/packages/muon/tests/components/form/__snapshots__/form.test.snap.js @@ -113,3 +113,9 @@ snapshots["form form cta reset with inputter"] = `; /* end snapshot form form cta reset with inputter */ +snapshots["form form input is too nested"] = +` + +`; +/* end snapshot form form input is too nested */ + diff --git a/packages/muon/tests/components/form/form.test.js b/packages/muon/tests/components/form/form.test.js index 256356a3..21aa840d 100644 --- a/packages/muon/tests/components/form/form.test.js +++ b/packages/muon/tests/components/form/form.test.js @@ -336,4 +336,26 @@ describe('form', () => { resetBtn.click(); expect(input.value).to.equal('test', 'no reset input value'); }); + + it('form input is too nested', async () => { + const el = await fixture(html` + <${tag}> +
+ <${inputterTag} validation='["isRequired"]' value='test'> +
+ + +
+ + <${ctaTag} type="submit">Submit +
+ + `); + await defaultChecks(el); + + expect(el._elements).to.have.lengthOf(2); + expect(el._elements[0].nodeName).to.equal('INPUT'); + + expect(el._findInputElement(el._elements[0])).to.equal(el._elements[0]); + }); }); From 4f2fadb06e542548054b92db52dfbbc2bbebb788 Mon Sep 17 00:00:00 2001 From: jholt1 Date: Tue, 6 Feb 2024 13:23:54 +0000 Subject: [PATCH 2/2] chore: add comment --- packages/muon/components/form/src/form-component.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/muon/components/form/src/form-component.js b/packages/muon/components/form/src/form-component.js index c7c715b6..5c30cd22 100644 --- a/packages/muon/components/form/src/form-component.js +++ b/packages/muon/components/form/src/form-component.js @@ -139,6 +139,7 @@ export class Form extends MuonElement { this.querySelector('*:not([hidden])[type="reset"]'); } + // TODO: Decide a better way to find the input element _findInputElement(element) { const limit = 10; let count = 0;