Skip to content

Commit

Permalink
feat(datepicker): allow to select dates from other months (#4828)
Browse files Browse the repository at this point in the history
closes #4485
closes #3746
  • Loading branch information
daniloff200 authored and valorkin committed Dec 6, 2018
1 parent a551277 commit b17926b
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 3 deletions.
10 changes: 9 additions & 1 deletion demo/src/app/components/+datepicker/datepicker-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DemoDatePickerVisibilityEventsComponent } from './demos/visibility-even
import { DemoDatepickerValueChangeEventComponent } from './demos/value-change-event/value-change-event';
import { DemoDatepickerTriggersCustomComponent } from './demos/triggers-custom/triggers-custom';
import { DemoDatepickerHideOnScrollComponent } from './demos/hide-on-scroll/hide-on-scroll';

import { DemoDatePickerSelectDatesFromOtherMonthsComponent } from './demos/select-dates-from-other-months/select-dates-from-other-months';
import { ContentSection } from '../../docs/models/content-section.model';
import { DemoTopSectionComponent } from '../../docs/demo-section-components/demo-top-section/index';
import { ExamplesComponent } from '../../docs/demo-section-components/demo-examples-section/index';
Expand Down Expand Up @@ -232,6 +232,14 @@ export const demoComponentContent: ContentSection[] = [
description: `<p>You can configure the datepicker via its <code>bsConfig</code> option</p>`,
outlet: DemoDatePickerConfigObjectComponent
},
{
title: 'Select dates from other month',
anchor: 'select-dates-from-other-month',
component: require('!!raw-loader?lang=typescript!./demos/select-dates-from-other-months/select-dates-from-other-months.ts'),
html: require('!!raw-loader?lang=markup!./demos/select-dates-from-other-months/select-dates-from-other-months.html'),
description: `<p>You can enable dates from other months via <code>selectFromOtherMonth</code> option in <code>bsConfig</code></p>`,
outlet: DemoDatePickerSelectDatesFromOtherMonthsComponent
},
{
title: 'Outside click',
anchor: 'outside-click',
Expand Down
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 @@ -15,6 +15,7 @@ import { DemoDatepickerConfigMethodComponent } from './config-method/config-meth
import { DemoDatePickerVisibilityEventsComponent } from './visibility-events/visibility-events';
import { DemoDatepickerValueChangeEventComponent } from './value-change-event/value-change-event';
import { DemoDatePickerConfigObjectComponent } from './config-object/config-object';
import { DemoDatePickerSelectDatesFromOtherMonthsComponent } from './select-dates-from-other-months/select-dates-from-other-months';
import { DemoDatepickerOutsideClickComponent } from './outside-click/outside-click';
import { DemoDatepickerByIsOpenPropComponent } from './trigger-by-isopen-property/trigger-by-isopen-property';
import { DemoDatepickerDateInitialStateComponent } from './date-initial-state/date-initial-state';
Expand All @@ -40,6 +41,7 @@ export const DEMO_COMPONENTS = [
DemoDatepickerPlacementComponent,
DemoDatepickerValueChangeEventComponent,
DemoDatePickerConfigObjectComponent,
DemoDatePickerSelectDatesFromOtherMonthsComponent,
DemoDatepickerOutsideClickComponent,
DemoDatepickerByIsOpenPropComponent,
DemoDatepickerDateInitialStateComponent,
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
class="form-control"
placeholder="Datepicker"
bsDatepicker
[bsConfig]="{ dateInputFormat: 'DD-MM-YYYY', selectFromOtherMonth: 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-datepicker-select-dates-from-other-months',
templateUrl: './select-dates-from-other-months.html'
})
export class DemoDatePickerSelectDatesFromOtherMonthsComponent {}
6 changes: 6 additions & 0 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ export const ngdoc: any = {
"defaultValue": "true",
"type": "boolean",
"description": "<p>Allows to hide week numbers in datepicker</p>\n"
},
{
"name": "selectFromOtherMonth",
"defaultValue": "false",
"type": "boolean",
"description": "<p>Allows to use dates from previous/future month as active</p>\n"
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions src/chronos/i18n/pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const plLocale: LocaleData = {
case 3:
return '[W środę o] LT';

case 5:
return '[W piątek o] LT';

case 6:
return '[W sobotę o] LT';

Expand All @@ -94,6 +97,10 @@ export const plLocale: LocaleData = {
return '[W zeszłą niedzielę o] LT';
case 3:
return '[W zeszłą środę o] LT';
case 4:
return '[W zeszłą czwartek o] LT';
case 5:
return '[W zeszłą piątek o] LT';
case 6:
return '[W zeszłą sobotę o] LT';
default:
Expand Down
7 changes: 7 additions & 0 deletions src/chronos/test/locale/pl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ describe('locale: pl', () => {
case 3:
return '[W środę o] LT';

case 5:
return '[W piątek o] LT';

case 6:
return '[W sobotę o] LT';

Expand Down Expand Up @@ -257,6 +260,10 @@ describe('locale: pl', () => {
return '[W zeszłą niedzielę o] LT';
case 3:
return '[W zeszłą środę o] LT';
case 4:
return '[W zeszłą czwartek o] LT';
case 5:
return '[W zeszłą piątek o] LT';
case 6:
return '[W zeszłą sobotę o] LT';
default:
Expand Down
1 change: 1 addition & 0 deletions src/datepicker/base/bs-datepicker-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {

export abstract class BsDatepickerAbstractComponent {
containerClass: string;
isOtherMonthsActive: boolean;

_effects: BsDatepickerEffects;
_customRangesFish: BsCustomDates[] = [];
Expand Down
5 changes: 5 additions & 0 deletions src/datepicker/bs-datepicker.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class BsDatepickerConfig implements DatepickerRenderOptions {
*/
maxDate?: Date;

/**
* Makes dates from other months active
*/
selectFromOtherMonth?: boolean;

/**
* Defaut mode for all date pickers
*/
Expand Down
8 changes: 8 additions & 0 deletions src/datepicker/bs-datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@
transition: 0s;
}

&.is-active-other-month:not(.disabled):not(.selected) span,
span.is-active-other-month:not(.disabled):not(.selected) {
background-color: $highlighted;
transition: 0s;
cursor: pointer !important;
}

span.disabled,
&.disabled span {
color: $font-color-02;
Expand Down Expand Up @@ -432,6 +439,7 @@
/* .is-other-month */
.is-other-month {
color: rgba(0, 0, 0, 0.25);
cursor: default !important;
}

/* .bs-datepicker-buttons */
Expand Down
1 change: 1 addition & 0 deletions src/datepicker/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CalendarCellViewModel {
/** *************** */
// days matrix: day cell view model
export interface DayViewModel extends CalendarCellViewModel {
isOtherMonthHovered?: boolean;
isOtherMonth?: boolean;
isInRange?: boolean;
isSelectionStart?: boolean;
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 @@ -55,6 +55,7 @@ export class BsDatepickerState
// DatepickerRenderOptions
showWeekNumbers?: boolean;
displayMonths?: number;
selectFromOtherMonth?: boolean;

// DatepickerFormatOptions
locale: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class BsDatepickerContainerComponent extends BsDatepickerAbstractComponen
}

ngOnInit(): void {
this.isOtherMonthsActive = this._config.selectFromOtherMonth;
this.containerClass = this._config.containerClass;
this._effects
.init(this._store)
Expand All @@ -61,9 +62,12 @@ export class BsDatepickerContainerComponent extends BsDatepickerAbstractComponen
}

daySelectHandler(day: DayViewModel): void {
if (day.isOtherMonth || day.isDisabled) {
const isDisabled = this.isOtherMonthsActive ? day.isDisabled : (day.isOtherMonth || day.isDisabled);

if (isDisabled) {
return;
}

this._store.dispatch(this._actions.select(day.date));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DayViewModel } from '../../models';
'[class.disabled]': 'day.isDisabled',
'[class.is-highlighted]': 'day.isHovered',
'[class.is-other-month]': 'day.isOtherMonth',
'[class.is-active-other-month]': 'day.isOtherMonthHovered',
'[class.in-range]': 'day.isInRange',
'[class.select-start]': 'day.isSelectionStart',
'[class.select-end]': 'day.isSelectionEnd',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class BsDaterangepickerContainerComponent extends BsDatepickerAbstractCom

ngOnInit(): void {
this.containerClass = this._config.containerClass;
this.isOtherMonthsActive = this._config.selectFromOtherMonth;
this._effects
.init(this._store)
// intial state options
Expand All @@ -61,7 +62,9 @@ export class BsDaterangepickerContainerComponent extends BsDatepickerAbstractCom
}

daySelectHandler(day: DayViewModel): void {
if (day.isOtherMonth || day.isDisabled) {
const isDisabled = this.isOtherMonthsActive ? day.isDisabled : (day.isOtherMonth || day.isDisabled);

if (isDisabled) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/datepicker/themes/bs/bs-days-calendar-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DaysCalendarViewModel,
DayViewModel
} from '../../models';
import { BsDatepickerConfig } from '../../bs-datepicker.config';

@Component({
selector: 'bs-days-calendar-view',
Expand Down Expand Up @@ -65,6 +66,8 @@ export class BsDaysCalendarViewComponent {
@Output() onSelect = new EventEmitter<DayViewModel>();
@Output() onHover = new EventEmitter<CellHoverEvent>();

constructor(private _config: BsDatepickerConfig) { }

navigateTo(event: BsNavigationDirection): void {
const step = BsNavigationDirection.DOWN === event ? -1 : 1;
this.onNavigate.emit({ step: { month: step } });
Expand All @@ -79,6 +82,10 @@ export class BsDaysCalendarViewComponent {
}

hoverDay(cell: DayViewModel, isHovered: boolean): void {
if (this._config.selectFromOtherMonth && cell.isOtherMonth) {
cell.isOtherMonthHovered = isHovered;
}

this.onHover.emit({ cell, isHovered });
}
}

0 comments on commit b17926b

Please sign in to comment.