Skip to content

Commit

Permalink
feat(checkbox): update validity state on isRequired change
Browse files Browse the repository at this point in the history
  • Loading branch information
dpellier committed Nov 28, 2024
1 parent 55d1d0c commit f787a42
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ function FormNative(): ReactElement {
- All fields have default value: {withDefaultValue.toString()}
</p>

{/*<div>*/}
{/* /!* OKish no validity method but required is managed by browser directly *!/*/}
{/* <OdsCheckbox*/}
{/* // isRequired={ true }*/}
{/* inputId="checkbox"*/}
{/* name="checkbox"*/}
{/* value="checkbox"*/}
{/* ref={ checkboxRef }*/}
{/* />*/}
{/* <label htmlFor="checkbox">Checked</label>*/}

{/* <OdsCheckbox*/}
{/* // isRequired={ true }*/}
{/* inputId="checkbox2"*/}
{/* name="checkbox"*/}
{/* value="checkbox2"*/}
{/* />*/}
{/* <label htmlFor="checkbox2">Checked 2</label>*/}
{/*</div>*/}
<div>
{/* OKish no validity method but required is managed by browser directly */}
<OdsCheckbox
isRequired={ areAllRequired }
inputId="checkbox"
name="checkbox"
value="checkbox"
/>
<label htmlFor="checkbox">Checked</label>

<OdsCheckbox
isRequired={ areAllRequired }
inputId="checkbox2"
name="checkbox"
value="checkbox2"
/>
<label htmlFor="checkbox2">Checked 2</label>
</div>

{/*/!* KO? reset to "" instead of null *!/*/}
{/*<OdsDatepicker*/}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type OdsCheckboxChangeEventDetail } from '../../interfaces/event';
})
export class OdsCheckbox {
private inputEl?: HTMLInputElement;
private observer?: MutationObserver;

@State() private isInvalid: boolean = false;

Expand Down Expand Up @@ -85,6 +86,29 @@ export class OdsCheckbox {
return this.inputEl?.willValidate;
}

componentWillLoad(): void {
this.observer = new MutationObserver((mutations: MutationRecord[]) => {
for (const mutation of mutations) {
if (mutation.attributeName === 'required') {
this.isInvalid = !this.inputEl?.validity.valid;
}
}
});
}

componentDidLoad(): void {
if (this.inputEl) {
this.observer?.observe(this.inputEl, {
attributeFilter: ['required'],
attributeOldValue: false,
});
}
}

disconnectedCallback(): void {
this.observer?.disconnect();
}

async formResetCallback(): Promise<void> {
await this.reset();
}
Expand Down Expand Up @@ -118,7 +142,8 @@ export class OdsCheckbox {

render(): FunctionalComponent {
return (
<Host class="ods-checkbox"
<Host
class="ods-checkbox"
disabled={ this.isDisabled }>
<input
aria-label={ this.ariaLabel }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,24 @@ describe('ods-checkbox validity', () => {
expect(formValidity).toBe(true);
});
});

describe('watchers', () => {
describe('is-required', () => {
it('should update validity when is-required change', async() => {
await setup('<ods-checkbox is-required></ods-checkbox>');

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

await el.removeAttribute('is-required');
await page.waitForChanges();

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

await el.setAttribute('is-required', 'true');
await page.waitForChanges();

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

0 comments on commit f787a42

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy