UNPKG

@taiga-ui/cdk

Version:

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

50 lines (46 loc) 2.48 kB
import * as i0 from '@angular/core'; import { Directive, Input } from '@angular/core'; import { NG_VALIDATORS } from '@angular/forms'; import { tuiZonefree, tuiTakeUntilDestroyed } from '@taiga-ui/cdk/observables'; import { tuiInjectElement, tuiProvide } from '@taiga-ui/cdk/utils'; import { BehaviorSubject, switchMap, of, delay } from 'rxjs'; class TuiNativeValidator { constructor() { this.el = tuiInjectElement(); this.control$ = new BehaviorSubject(null); this.sub = this.control$ .pipe(switchMap((control) => control?.events || of(null)), delay(0), tuiZonefree(), tuiTakeUntilDestroyed()) .subscribe(() => this.handleValidation()); this.tuiNativeValidator = 'Invalid'; } validate(control) { this.control$.next(control); return null; } handleValidation() { const invalid = !!this.control$.value?.touched && this.control$.value?.invalid; // TODO: Replace with :has(:invalid) when supported this.el.closest('tui-textfield')?.classList.toggle('tui-invalid', invalid); this.el.setCustomValidity?.(invalid ? this.tuiNativeValidator : ''); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiNativeValidator, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: TuiNativeValidator, isStandalone: true, selector: "[tuiNativeValidator]", inputs: { tuiNativeValidator: "tuiNativeValidator" }, host: { listeners: { "focusout": "handleValidation()" } }, providers: [tuiProvide(NG_VALIDATORS, TuiNativeValidator, true)], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiNativeValidator, decorators: [{ type: Directive, args: [{ standalone: true, selector: '[tuiNativeValidator]', providers: [tuiProvide(NG_VALIDATORS, TuiNativeValidator, true)], host: { '(focusout)': 'handleValidation()', }, }] }], propDecorators: { tuiNativeValidator: [{ type: Input }] } }); /** * Generated bundle index. Do not edit. */ export { TuiNativeValidator }; //# sourceMappingURL=taiga-ui-cdk-directives-native-validator.mjs.map