Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(form-core): add a way to prevent registration of a form-element #2247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-windows-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': minor
---

[form-core] add a way to prevent registration of a form-element
4 changes: 4 additions & 0 deletions docs/fundamentals/systems/form/use-cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export const main = () => {
<lion-input-email name="email" label="Email"></lion-input-email>
<lion-input-file name="file" label="File"></lion-input-file>
<lion-input-tel name="tel" label="Telephone number"></lion-input-tel>
<lion-input-tel-dropdown
name="tel-dropdown"
label="Telephone number"
></lion-input-tel-dropdown>
<lion-checkbox-group
label="What do you like?"
name="checkers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const FormRegisteringMixinImplementation = superclass =>
class extends superclass {
constructor() {
super();
/**
* In case the form component should not register itself, set this property to true
*/
this.preventRegistration = false;
/**
* The registrar this FormControl registers to, Usually a descendant of FormGroup or
* ChoiceGroup
Expand All @@ -45,13 +49,15 @@ const FormRegisteringMixinImplementation = superclass =>

connectedCallback() {
super.connectedCallback();
this.dispatchEvent(
new CustomEvent('form-element-register', {
detail: { element: this },
bubbles: true,
composed: Boolean(this.allowCrossRootRegistration),
}),
);
if (!this.preventRegistration) {
this.dispatchEvent(
new CustomEvent('form-element-register', {
detail: { element: this },
bubbles: true,
composed: Boolean(this.allowCrossRootRegistration),
}),
);
}
}

disconnectedCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export const runRegistrationSuite = customConfig => {
expect(eventSpy.getCall(0).args[0].composed).to.equal(true);
expect(el.formElements).to.deep.equal([el.shadowRoot?.querySelector('#child')]);
});

it('dispatches the form-element-register event with compose true if allowCrossRootRegistration is set', async () => {
const eventSpy = sinon.spy();
/** @type {RegisteringClass} */ (
Expand All @@ -300,6 +301,7 @@ export const runRegistrationSuite = customConfig => {
expect(eventSpy).to.have.been.calledOnce;
expect(eventSpy.getCall(0).args[0].composed).to.equal(true);
});

it('dispatches the form-element-register event with compose false if allowCrossRootRegistration is not set', async () => {
const eventSpy = sinon.spy();
/** @type {RegisteringClass} */ (
Expand All @@ -310,7 +312,29 @@ export const runRegistrationSuite = customConfig => {
expect(eventSpy).to.have.been.calledOnce;
expect(eventSpy.getCall(0).args[0].composed).to.equal(false);
});

it('will not dispatch form-element-register event when it has "preventRegistration" set', async () => {
class BlockingFormElement extends FormRegisteringMixin(LitElement) {
constructor() {
super();
this.preventRegistration = true;
}
}
const tagBlockingChildString = defineCE(BlockingFormElement);
const blockingChildTag = unsafeStatic(tagBlockingChildString);
const eventSpy = sinon.spy();
/** @type {RegisteringClass} */ (
await fixture(html`
<${blockingChildTag}
@form-element-register=${eventSpy}
>
</${blockingChildTag}>
`)
);
expect(eventSpy).not.to.have.been.calledOnce;
});
});

describe('FormRegistrarPortalMixin', () => {
it('forwards registrations to the .registrationTarget', async () => {
const el = /** @type {RegistrarClass} */ (
Expand Down
Loading