UNPKG

@taiga-ui/cdk

Version:

Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance

123 lines (117 loc) 5.21 kB
import * as i0 from '@angular/core'; import { inject, signal, ChangeDetectorRef, computed, input, untracked, Directive } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { NgControl, NgModel } from '@angular/forms'; import { EMPTY_FUNCTION } from '@taiga-ui/cdk/constants'; import { TUI_FALLBACK_VALUE } from '@taiga-ui/cdk/tokens'; import { tuiProvide } from '@taiga-ui/cdk/utils/di'; import { identity, Subject, delay, startWith, map, filter, distinctUntilChanged, switchMap, merge } from 'rxjs'; class TuiValueTransformer { } function tuiValueTransformerFrom(token) { return { provide: TuiValueTransformer, useFactory: () => inject(token).valueTransformer, }; } class TuiNonNullableValueTransformer extends TuiValueTransformer { fromControlValue(value) { this.prevValue = value; return value; } toControlValue(value) { this.prevValue = value ?? this.prevValue; return this.prevValue; } } const TUI_IDENTITY_VALUE_TRANSFORMER = { fromControlValue: identity, toControlValue: identity, }; const FLAGS = { self: true, optional: true }; /** * Basic ControlValueAccessor class to build form components upon */ class TuiControl { constructor() { this.fallback = inject(TUI_FALLBACK_VALUE, FLAGS); this.refresh$ = new Subject(); this.internal = signal(this.fallback); this.control = inject(NgControl, { self: true }); this.cdr = inject(ChangeDetectorRef); this.transformer = inject(TuiValueTransformer, FLAGS) ?? TUI_IDENTITY_VALUE_TRANSFORMER; this.value = computed(() => this.internal() ?? this.fallback); this.readOnly = input(false); this.pseudoInvalid = input(undefined, { alias: 'invalid' }); this.touched = signal(false); this.status = signal(undefined); this.disabled = computed(() => this.status() === 'DISABLED'); this.interactive = computed(() => !this.disabled() && !this.readOnly()); this.invalid = computed(() => { const pseudoInvalid = this.pseudoInvalid(); return pseudoInvalid == null ? this.interactive() && this.touched() && this.status() === 'INVALID' : pseudoInvalid && this.interactive(); }); this.mode = computed(() => // eslint-disable-next-line no-nested-ternary this.readOnly() ? 'readonly' : this.invalid() ? 'invalid' : 'valid'); this.onTouched = EMPTY_FUNCTION; this.onChange = EMPTY_FUNCTION; this.control.valueAccessor = this; this.refresh$ .pipe(delay(0), startWith(null), map(() => this.control.control), filter(Boolean), distinctUntilChanged(), switchMap((c) => merge(c.valueChanges, c.statusChanges, c.events).pipe(startWith(null))), takeUntilDestroyed()) .subscribe(() => this.update()); } registerOnChange(onChange) { this.refresh$.next(); this.onChange = (value) => { const internal = untracked(this.internal); if (value === internal) { return; } onChange(this.transformer.toControlValue(value)); this.internal.set(value); this.update(); }; } registerOnTouched(onTouched) { this.onTouched = () => { onTouched(); this.update(); }; } setDisabledState() { this.update(); } writeValue(value) { // TODO: https://github.com/angular/angular/issues/14988 const safe = this.control instanceof NgModel ? this.control.model : value; this.internal.set(this.transformer.fromControlValue(safe)); this.update(); } update() { this.status.set(this.control.control?.status); this.touched.set(!!this.control.control?.touched); this.cdr.markForCheck(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: TuiControl, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "19.2.25", type: TuiControl, isStandalone: true, inputs: { readOnly: { classPropertyName: "readOnly", publicName: "readOnly", isSignal: true, isRequired: false, transformFunction: null }, pseudoInvalid: { classPropertyName: "pseudoInvalid", publicName: "invalid", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: TuiControl, decorators: [{ type: Directive }], ctorParameters: () => [] }); function tuiAsControl(control) { return tuiProvide(TuiControl, control); } class TuiValidationError { constructor(message, context = {}) { this.message = message; this.context = context; } } /** * Generated bundle index. Do not edit. */ export { TUI_IDENTITY_VALUE_TRANSFORMER, TuiControl, TuiNonNullableValueTransformer, TuiValidationError, TuiValueTransformer, tuiAsControl, tuiValueTransformerFrom }; //# sourceMappingURL=taiga-ui-cdk-classes.mjs.map