Skip to content

Commit

Permalink
fix(form): form element should emit her own odsChange event
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Esteves <[email protected]>
  • Loading branch information
aesteves60 authored and dpellier committed Nov 28, 2024
1 parent 94785aa commit 3092b76
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class OdsPassword {
}

private async onOdsChange(event: OdsInputChangeEvent): Promise<void> {
event.stopPropagation();
this.value = event.detail.value?.toString() ?? null;
await updateInternals(this.internals, this.value, this.odsInput);

Expand All @@ -127,6 +128,13 @@ export class OdsPassword {
this.isInvalid = !this.internals.validity.valid;
this.shouldUpdateIsInvalidState = false;
}

this.odsChange.emit({
name: this.name,
previousValue: event.detail.previousValue,
validity: this.internals.validity,
value: this.value,
});
}

private async onOdsInvalid(event: CustomEvent<boolean>): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,28 @@ describe('ods-password validity', () => {
});
});
});

describe('event', () => {
describe('odsChange', () => {
it('should have a valid validity change after odsChange', async() => {
let isValidityChecked: boolean | undefined = undefined;
await setup('<ods-password is-required></ods-password>');
await page.exposeFunction('onOdsChange', async() => {
isValidityChecked = await el.callMethod('getValidity');
});
await page.evaluate(() => {
const password = document.querySelector('ods-password');
// @ts-ignore function is exposed internally in the page
password?.addEventListener('odsChange', (e) => onOdsChange(e));
});

expect(await el.callMethod('checkValidity')).toBe(false);

await el.type('a');
await page.waitForChanges();

expect(isValidityChecked).toBe(true);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class OdsPhoneNumber {
}

private async onOdsChange(event: OdsInputChangeEvent): Promise<void> {
event.stopImmediatePropagation();
event.stopPropagation();

this.value = event.detail.value as string | null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export class OdsQuantity {
}

private async onOdsChange(event: OdsInputChangeEvent): Promise<void> {
event.stopPropagation();

if (event.detail.value === null || event.detail.value === '') {
this.value = null;
} else {
Expand All @@ -145,6 +147,13 @@ export class OdsQuantity {
await this.odsInput?.checkValidity();
this.isInvalid = !this.internals.validity.valid;
}

this.odsChange.emit({
name: this.name,
previousValue: Number(event.detail.previousValue),
validity: this.internals.validity,
value: this.value,
});
}

private async onOdsInvalid(event: CustomEvent<boolean>): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ export class OdsSwitchItem {
case 'is-required':
this.isRequired = this.el.getAttribute('is-required') === '';
break;
case 'required':
this.odsInvalid.emit(!this.inputEl?.validity.valid);
break;
default:
break;
}
Expand All @@ -106,11 +103,6 @@ export class OdsSwitchItem {
attributeFilter: ['input-id', 'is-disabled', 'is-required'],
attributes: true,
});

this.inputEl && this.observer?.observe(this.inputEl, {
attributeFilter: ['required'],
attributes: true,
});
}

disconnectedCallback(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export class OdsTimepicker {
}

private async onOdsChange(event: OdsInputChangeEvent | OdsSelectChangeEvent, isFromSelect: boolean): Promise<void> {
event.preventDefault();
event.stopPropagation();

if (isFromSelect) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { E2EElement, E2EPage } from '@stencil/core/testing';
import { newE2EPage } from '@stencil/core/testing';

describe('ods-input navigation', () => {
describe('ods-toggle navigation', () => {
let el: E2EElement;
let page: E2EPage;

Expand Down

0 comments on commit 3092b76

Please sign in to comment.