Skip to content

Commit cb3efc5

Browse files
author
m2rt
committed
fix(datepicker): displayed value not changing on external update #322
1 parent 4ede0af commit cb3efc5

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

tedi/components/form/date-picker/date-picker.component.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,26 @@ describe("DatePickerComponent", () => {
12301230
jest.useRealTimers();
12311231
});
12321232
});
1233+
1234+
describe("External selected input changes", () => {
1235+
it("should update inputValue when selected input is changed externally", () => {
1236+
const date = new Date(2024, 5, 15);
1237+
fixture.componentRef.setInput("selected", date);
1238+
fixture.detectChanges();
1239+
1240+
expect(component.inputValue()).toBe("15.06.2024");
1241+
});
1242+
1243+
it("should clear inputValue when selected input is set to null externally", () => {
1244+
fixture.componentRef.setInput("selected", new Date(2024, 5, 15));
1245+
fixture.detectChanges();
1246+
1247+
expect(component.inputValue()).toBe("15.06.2024");
1248+
1249+
fixture.componentRef.setInput("selected", null);
1250+
fixture.detectChanges();
1251+
1252+
expect(component.inputValue()).toBe("");
1253+
});
1254+
});
12331255
});

tedi/components/form/date-picker/date-picker.component.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
OnInit,
1111
viewChild,
1212
ElementRef,
13+
effect,
1314
} from "@angular/core";
1415
import { ButtonComponent } from "../../buttons/button/button.component";
1516
import { ClosingButtonComponent } from "../../buttons/closing-button/closing-button.component";
@@ -272,11 +273,15 @@ export class DatePickerComponent implements OnInit {
272273

273274
readonly translationService = inject(TediTranslationService);
274275

275-
ngOnInit(): void {
276-
const selected = this.selected();
277-
this.inputValue.set(selected ? formatDate(selected) : "");
276+
constructor() {
277+
effect(() => {
278+
const selected = this.selected();
279+
this.inputValue.set(selected ? formatDate(selected) : "");
280+
});
281+
}
278282

279-
let active = selected ?? this.today;
283+
ngOnInit(): void {
284+
let active = this.selected() ?? this.today;
280285

281286
// If the initial active date is disabled, find the first enabled date
282287
if (this.isDisabled(active)) {

0 commit comments

Comments
 (0)