Skip to content

Commit 827b263

Browse files
committed
Refactor Consent/Checkbox component to extend LitElements
1 parent ea3df99 commit 827b263

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/components/Consent/Checkbox/client/__tests__/testUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export const renderConsentCheckbox = async (
6262
moduleSpecifier: '@components/Consent/Checkbox/client/index',
6363
args: { props: { ...defaultProps, ...props } },
6464
selector: 'consent-checkbox',
65-
waitForReady: waitForConsentReady,
65+
waitForReady: async (element: ConsentCheckboxElement) => {
66+
await waitForConsentReady(element)
67+
await element.updateComplete
68+
},
6669
assert: async ({ element, window, module, renderResult }) => {
6770
if (!window) {
6871
throw new TestError('Consent checkbox tests require a browser-like window environment')

src/components/Consent/Checkbox/client/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Handles validation and state management for GDPR consent checkboxes in forms
55
*/
6+
import { LitElement } from 'lit'
67
import { addScriptBreadcrumb, ClientScriptError } from '@components/scripts/errors'
78
import { handleScriptError } from '@components/scripts/errors/handler'
89
import { defineCustomElement } from '@components/scripts/utils'
@@ -26,7 +27,9 @@ const COMPONENT_TAG_NAME = 'consent-checkbox' as const
2627
const READY_EVENT = 'consent-checkbox:ready'
2728
const COMPONENT_SCRIPT_NAME = 'ConsentCheckboxElement'
2829

29-
export class ConsentCheckboxElement extends HTMLElement {
30+
export class ConsentCheckboxElement extends LitElement {
31+
static registeredName = COMPONENT_TAG_NAME
32+
3033
private domReadyHandler: (() => void) | null = null
3134
private checkbox: HTMLInputElement | null = null
3235
private container: HTMLDivElement | null = null
@@ -42,7 +45,12 @@ export class ConsentCheckboxElement extends HTMLElement {
4245

4346
public isInitialized = false
4447

45-
connectedCallback(): void {
48+
protected override createRenderRoot(): HTMLElement {
49+
return this
50+
}
51+
52+
override connectedCallback(): void {
53+
super.connectedCallback()
4654
if (typeof document === 'undefined' || this.isInitialized) {
4755
return
4856
}
@@ -60,7 +68,8 @@ export class ConsentCheckboxElement extends HTMLElement {
6068
this.initialize()
6169
}
6270

63-
disconnectedCallback(): void {
71+
override disconnectedCallback(): void {
72+
super.disconnectedCallback()
6473
this.cleanupDomReadyHandler()
6574
this.cleanupListeners()
6675
this.isInitialized = false
@@ -363,8 +372,10 @@ export const registerConsentCheckboxWebComponent = (tagName: string = COMPONENT_
363372
defineCustomElement(tagName, ConsentCheckboxElement)
364373
}
365374

375+
export const registerWebComponent = registerConsentCheckboxWebComponent
376+
366377
export const webComponentModule: WebComponentModule<ConsentCheckboxElement> = {
367378
registeredName: COMPONENT_TAG_NAME,
368379
componentCtor: ConsentCheckboxElement,
369-
registerWebComponent: registerConsentCheckboxWebComponent,
380+
registerWebComponent,
370381
}

0 commit comments

Comments
 (0)