Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datepicker): fixed inline datepicker subscriptions #6339

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 14 additions & 2 deletions src/datepicker/bs-datepicker-inline.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges

ngOnInit(): void {
this.setConfig();
this.initSubscribes();
}

// if date changes from external source (model -> view)
initSubscribes() {
this.unsubscribeSubscriptions();
this._subs.push(
this.bsValueChange.subscribe((value: Date) => {
if (this._datepickerRef) {
Expand All @@ -121,7 +124,6 @@ export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges
})
);

// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange.subscribe((value: Date) => {
Expand All @@ -131,6 +133,13 @@ export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges
}
}

unsubscribeSubscriptions() {
if (this._subs?.length) {
this._subs.map(sub => sub.unsubscribe());
SvetlanaMuravlova marked this conversation as resolved.
Show resolved Hide resolved
}
this._subs = [];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this._subs = [];

}

ngOnChanges(changes: SimpleChanges): void {
if (changes["bsConfig"]) {
if (changes["bsConfig"].currentValue?.initCurrentTime && changes["bsConfig"].currentValue?.initCurrentTime !== changes["bsConfig"].previousValue?.initCurrentTime && this._bsValue) {
Expand Down Expand Up @@ -201,9 +210,12 @@ export class BsDatepickerInlineDirective implements OnInit, OnDestroy, OnChanges
.attach(BsDatepickerInlineContainerComponent)
.to(this._elementRef)
.show();

this.initSubscribes();
}

ngOnDestroy() {
this._datepicker.dispose();
this.unsubscribeSubscriptions();
}
}
47 changes: 29 additions & 18 deletions src/datepicker/bs-datepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ export class BsDatepickerDirective implements OnInit, OnDestroy, OnChanges, Afte
}
}

initSubscribes() {
// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date) => {
if (this._datepickerRef) {
this._datepickerRef.instance.value = value;
}
})
);

// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange.subscribe((value: Date) => {
this.bsValue = value;
this.hide();
})
);
}
}

ngAfterViewInit(): void {
this.isOpen$.pipe(
filter(isOpen => isOpen !== this.isOpen),
Expand All @@ -250,24 +271,7 @@ export class BsDatepickerDirective implements OnInit, OnDestroy, OnChanges, Afte
.position({ attachment: this.placement })
.show({ placement: this.placement });

// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date) => {
if (this._datepickerRef) {
this._datepickerRef.instance.value = value;
}
})
);

// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange.subscribe((value: Date) => {
this.bsValue = value;
this.hide();
})
);
}
this.initSubscribes();
}

/**
Expand Down Expand Up @@ -318,12 +322,19 @@ export class BsDatepickerDirective implements OnInit, OnDestroy, OnChanges, Afte
});
}

unsubscribeSubscriptions() {
if (this._subs?.length) {
this._subs.map(sub => sub.unsubscribe());
}
}

ngOnDestroy(): void {
this._datepicker.dispose();
this.isOpen$.next(false);
if (this.isDestroy$) {
this.isDestroy$.next(null);
this.isDestroy$.complete();
}
this.unsubscribeSubscriptions();
}
}
58 changes: 36 additions & 22 deletions src/datepicker/bs-daterangepicker-inline.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,7 @@ export class BsDaterangepickerInlineDirective implements OnInit, OnDestroy, OnCh

ngOnInit(): void {
this.setConfig();

// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date[]) => {
if (this._datepickerRef) {
this._datepickerRef.instance.value = value;
}
})
);

// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange
.pipe(
filter((range: Date[]) => range && range[0] && !!range[1])
)
.subscribe((value: Date[]) => {
this.bsValue = value;
})
);
}
this.initSubscribes();
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down Expand Up @@ -196,9 +175,44 @@ export class BsDaterangepickerInlineDirective implements OnInit, OnDestroy, OnCh
.attach(BsDaterangepickerInlineContainerComponent)
.to(this._elementRef)
.show();

this.initSubscribes();
}

initSubscribes() {
this.unsubscribeSubscriptions();
// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date[]) => {
if (this._datepickerRef) {
this._datepickerRef.instance.value = value;
}
})
);

// if date changes from picker (view -> model)
if (this._datepickerRef) {
this._subs.push(
this._datepickerRef.instance.valueChange
.pipe(
filter((range: Date[]) => range && range[0] && !!range[1])
)
.subscribe((value: Date[]) => {
this.bsValue = value;
})
);
}
}

unsubscribeSubscriptions() {
if (this._subs?.length) {
this._subs.map(sub => sub.unsubscribe());
}
this._subs = [];
}

ngOnDestroy() {
this._datepicker.dispose();
this.unsubscribeSubscriptions();
}
}
12 changes: 12 additions & 0 deletions src/datepicker/bs-daterangepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ export class BsDaterangepickerDirective
.position({ attachment: this.placement })
.show({ placement: this.placement });

this.initSubscribes();
}

initSubscribes() {
// if date changes from external source (model -> view)
this._subs.push(
this.bsValueChange.subscribe((value: Date[]) => {
Expand Down Expand Up @@ -303,12 +307,20 @@ export class BsDaterangepickerDirective
this.show();
}

unsubscribeSubscriptions() {
if (this._subs?.length) {
this._subs.map(sub => sub.unsubscribe());
}
}

ngOnDestroy(): void {
this._datepicker.dispose();
this.isOpen$.next(false);
if (this.isDestroy$) {
this.isDestroy$.next(null);
this.isDestroy$.complete();
}

this.unsubscribeSubscriptions();
}
}