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

feat(datepicker): Add optional "today" button #5786

Merged
merged 22 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2b589ba
add optional configuration for datepicker 'today' button
May 17, 2020
a1fbe8d
add datepicker today html and css
May 17, 2020
79b5a56
add tests for optional datepicker today button and function
May 17, 2020
977c803
remove unused code
May 17, 2020
8b4cb9c
feat(datepicker): Add optional "today" button config
May 17, 2020
dd8a48b
feat(datepicker): Add optional "today" button html/css
May 17, 2020
5f46e36
feat(datepicker): Add optional "today" button tests
May 17, 2020
05f33da
feat(datepicker): Add optional "today" button tests
May 17, 2020
6975d0a
feat(datepicker): Add optional "today" button demo and docs
May 17, 2020
79cb2c0
feat(datepicker): Add optional "today" button tests
May 17, 2020
a912835
feat(datepicker): resolve merge conflicts
May 17, 2020
f11351e
Merge branch 'development' into datepicker-today-button
pjlinn May 29, 2020
1b6442e
Merge branch 'development' into datepicker-today-button
daniloff200 Jun 15, 2020
4abae66
feat(datepicker): Add optional "today" ng-api-docs update
Jun 15, 2020
6ef2092
Merge branch 'development' of https://github.com/valor-software/ngx-b…
Jun 15, 2020
543d88b
feat(datepicker): Add optional "today" button: auto generated ng-api-…
Jun 15, 2020
6070968
Merge branch 'datepicker-today-button' of https://github.com/pjlinn/n…
Jun 15, 2020
425a588
Merge branch 'development' into datepicker-today-button
pjlinn Jun 15, 2020
3b0fbee
Merge branch 'development' into datepicker-today-button
daniloff200 Jun 16, 2020
88c9dbf
Merge branch 'development' into datepicker-today-button
daniloff200 Jun 16, 2020
cefc5a2
Merge branch 'development' of github.com:valor-software/ngx-bootstrap…
daniloff200 Jun 19, 2020
82158c2
feat(datepicker): add optional positioning for Today button
daniloff200 Jun 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { DemoDatePickerQuickSelectRangesComponent } from './demos/quick-select-r
import { DemoDateRangePickerShowPreviousMonth } from './demos/daterangepicker-show-previous-month/show-previous-month';
import { DemoDatePickerSelectWeekRangeComponent } from './demos/select-week-range/select-week-range';
import { DemoDateRangePickerDisplayOneMonth } from './demos/daterangepicker-display-one-month/display-one-month';
import { DemoDatepickerTodayButtonComponent } from './demos/today-button/today-button';


export const demoComponentContent: ContentSection[] = [
Expand Down Expand Up @@ -406,6 +407,14 @@ export const demoComponentContent: ContentSection[] = [
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
},
{
title: 'Show Today Button',
anchor: 'datepicker-show-today-button',
component: require('!!raw-loader!./demos/today-button/today-button.ts'),
html: require('!!raw-loader!./demos/today-button/today-button.html'),
description: `<p>Display an optional 'Today' button that will automatically select today's date.</p>`,
outlet: DemoDatepickerTodayButtonComponent
}
]
},
Expand Down
4 changes: 3 additions & 1 deletion demo/src/app/components/+datepicker/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { DemoDatePickerVisibilityEventsComponent } from './visibility-events/vis
import { DemoDatePickerQuickSelectRangesComponent } from './quick-select-ranges/quick-select-ranges';
import { DemoDateRangePickerShowPreviousMonth } from './daterangepicker-show-previous-month/show-previous-month';
import { DemoDateRangePickerDisplayOneMonth } from './daterangepicker-display-one-month/display-one-month';
import { DemoDatepickerTodayButtonComponent } from './today-button/today-button';


export const DEMO_COMPONENTS = [
Expand Down Expand Up @@ -76,5 +77,6 @@ export const DEMO_COMPONENTS = [
DemoDateRangePickerShowPreviousMonth,
DemoDateRangePickerDisplayOneMonth,
DemoDatePickerVisibilityEventsComponent,
DemoDatePickerQuickSelectRangesComponent
DemoDatePickerQuickSelectRangesComponent,
DemoDatepickerTodayButtonComponent
];
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="Datepicker"
class="form-control"
bsDatepicker
[bsConfig]="{ showTodayButton: true, todayPosition: 'center' }">
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Component } from '@angular/core';

