import { booleanAttribute, ChangeDetectionStrategy, Component, computed, inject, input, } from '@angular/core'; import { NgIcon, provideIcons } from '@ng-icons/core'; import { lucideCalendar, lucideX } from '@ng-icons/lucide'; import { BrnDateInput, type BrnDatePickerTriggerBase, provideBrnDatePickerTrigger, } from '@spartan-ng/brain/date-picker'; import { BrnFieldControl } from '@spartan-ng/brain/field'; import type { BooleanInput } from '@angular/cdk/coercion'; import { HlmInputGroup, HlmInputGroupImports } from '@spartan-ng/styles/input-group'; import { injectHlmDatePickerConfig } from './hlm-date-picker.token'; @Component({ selector: 'hlm-date-picker-input', imports: [HlmInputGroupImports, NgIcon], providers: [ provideIcons({ lucideCalendar, lucideX }), provideBrnDatePickerTrigger(HlmDatePickerInput), ], changeDetection: ChangeDetectionStrategy.OnPush, hostDirectives: [HlmInputGroup], template: ` @if (_showClearButton() && !readonly()) { } `, }) export class HlmDatePickerInput extends BrnDateInput implements BrnDatePickerTriggerBase { private readonly _config = injectHlmDatePickerConfig(); private readonly _fieldControl = inject(BrnFieldControl, { optional: true }); /** When true, typing is blocked but the calendar toggle still works. */ public readonly readonly = input(false, { transform: booleanAttribute }); private readonly _invalid = this._fieldControl?.invalid; protected readonly _spartanInvalid = computed( () => this.forceInvalid() || this._fieldControl?.spartanInvalid(), ); protected readonly _dirty = this._fieldControl?.dirty; protected readonly _touched = this._fieldControl?.touched; protected readonly _ariaInvalid = computed(() => (this._invalid?.() ? 'true' : null)); /** * Parses input text into a date value. Return `null` for invalid * input - the picker's date is cleared while the text is preserved so * the user can fix it. * * Defaults to `parseDate` from `HlmDatePickerConfig`. */ public readonly parseDate = input<(value: string) => T | null>(this._config.parseDate); /** * Formats the current date into the input/edit format shown while the * input is focused. On blur the picker's display format is restored. * * Defaults to `formatInputDate` from `HlmDatePickerConfig`. */ public readonly formatInputDate = input<(date: T) => string>(this._config.formatInputDate); protected parseValue(value: string): T | null { return this.parseDate()(value); } protected formatInputValue(value: T): string { return this.formatInputDate()(value); } }