import { ChangeDetectionStrategy, Component, booleanAttribute, computed, forwardRef, input, output, } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { injectPassThrough, injectTokens, injectUnstyled, TxPassThrough, TxPassThroughOptions } from '@tectra-ng/core/config'; import { injectFluid } from '@tectra-ng/core/fluid'; import { TxRipple } from '@tectra-ng/core/ripple'; import { TxFormControlBase } from '@tectra-ng/core/forms'; import { optionalBoolean, optionalNumber } from '@tectra-ng/core/utils'; import { injectTxIcons, TxIcon, TxIconName } from '@tectra-ng/icons'; import { TectraDeepPartial, TectraToggleButtonTokens, toStyleObject } from '@tectra-ng/themes'; import { TxToggleButtonChangeEvent, TxToggleButtonIconPosition, TxToggleButtonPart, TxToggleButtonSize, } from './toggle-button.types'; import { TOGGLE_BUTTON_THEME } from './toggle-button.theme'; @Component({ selector: 'tx-toggle-button', imports: [TxIcon, TxRipple], templateUrl: './toggle-button.component.html', styleUrl: './toggle-button.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => TxToggleButton), multi: true }], host: { class: 'tx-toggle-button', '[class]': 'passThrough().root?.class', '[style]': 'hostStyle()', '[attr.data-styled]': '!isUnstyled() || null', '[attr.data-size]': 'size()', '[attr.data-icon-pos]': 'iconPos()', '[class.tx-toggle-button--checked]': 'checked()', '[class.tx-toggle-button--disabled]': 'isDisabled()', '[class.tx-toggle-button--invalid]': 'invalid()', '[class.tx-toggle-button--fluid]': 'isFluid()', }, }) export class TxToggleButton extends TxFormControlBase { private readonly toggleButtonTheme = injectTokens(TOGGLE_BUTTON_THEME); private txIconRegistry = injectTxIcons(); readonly onLabel = input('Yes'); readonly offLabel = input('No'); readonly onIcon = input(undefined); readonly offIcon = input(undefined); readonly iconPos = input('left'); readonly size = input('medium'); readonly allowEmpty = input(true, { transform: booleanAttribute }); readonly invalid = input(false, { transform: booleanAttribute }); readonly fluid = input(undefined, { transform: optionalBoolean }); // eslint-disable-next-line @angular-eslint/no-input-rename readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute }); readonly tabindex = input(undefined, { transform: optionalNumber }); readonly ariaLabel = input(undefined); readonly dt = input | undefined>(undefined); readonly pt = input | undefined>(undefined); readonly ptOptions = input(undefined); readonly unstyled = input(undefined, { transform: optionalBoolean }); changed = output(); isUnstyled = injectUnstyled(this.unstyled); isFluid = injectFluid(this.fluid); passThrough = injectPassThrough('toggleButton', this, this.pt, this.ptOptions); readonly checked = computed(() => !!this.value()); readonly isDisabled = computed(() => this.disabled() || this.disabledInput()); readonly label = computed(() => (this.checked() ? this.onLabel() : this.offLabel())); readonly currentIcon = computed(() => (this.checked() ? this.onIcon() : this.offIcon())); readonly iconName = computed(() => { const icon = this.currentIcon(); return this.txIconRegistry.has(icon) ? icon : null; }); readonly hostStyle = computed(() => ({ ...toStyleObject(this.dt(), '--tx-toggle-button'), ...this.passThrough().root?.style })); toggle(event: Event): void { if (this.isDisabled() || (this.checked() && !this.allowEmpty())) { return; } const checked = !this.checked(); this.updateValue(checked); this.changed.emit({ originalEvent: event, checked }); } onBlur(): void { this.markTouched(); } }