From faf0c71dd78c9f30646c3dd8776bed8213d111f8 Mon Sep 17 00:00:00 2001 From: Sam Richardson Date: Mon, 30 Oct 2023 15:47:33 -0400 Subject: [PATCH] chore(checkbox): address pr comments --- src/lib/checkbox/checkbox-adapter.ts | 40 ++++++++++------ src/lib/checkbox/checkbox.html | 12 ++--- src/lib/checkbox/checkbox.scss | 1 - .../core/styles/tokens/checkbox/_tokens.scss | 2 +- src/lib/core/utils/reflect-utils.ts | 47 ++++++++++--------- src/lib/list/list/list-adapter.ts | 1 - src/lib/slider/slider.html | 4 +- src/lib/switch/switch-adapter.ts | 16 +++---- 8 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/lib/checkbox/checkbox-adapter.ts b/src/lib/checkbox/checkbox-adapter.ts index 872f8bc18..b293085a6 100644 --- a/src/lib/checkbox/checkbox-adapter.ts +++ b/src/lib/checkbox/checkbox-adapter.ts @@ -1,5 +1,5 @@ import { getShadowElement, toggleAttribute } from '@tylertech/forge-core'; -import { BaseAdapter, IBaseAdapter, INPUT_PROPERTIES, InputAdapter, cloneAttributes, cloneProperties, cloneValidationMessage, forwardAttributes } from '../core'; +import { BaseAdapter, IBaseAdapter, INPUT_PROPERTIES, SlottedElementAdapter, cloneAttributes, cloneProperties, cloneValidationMessage, forwardAttributes } from '../core'; import { StateLayerComponent } from '../state-layer'; import { ICheckboxComponent } from './checkbox'; import { CHECKBOX_CONSTANTS, CheckboxLabelPosition, CheckboxState } from './checkbox-constants'; @@ -28,7 +28,7 @@ export class CheckboxAdapter extends BaseAdapter implements private readonly _labelElement: HTMLElement; private readonly _inputSlotElement: HTMLSlotElement; private readonly _stateLayerElement: StateLayerComponent; - private readonly _inputAdapter: InputAdapter; + private readonly _inputAdapter: SlottedElementAdapter; private _forwardObserver?: MutationObserver; private get _activeInputElement(): HTMLInputElement { @@ -43,19 +43,31 @@ export class CheckboxAdapter extends BaseAdapter implements this._labelElement = getShadowElement(component, CHECKBOX_CONSTANTS.selectors.LABEL); this._inputSlotElement = getShadowElement(component, CHECKBOX_CONSTANTS.selectors.INPUT_SLOT) as HTMLSlotElement; this._stateLayerElement = getShadowElement(component, CHECKBOX_CONSTANTS.selectors.STATE_LAYER) as StateLayerComponent; - this._inputAdapter = new InputAdapter(); + this._inputAdapter = new SlottedElementAdapter(); } public initialize(): void { - this._inputAdapter.initialize(this._inputElement, (newEl, oldEl) => { - if (oldEl) { - cloneAttributes(oldEl, newEl, ['type', 'checked', 'aria-readonly']); - cloneProperties(oldEl, newEl, INPUT_PROPERTIES); - cloneValidationMessage(oldEl, newEl); - } - - this._forwardObserver?.disconnect(); - this._initializeForwardObserver(newEl); + const slottedInput = this._component.querySelector(':is(input[type=checkbox]:not([slot]), [slot=input])') as HTMLInputElement; + if (slottedInput) { + slottedInput.slot = 'input'; + } + this.observeInput(slottedInput ?? this._inputElement); + if (!slottedInput) { + this.initializeInput(); + } + } + + public initializeInput(): void { + this._forwardObserver?.disconnect(); + this._initializeForwardObserver(this._activeInputElement); + } + + public observeInput(el: HTMLInputElement = this._inputElement): void { + this._inputAdapter.initialize(el, (newEl, oldEl) => { + cloneAttributes(oldEl, newEl, ['type', 'checked', 'aria-readonly']); + cloneProperties(oldEl, newEl, INPUT_PROPERTIES); + cloneValidationMessage(oldEl, newEl); + this.initializeInput(); }); } @@ -111,9 +123,9 @@ export class CheckboxAdapter extends BaseAdapter implements public detectInputElement(): void { const inputElement = this._inputSlotElement.assignedElements()[0] as HTMLInputElement; if (inputElement) { - this._inputAdapter.attachInput(inputElement); + this._inputAdapter.attachElement(inputElement); } else { - this._inputAdapter.attachInput(this._inputElement); + this._inputAdapter.attachElement(this._inputElement); } } diff --git a/src/lib/checkbox/checkbox.html b/src/lib/checkbox/checkbox.html index ee26c0083..54bcb2f16 100644 --- a/src/lib/checkbox/checkbox.html +++ b/src/lib/checkbox/checkbox.html @@ -1,19 +1,19 @@