@Component({
selector: 'demo-datepicker-today-button',
templateUrl: './today-button.html'
})
export class DemoDatepickerTodayButtonComponent {}
30 changes: 27 additions & 3 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export const ngdoc: any = {
"properties": [
{
"name": "_a",
"type": "DateArray",
"type": "number[]",
"description": "<p>DateArray [year, month, date, .....]</p>\n"
},
{
Expand Down Expand Up @@ -613,7 +613,7 @@ export const ngdoc: any = {
{
"name": "_w",
"type": "WeekParsing",
"description": "<p>week</p>\n"
"description": "<p>date specific info\nweek</p>\n"
}
]
},
Expand Down Expand Up @@ -1093,6 +1093,18 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>Shows previous and current month, instead of current and next (dateRangePicker only)</p>\n"
},
{
"name": "showTodayButton",
"defaultValue": "false",
"type": "boolean",
"description": "<p>Shows &#39;today&#39; button</p>\n"
},
{
"name": "todayPosition",
"defaultValue": "center",
"type": "string",
"description": "<p>Positioning for &#39;today&#39; button</p>\n"
},
{
"name": "showWeekNumbers",
"defaultValue": "true",
Expand Down Expand Up @@ -2352,6 +2364,12 @@ export const ngdoc: any = {
}
],
"returnType": "BsModalRef"
},
{
"name": "checkScrollbar",
"description": "<p>Checks if the body is overflowing and sets scrollbar width</p>\n",
"args": [],
"returnType": "void"
}
],
"properties": []
Expand Down Expand Up @@ -2512,6 +2530,12 @@ export const ngdoc: any = {
"description": "<p>Events tricks</p>\n",
"args": [],
"returnType": "void"
},
{
"name": "checkScrollbar",
"description": "<p>Scroll bar tricks</p>\n",
"args": [],
"returnType": "void"
}
]
},
Expand Down Expand Up @@ -4215,7 +4239,7 @@ export const ngdoc: any = {
{
"name": "activeDescendant",
"type": "string",
"description": "<p>if false don&#39;t focus the input element the typeahead directive is associated with on selection</p>\n"
"description": "<p>if false restrict model values to the ones selected from the popup only will be provided\nif false the first match automatically will not be focused as you type\nformat the ng-model result after selection\nif true automatically select an item when there is one option that exactly matches the user input\nif true select the currently highlighted match on blur\nif false don&#39;t focus the input element the typeahead directive is associated with on selection</p>\n"
}
],
"methods": []
Expand Down
4 changes: 4 additions & 0 deletions src/datepicker/base/bs-datepicker-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
export abstract class BsDatepickerAbstractComponent {
containerClass: string;
isOtherMonthsActive: boolean;
showTodayBtn: boolean;
todayPos: string;

_effects: BsDatepickerEffects;
customRanges: BsCustomDates[] = [];
Expand Down Expand Up @@ -77,6 +79,8 @@ export abstract class BsDatepickerAbstractComponent {

setRangeOnCalendar(dates: BsCustomDates): void {}

setToday(): void {}

/* tslint:disable-next-line: no-any */
_stopPropagation(event: any): void {
event.stopPropagation();
Expand Down
9 changes: 9 additions & 0 deletions src/datepicker/bs-datepicker.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,13 @@ export class BsDatepickerConfig implements DatepickerRenderOptions {
monthLabel = 'MMMM';
yearLabel = 'YYYY';
weekNumbers = 'w';

/**
* Shows 'today' button
*/
showTodayButton = false;
/**
* Positioning of 'today' button
*/
todayPosition = 'center';
}
17 changes: 17 additions & 0 deletions src/datepicker/bs-datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,23 @@
.btn-default {
margin-left: 10px;
}

.btn-today-wrapper {
display: flex;
flex-flow: row wrap;
}

.today-right {
flex-grow: 0;
}

.today-left {
flex-grow: 1;
}

.today-center {
flex-grow: 0.5;
}
}
}

Expand Down
41 changes: 39 additions & 2 deletions src/datepicker/bs-datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BsDatepickerDirective } from './bs-datepicker.component';
import { BsDatepickerConfig } from './bs-datepicker.config';
import { BsDatepickerContainerComponent } from './themes/bs/bs-datepicker-container.component';
import { CalendarCellViewModel, WeekViewModel } from './models';
import { dispatchKeyboardEvent } from '@netbasal/spectator';
import { dispatchKeyboardEvent, queryAll } from '@netbasal/spectator';
import { registerEscClick } from '../utils';

