import { NgTemplateOutlet } from '@angular/common'; import { ChangeDetectionStrategy, Component, ElementRef, booleanAttribute, computed, contentChild, forwardRef, input, output, viewChild, } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { uniqueId } from '@tectra-ng/core/a11y'; import { injectPassThrough, injectTokens, injectUnstyled, TxPassThrough, TxPassThroughOptions } from '@tectra-ng/core/config'; import { TxFormControlBase, injectFieldInvalid } from '@tectra-ng/core/forms'; import { optionalBoolean, optionalNumber } from '@tectra-ng/core/utils'; import { injectTxIcons, TxIcon, TxIconName } from '@tectra-ng/icons'; import { TectraDeepPartial, TectraToggleSwitchTokens, toStyleObject } from '@tectra-ng/themes'; import { TxToggleSwitchHandleTemplate } from './toggle-switch-templates.directive'; import { TxToggleSwitchChangeEvent, TxToggleSwitchLabelPosition, TxToggleSwitchPart, TxToggleSwitchSize } from './toggle-switch.types'; import { TOGGLE_SWITCH_THEME } from './toggle-switch.theme'; @Component({ selector: 'tx-toggle-switch', imports: [NgTemplateOutlet, TxIcon], templateUrl: './toggle-switch.component.html', styleUrl: './toggle-switch.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => TxToggleSwitch), multi: true }], host: { class: 'tx-toggle-switch', '[class]': 'passThrough().root?.class', '[style]': 'hostStyle()', '[attr.data-styled]': '!isUnstyled() || null', '[attr.data-size]': 'size()', '[attr.data-label-position]': 'labelPosition()', '[class.tx-toggle-switch--checked]': 'checked()', '[class.tx-toggle-switch--disabled]': 'isDisabled()', '[class.tx-toggle-switch--readonly]': 'readonly()', '[class.tx-toggle-switch--invalid]': 'isInvalid()', }, }) export class TxToggleSwitch extends TxFormControlBase { private readonly toggleSwitchTheme = injectTokens(TOGGLE_SWITCH_THEME); private txIconRegistry = injectTxIcons(); private generatedId = uniqueId('tx-toggle-switch'); readonly label = input(undefined); readonly description = input(undefined); readonly labelPosition = input('end'); readonly trueValue = input(true); readonly falseValue = input(false); readonly onIcon = input(undefined); readonly offIcon = input(undefined); readonly size = input('medium'); readonly invalid = input(undefined, { transform: optionalBoolean }); // eslint-disable-next-line @angular-eslint/no-input-rename readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute }); readonly readonly = input(false, { transform: booleanAttribute }); readonly required = input(false, { transform: booleanAttribute }); readonly name = input(undefined); readonly inputId = input(undefined); readonly ariaLabel = input(undefined); readonly ariaLabelledBy = input(undefined); readonly tabindex = input(undefined, { transform: optionalNumber }); readonly dt = input | undefined>(undefined); readonly pt = input | undefined>(undefined); readonly ptOptions = input(undefined); readonly unstyled = input(undefined, { transform: optionalBoolean }); readonly changed = output(); readonly handleTemplate = contentChild(TxToggleSwitchHandleTemplate); readonly inputRef = viewChild.required>('input'); readonly isUnstyled = injectUnstyled(this.unstyled); readonly passThrough = injectPassThrough('toggleSwitch', this, this.pt, this.ptOptions); readonly isInvalid = injectFieldInvalid(this.invalid); readonly resolvedId = computed(() => this.inputId() ?? this.generatedId); readonly descriptionId = `${this.generatedId}-description`; readonly isDisabled = computed(() => this.disabled() || this.disabledInput()); readonly checked = computed(() => this.value() === this.trueValue()); readonly handleIcon = computed(() => (this.checked() ? this.onIcon() : this.offIcon())); readonly handleIconName = computed(() => { const icon = this.handleIcon(); return icon && this.txIconRegistry.has(icon) ? icon : null; }); readonly hostStyle = computed(() => ({ ...toStyleObject(this.dt(), '--tx-toggle-switch'), ...this.passThrough().root?.style })); override writeValue(value: unknown): void { super.writeValue(value === undefined || value === null ? this.falseValue() : value); } focus(): void { this.inputRef().nativeElement.focus(); } onInputClick(event: MouseEvent): void { if (this.readonly()) { event.preventDefault(); } } onInputChange(event: Event): void { const checked = (event.target as HTMLInputElement).checked; const value = checked ? this.trueValue() : this.falseValue(); this.updateValue(value); this.changed.emit({ checked, value, originalEvent: event }); } onBlur(): void { this.markTouched(); } }