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

Inline date range picker: emit an event when first date is selected #6645

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
9 changes: 9 additions & 0 deletions libs/doc-pages/datepicker/src/lib/datepicker-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { DemoDatepickerPreventChangeToNextMonthComponent } from './demos/prevent
import { DemoDatepickerWithTimepickerComponent } from './demos/with-timepicker/with-timepicker';
import { DatepickerCloseBehaviorComponent } from './demos/closeBehaviour/datepicker-close-behavior';
import { KeepDatesOutOfRulesComponent } from './demos/keep-dates-out-of-rules/keep-dates-out-of-rules.component';
import { InlineRangeBeginSelectionEventComponent } from "./demos/inline-range-begin-selection-event/inline-range-begin-selection-event";

export const demoComponentContent: ContentSection[] = [
{
Expand Down Expand Up @@ -478,6 +479,14 @@ export const demoComponentContent: ContentSection[] = [
html: require('!!raw-loader!./demos/keep-dates-out-of-rules/keep-dates-out-of-rules.component.html'),
description: `<p>If you use datepicker with rules (minDate, maxDate) you can set config property <code>keepDatesOutOfRules</code> to true to avoid overwriting invalid dates. Default value is false.</p>`,
outlet: KeepDatesOutOfRulesComponent
},
{
title: "Range begin selection event in inline date range picker",
anchor: 'inline-range-begin-selection-event',
component: require('!!raw-loader!./demos/inline-range-begin-selection-event/inline-range-begin-selection-event'),
html: require('!!raw-loader!./demos/inline-range-begin-selection-event/inline-range-begin-selection-event.html'),
description: `<p>You can subscribe to the inline date range picker's begin value change event (<code>bsValueBeginChange</code>) to be notified when a user begins selecting a range.</p>`,
outlet: InlineRangeBeginSelectionEventComponent
}
]
},
Expand Down
4 changes: 3 additions & 1 deletion libs/doc-pages/datepicker/src/lib/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { DemoDatepickerPreventChangeToNextMonthComponent } from './prevent-chang
import { DemoDatepickerWithTimepickerComponent } from './with-timepicker/with-timepicker';
import { DatepickerCloseBehaviorComponent } from './closeBehaviour/datepicker-close-behavior';
import { KeepDatesOutOfRulesComponent } from './keep-dates-out-of-rules/keep-dates-out-of-rules.component';
import { InlineRangeBeginSelectionEventComponent } from './inline-range-begin-selection-event/inline-range-begin-selection-event';

export const DEMO_COMPONENTS = [
DemoDatePickerAdaptivePositionComponent,
Expand Down Expand Up @@ -91,5 +92,6 @@ export const DEMO_COMPONENTS = [
DemoDatepickerStartViewComponent,
DemoDatepickerWithTimepickerComponent,
DatepickerCloseBehaviorComponent,
KeepDatesOutOfRulesComponent
KeepDatesOutOfRulesComponent,
InlineRangeBeginSelectionEventComponent
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="row">
<div class="pr-3 pb-3 pe-3 w-auto">
<bs-daterangepicker-inline
(bsValueBeginChange)="valueBeginChange($event)"
(bsValueChange)="valueChange($event)"
></bs-daterangepicker-inline>
</div>
<div class="mb-3">
<pre class="card card-block card-header" *ngIf="startDate">Start date is {{startDate | date}}</pre>
<pre class="card card-block card-header" *ngIf="endDate">End date is {{endDate | date}}</pre>
</div>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Component } from '@angular/core';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'demo-datepicker-inline',
templateUrl: './inline-range-begin-selection-event.html'
})
export class InlineRangeBeginSelectionEventComponent {
startDate?: Date;
endDate?: Date;

public valueBeginChange(event: Date[]) {
this.startDate = event[0];
this.endDate = undefined;
}

public valueChange(event: Date[]) {
this.endDate = event[1];
}
}
18 changes: 13 additions & 5 deletions src/datepicker/bs-daterangepicker-inline.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { ComponentLoader, ComponentLoaderFactory } from 'ngx-bootstrap/component-loader';

import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';

import { BsDatepickerConfig } from './bs-datepicker.config';
import { BsDaterangepickerInlineConfig } from './bs-daterangepicker-inline.config';
Expand Down Expand Up @@ -77,6 +76,10 @@ export class BsDaterangepickerInlineDirective implements OnInit, OnDestroy, OnCh
* Disable specific dates
*/
@Input() datesEnabled?: Date[];
/**
* Emits when the first range value has been received.
*/
@Output() bsValueBeginChange: EventEmitter<(Date|undefined)[] | undefined> = new EventEmitter();
/**
* Emits when daterangepicker value has been changed
*/
Expand Down Expand Up @@ -198,11 +201,16 @@ export class BsDaterangepickerInlineDirective implements OnInit, OnDestroy, OnCh
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;
if (value && value[0]) {
if (value[1]) {
// We have a range
this.bsValue = value;
} else {
// We have the first selection of a range
this.bsValueBeginChange.emit(value);
}
}
})
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/datepicker/testing/bs-inline-daterangepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ describe('daterangepicker inline:', () => {
expect(view[1].getMinutes()).toEqual(ranges[1].value[1].getMinutes());
});
});

it('should emit an event when the first date in a range is selected', () => {
const datepicker = getDaterangepickerInlineDirective(fixture);
const datepickerContainerInstance = getDatepickerInlineContainer(datepicker);
fixture.detectChanges();

const valueBeginChangeSpy = jest.spyOn(datepicker.bsValueBeginChange, "emit");

const currentDate = new Date();
const range = [currentDate, undefined];
datepickerContainerInstance.valueChange.emit(range);
fixture.detectChanges();

expect(valueBeginChangeSpy).toHaveBeenCalledTimes(1);
});
});
Loading