Skip to content

Commit

Permalink
feat(daterangepicker): add ability to show prev and current month ins…
Browse files Browse the repository at this point in the history
…tead of current and next one (#5513)

Co-authored-by: Vitaliy Makogon <mvitaliyd@gmail.com>
  • Loading branch information
daniloff200 and Domainv committed Jan 9, 2020
1 parent e1d2f75 commit cb6b29e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 3 deletions.
10 changes: 10 additions & 0 deletions demo/src/app/components/+datepicker/datepicker-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
NgApiDocComponent,
NgApiDocConfigComponent
} from '../../docs/api-docs';
import { DemoDateRangePickerShowPreviousMonth } from './demos/daterangepicker-show-previous-month/show-previous-month';


export const demoComponentContent: ContentSection[] = [
Expand Down Expand Up @@ -346,6 +347,15 @@ export const demoComponentContent: ContentSection[] = [
style: require('!!raw-loader!./demos/date-custom-classes/date-custom-classes.scss'),
description: `<p>Style dates with custom classes</p>`,
outlet: DemoDatepickerDateCustomClassesComponent
},
{
title: 'Previous month in Daterangepicker',
anchor: 'daterangepicker-previous-month',
component: require('!!raw-loader!./demos/daterangepicker-show-previous-month/show-previous-month.ts'),
html: require('!!raw-loader!./demos/daterangepicker-show-previous-month/show-previous-month.html'),
description: `<p>Pick previous & current month instead of current & next month.When daterange selected and related to current month,
daterangepicker will works by default, with current & next month</p>`,
outlet: DemoDateRangePickerShowPreviousMonth
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<input type="text"
placeholder="Daterangepicker"
class="form-control"
bsDaterangepicker
[bsConfig]="{ showPreviousMonth: true }">
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Component } from '@angular/core';

@Component({
selector: 'demo-daterangepicker-show-previous-month',
templateUrl: './show-previous-month.html'
})
export class DemoDateRangePickerShowPreviousMonth {}
2 changes: 2 additions & 0 deletions demo/src/app/components/+datepicker/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { DemoDatepickerTriggersCustomComponent } from './triggers-custom/trigger
import { DemoDatepickerTriggersManualComponent } from './triggers-manual/triggers-manual';
import { DemoDatepickerValueChangeEventComponent } from './value-change-event/value-change-event';
import { DemoDatePickerVisibilityEventsComponent } from './visibility-events/visibility-events';
import { DemoDateRangePickerShowPreviousMonth } from './daterangepicker-show-previous-month/show-previous-month';


export const DEMO_COMPONENTS = [
Expand Down Expand Up @@ -66,5 +67,6 @@ export const DEMO_COMPONENTS = [
DemoDatepickerTriggersCustomComponent,
DemoDatepickerTriggersManualComponent,
DemoDatepickerValueChangeEventComponent,
DemoDateRangePickerShowPreviousMonth,
DemoDatePickerVisibilityEventsComponent
];
5 changes: 5 additions & 0 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,11 @@ export const ngdoc: any = {
"type": "BsDatepickerViewMode",
"description": "<p>Default mode for all date pickers</p>\n"
},
{
"name": "showPreviousMonth",
"type": "boolean",
"description": "<p>Shows previous and current month, instead of current and next (daterangepicker only</p>\n"
},
{
"name": "rangeInputFormat",
"defaultValue": "L",
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/ng-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ ng serve

- Install `ngx-bootstrap` and `bootstrap`

```bash
```bash
npm install ngx-bootstrap bootstrap --save
```
```

- Open `src/app/app.module.ts` and add:

Expand Down
7 changes: 6 additions & 1 deletion src/datepicker/bs-datepicker.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ export class BsDatepickerConfig implements DatepickerRenderOptions {
selectFromOtherMonth?: boolean;

/**
* Makes dates from other months active
* Allows select first date of the week by click on week number
*/
selectWeek?: boolean;

/**
* Shows previous and current month, instead of current and next (dateRangePicker only)
*/
showPreviousMonth?: boolean;

/**
* Add class to current day
*/
Expand Down
4 changes: 4 additions & 0 deletions src/datepicker/reducer/bs-datepicker.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ function calculateReducer(state: BsDatepickerState): BsDatepickerState {
let viewDate = state.view.date;

if (state.view.mode === 'day') {
if (state.showPreviousMonth && state.selectedRange.length === 0) {
viewDate = shiftDate(viewDate, { month: -1 });
}

state.monthViewOptions.firstDayOfWeek = getLocale(state.locale).firstDayOfWeek();
const monthsModel = new Array(displayMonths);
for (let monthIndex = 0; monthIndex < displayMonths; monthIndex++) {
Expand Down
1 change: 1 addition & 0 deletions src/datepicker/reducer/bs-datepicker.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class BsDatepickerState
formattedMonths?: DaysCalendarViewModel[];
flaggedMonths?: DaysCalendarViewModel[];
selectFromOtherMonth?: boolean;
showPreviousMonth?: boolean; // dateRangePicker only;

// months calendar
monthsCalendar?: MonthsCalendarViewModel[];
Expand Down

0 comments on commit cb6b29e

Please sign in to comment.