Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api-goldens/element-ng/datepicker/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ export class SiDateRangeComponent implements ControlValueAccessor, Validator, Af
// (undocumented)
registerOnTouched(fn: () => void): void;
// (undocumented)
registerOnValidatorChange(fn: () => void): void;
// (undocumented)
setDisabledState(isDisabled: boolean): void;
readonly siDatepickerConfig: _angular_core.ModelSignal<DatepickerInputConfig>;
readonly siDatepickerRangeChange: _angular_core.OutputEmitterRef<DateRange | undefined>;
Expand Down
31 changes: 31 additions & 0 deletions projects/element-ng/datepicker/si-date-range.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { Component, signal, viewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { form, FormField } from '@angular/forms/signals';

import {
DatepickerInputConfig,
Expand Down Expand Up @@ -243,6 +244,36 @@ class FormWrapperComponent {

readonly siDateRangeComponent = viewChild.required(SiDateRangeComponent);
}

@Component({
imports: [FormField, SiDateRangeComponent],
template: `<si-date-range
[formField]="form.dateRange"
[siDatepickerConfig]="{ dateFormat: 'dd-MM-yyyy' }"
/>`
})
class SignalFormWrapperComponent {
readonly model = signal<{ dateRange: DateRange }>({
dateRange: { start: undefined, end: undefined }
});
readonly form = form(this.model);
}

describe('SiDateRangeComponent with a signal form field', () => {
it('should expose invalid start date format errors', async () => {
const fixture = TestBed.createComponent(SignalFormWrapperComponent);
await fixture.whenStable();

enterValue(startInput(fixture.nativeElement), 'INVALID DATE');
startInput(fixture.nativeElement).dispatchEvent(new Event('input'));
await fixture.whenStable();

expect(fixture.componentInstance.form.dateRange().errors()).toEqual([
expect.objectContaining({ kind: 'invalidStartDateFormat' })
]);
});
});

describe('SiDateRangeComponent within form', () => {
let fixture: ComponentFixture<FormWrapperComponent>;
let component: FormWrapperComponent;
Expand Down
6 changes: 6 additions & 0 deletions projects/element-ng/datepicker/si-date-range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class SiDateRangeComponent
private validator!: ValidatorFn;
private onChange = (val: any): void => {};
private onTouch = (): void => {};
private onValidatorChange = (): void => {};

protected readonly icons = addIcons({ elementCalendar });
protected readonly disabled = computed(() => this.disabledInput() || this.disabledNgControl());
Expand Down Expand Up @@ -291,6 +292,10 @@ export class SiDateRangeComponent
this.onTouch = fn;
}

registerOnValidatorChange(fn: () => void): void {
this.onValidatorChange = fn;
}

setDisabledState(isDisabled: boolean): void {
this.disabledNgControl.set(isDisabled);
}
Expand Down Expand Up @@ -321,6 +326,7 @@ export class SiDateRangeComponent
this.updateValue(dateRange);

this.onChange(this.value());
this.onValidatorChange();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also call thie function in case the config changes e.g. on min / max date change?

this.siDatepickerRangeChange.emit(this.value());
}

Expand Down
Loading