import { 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 { HlmInputGroup, HlmInputGroupImports } from '@spartan-ng/styles/input-group'; import { injectHlmMonthYearPickerConfig } from './hlm-month-year-picker.token'; @Component({ selector: 'hlm-month-year-input', imports: [HlmInputGroupImports, NgIcon], providers: [ provideIcons({ lucideCalendar, lucideX }), provideBrnDatePickerTrigger(HlmMonthYearInput), ], changeDetection: ChangeDetectionStrategy.OnPush, hostDirectives: [HlmInputGroup], template: ` @if (_showClearButton()) { } `, }) export class HlmMonthYearInput extends BrnDateInput implements BrnDatePickerTriggerBase { private readonly _config = injectHlmMonthYearPickerConfig(); private readonly _fieldControl = inject(BrnFieldControl, { optional: true }); 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 `HlmMonthYearPickerConfig`. */ 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 `HlmMonthYearPickerConfig`. */ public readonly formatInputDate = input<(date: T) => string>(this._config.formatInputDate); protected override parseValue(value: string): T | null { return this.parseDate()(value); } protected override formatInputValue(value: T): string { return this.formatInputDate()(value); } }