From 8314b1acc24c979e4fc9f53dc0c4b352ff3604fa Mon Sep 17 00:00:00 2001 From: realfighter92 <63035357+realfighter92@users.noreply.github.com> Date: Wed, 27 May 2020 14:13:16 +0530 Subject: [PATCH] feat(fieldset): add clearGroup method (#723) --- packages/fieldset/src/FormGroupMixin.js | 12 +++++ packages/fieldset/test/lion-fieldset.test.js | 45 +++++++++++++++++++ .../stories/form-examples.stories.mdx | 41 +++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/packages/fieldset/src/FormGroupMixin.js b/packages/fieldset/src/FormGroupMixin.js index 4fc46997a..6ee2af82b 100644 --- a/packages/fieldset/src/FormGroupMixin.js +++ b/packages/fieldset/src/FormGroupMixin.js @@ -229,6 +229,18 @@ export const FormGroupMixin = dedupeMixin( this.resetInteractionState(); } + clearGroup() { + this.formElements.forEach(child => { + if (typeof child.clearGroup === 'function') { + child.clearGroup(); + } else if (typeof child.clear === 'function') { + child.clear(); + } + }); + + this.resetInteractionState(); + } + resetInteractionState() { this.submitted = false; this.touched = false; diff --git a/packages/fieldset/test/lion-fieldset.test.js b/packages/fieldset/test/lion-fieldset.test.js index 26bc9956c..7f8f0a2b8 100644 --- a/packages/fieldset/test/lion-fieldset.test.js +++ b/packages/fieldset/test/lion-fieldset.test.js @@ -1010,6 +1010,51 @@ describe('', () => { expect(resetSpy.callCount).to.equal(1); }); }); + + describe('clearGroup method', () => { + it('calls clearGroup on children fieldset', async () => { + const el = await fixture(html` + <${tag} name="parentFieldset"> + <${tag} name="childFieldset"> + <${childTag} name="child[]" .modelValue="${'foo1'}"> + + + + `); + const childFieldsetEl = el.querySelector(tagString); + const clearGroupSpy = sinon.spy(childFieldsetEl, 'clearGroup'); + el.clearGroup(); + expect(clearGroupSpy.callCount).to.equal(1); + }); + + it('calls clear on children fields', async () => { + const el = await fixture(html` + <${tag} name="parentFieldset"> + <${tag} name="childFieldset"> + <${childTag} name="child[]" .modelValue="${'foo1'}"> + + + + `); + const childFieldsetEl = el.querySelector(childTagString); + const clearSpy = sinon.spy(childFieldsetEl, 'clear'); + el.clearGroup(); + expect(clearSpy.callCount).to.equal(1); + }); + + it('should clear the value of fields', async () => { + const el = await fixture(html` + <${tag} name="parentFieldset"> + <${tag} name="childFieldset"> + <${childTag} name="child" .modelValue="${'foo1'}"> + + + + `); + el.clearGroup(); + expect(el.querySelector('[name="child"]').modelValue).to.equal(''); + }); + }); }); describe('Accessibility', () => { diff --git a/packages/form-system/stories/form-examples.stories.mdx b/packages/form-system/stories/form-examples.stories.mdx index 7a3a85f0d..f5f26d27b 100644 --- a/packages/form-system/stories/form-examples.stories.mdx +++ b/packages/form-system/stories/form-examples.stories.mdx @@ -101,6 +101,47 @@ This button is only used here to demonstrate the `serializeGroup()` method. +# clearGroup of form + +To clear the values of the values of the fields use clearGroup(). + + + + {() => { + return html` +
+ + + + + + + + +
+          
+
+ `; + }} +
+
+ + ## Serialize in a multistep form