@Component({
Expand All @@ -18,7 +18,8 @@ class TestComponent {
@ViewChild(BsDatepickerDirective, { static: false }) datepicker: BsDatepickerDirective;
bsConfig: Partial<BsDatepickerConfig> = {
displayMonths: 2,
selectWeek: true
selectWeek: true,
showTodayButton: true
};
}

Expand Down Expand Up @@ -128,4 +129,40 @@ describe('datepicker:', () => {

expect(spy).toHaveBeenCalled();
}));

it('should show the today button when showTodayButton config is true', () => {
showDatepicker(fixture);
const buttonText: string[] = [];
queryAll('button').forEach(button => {
buttonText.push(button.textContent);
});
expect(buttonText.filter(button => button === 'Today').length).toEqual(1);
});

it('should set today date', () => {
const datepicker = showDatepicker(fixture);
const datepickerContainerInstance = getDatepickerContainer(datepicker);

datepickerContainerInstance[`_store`]
.select(state => state.view)
.subscribe(view => {
view.date = new Date(2020, 0, 1);
}).unsubscribe();
fixture.detectChanges();

datepickerContainerInstance[`_store`]
.select(state => state.view)
.subscribe(view => {
expect(view.date.getDay()).not.toEqual((new Date().getDay()), 'should start out not equal to today');
}).unsubscribe();

datepickerContainerInstance.setToday();
fixture.detectChanges();

datepickerContainerInstance[`_store`]
.select(state => state.view)
.subscribe(view => {
expect(view.date.getDay()).toEqual((new Date().getDay()), 'should update to equal today');
}).unsubscribe();
});
});
6 changes: 6 additions & 0 deletions src/datepicker/themes/bs/bs-datepicker-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export class BsDatepickerContainerComponent extends BsDatepickerAbstractComponen

this.isOtherMonthsActive = this._config.selectFromOtherMonth;
this.containerClass = this._config.containerClass;
this.showTodayBtn = this._config.showTodayButton;
this.todayPos = this._config.todayPosition;
this._effects
.init(this._store)
// intial state options
Expand Down Expand Up @@ -118,6 +120,10 @@ export class BsDatepickerContainerComponent extends BsDatepickerAbstractComponen
this._store.dispatch(this._actions.select(day.date));
}

setToday(): void {
this._store.dispatch(this._actions.select(new Date()));
}

ngOnDestroy(): void {
for (const sub of this._subs) {
sub.unsubscribe();
Expand Down
15 changes: 13 additions & 2 deletions src/datepicker/themes/bs/bs-datepicker-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@
<button class="btn btn-success" type="button">Apply</button>
<button class="btn btn-default" type="button">Cancel</button>
</div>

<div class="bs-datepicker-buttons">
<div class="btn-today-wrapper"
[class.today-left]="todayPos === 'left'"
[class.today-right]="todayPos === 'right'"
[class.today-center]="todayPos === 'center'"
*ngIf="showTodayBtn">
<button class="btn btn-success" (click)="setToday()">Today</button>
</div>
</div>

</div>

<!--custom dates or date ranges picker-->
<div class="bs-datepicker-custom-range" *ngIf="customRanges?.length > 0">
<bs-custom-date-view
[selectedRange]="chosenRange"
<bs-custom-date-view
[selectedRange]="chosenRange"
[ranges]="customRanges"
(onSelect)="setRangeOnCalendar($event)">
</bs-custom-date-view>
Expand Down