feat(fieldset): add clearGroup method (#723)

This commit is contained in:
realfighter92 2020-05-27 14:13:16 +05:30 committed by GitHub
parent 674abf7adc
commit 8314b1acc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 0 deletions

View file

@ -229,6 +229,18 @@ export const FormGroupMixin = dedupeMixin(
this.resetInteractionState(); 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() { resetInteractionState() {
this.submitted = false; this.submitted = false;
this.touched = false; this.touched = false;

View file

@ -1010,6 +1010,51 @@ describe('<lion-fieldset>', () => {
expect(resetSpy.callCount).to.equal(1); 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'}">
</${childTag}>
</${tag}>
</${tag}>
`);
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'}">
</${childTag}>
</${tag}>
</${tag}>
`);
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'}">
</${childTag}>
</${tag}>
</${tag}>
`);
el.clearGroup();
expect(el.querySelector('[name="child"]').modelValue).to.equal('');
});
});
}); });
describe('Accessibility', () => { describe('Accessibility', () => {

View file

@ -101,6 +101,47 @@ This button is only used here to demonstrate the `serializeGroup()` method.
</Story> </Story>
</Preview> </Preview>
# clearGroup of form
To clear the values of the values of the fields use clearGroup().
<Preview>
<Story name="clearGroup">
{() => {
return html`
<lion-form id="form3"
><form>
<lion-fieldset label="Name" name="name">
<lion-input
name="firstName"
label="First Name"
.modelValue=${'Foo'}
>
</lion-input>
<lion-input
name="lastName"
label="Last Name"
>
</lion-input>
</lion-fieldset>
<button type="button" @click=${() => {
document.querySelector('#form3').clearGroup();
const form = document.querySelector('#form3');
document.getElementById('form3_output').innerText = JSON.stringify(form.serializedValue);
}}>
Clear
</button>
<pre id="form3_output">
</pre>
</form></lion-form
>
`;
}}
</Story>
</Preview>
## Serialize in a multistep form ## Serialize in a multistep form