-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datepicker): initial version of datepicker component (#618)
Closes #20
- Loading branch information
1 parent
1c290a2
commit 9e8c5c0
Showing
34 changed files
with
1,720 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
demo/src/app/components/datepicker/datepicker.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Component} from '@angular/core'; | ||
import {DEMO_SNIPPETS} from './demos'; | ||
|
||
@Component({ | ||
selector: 'ngbd-datepicker', | ||
template: ` | ||
<ngbd-content-wrapper component="Datepicker"> | ||
<ngbd-api-docs directive="NgbDatepicker"></ngbd-api-docs> | ||
<ngbd-api-docs-class type="DayTemplateContext"></ngbd-api-docs-class> | ||
<ngbd-example-box demoTitle="Basic datepicker" [htmlSnippet]="snippets.basic.markup" [tsSnippet]="snippets.basic.code"> | ||
<ngbd-datepicker-basic></ngbd-datepicker-basic> | ||
</ngbd-example-box> | ||
</ngbd-content-wrapper> | ||
` | ||
}) | ||
export class NgbdDatepicker { | ||
snippets = DEMO_SNIPPETS; | ||
} |
13 changes: 13 additions & 0 deletions
13
demo/src/app/components/datepicker/demos/basic/datepicker-basic.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<p>Simple datepicker</p> | ||
|
||
<ngb-datepicker #dp [(ngModel)]="model"></ngb-datepicker> | ||
|
||
<hr/> | ||
|
||
<button class="btn btn-sm btn-outline-primary" (click)="selectToday()">Select Today</button> | ||
<button class="btn btn-sm btn-outline-primary" (click)="dp.navigateTo()">To current month</button> | ||
<button class="btn btn-sm btn-outline-primary" (click)="dp.navigateTo({year: 2013, month: 1})">To Feb 2013</button> | ||
|
||
<hr/> | ||
|
||
<pre>Model: {{ model | json }}</pre> |
16 changes: 16 additions & 0 deletions
16
demo/src/app/components/datepicker/demos/basic/datepicker-basic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Component} from '@angular/core'; | ||
|
||
const now = new Date(); | ||
|
||
@Component({ | ||
selector: 'ngbd-datepicker-basic', | ||
templateUrl: './datepicker-basic.html' | ||
}) | ||
export class NgbdDatepickerBasic { | ||
|
||
model; | ||
|
||
selectToday() { | ||
this.model = {year: now.getFullYear(), month: now.getMonth(), day: now.getDate()}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {NgbdDatepickerBasic} from './basic/datepicker-basic'; | ||
|
||
export const DEMO_DIRECTIVES = [NgbdDatepickerBasic]; | ||
|
||
export const DEMO_SNIPPETS = { | ||
basic: { | ||
code: require('!!prismjs?lang=typescript!./basic/datepicker-basic'), | ||
markup: require('!!prismjs?lang=markup!./basic/datepicker-basic.html')} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export * from './datepicker.component'; | ||
|
||
import {NgModule} from '@angular/core'; | ||
import {NgbdSharedModule} from '../../shared'; | ||
import {NgbdComponentsSharedModule} from '../shared'; | ||
import {NgbdDatepicker} from './datepicker.component'; | ||
import {DEMO_DIRECTIVES} from './demos'; | ||
|
||
@NgModule({ | ||
imports: [NgbdSharedModule, NgbdComponentsSharedModule], | ||
exports: [NgbdDatepicker], | ||
declarations: [NgbdDatepicker, ...DEMO_DIRECTIVES] | ||
}) | ||
export class NgbdDatepickerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Context for the datepicker 'day' template in case you want to override the default one | ||
*/ | ||
export interface DayTemplateContext { | ||
/** | ||
* Month currently displayed by the datepicker | ||
*/ | ||
currentMonth: number; | ||
|
||
/** | ||
* Date that corresponds to the template | ||
*/ | ||
date: {year: number, month: number, day: number}; | ||
|
||
/** | ||
* True if current date is disabled | ||
*/ | ||
disabled: boolean; | ||
|
||
/** | ||
* True if current date is selected | ||
*/ | ||
selected: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {TestBed} from '@angular/core/testing'; | ||
|
||
import {Component} from '@angular/core'; | ||
import {NgbDatepickerModule} from './datepicker.module'; | ||
import {NgbDatepickerDayView} from './datepicker-day-view'; | ||
|
||
function getElement(element: HTMLElement): HTMLElement { | ||
return <HTMLElement>element.querySelector('[ngbDatepickerDayView]'); | ||
} | ||
|
||
describe('ngbDatepickerDayView', () => { | ||
|
||
beforeEach(() => { | ||
TestBed.overrideModule(NgbDatepickerModule, {set: {exports: [NgbDatepickerDayView]}}); | ||
TestBed.configureTestingModule({declarations: [TestComponent], imports: [NgbDatepickerModule]}); | ||
}); | ||
|
||
it('should display date', () => { | ||
const fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el.innerText).toBe('22'); | ||
|
||
fixture.componentInstance.date = {year: 2016, month: 7, day: 25}; | ||
fixture.detectChanges(); | ||
expect(el.innerText).toBe('25'); | ||
}); | ||
|
||
it('should apply text-muted style for disabled days', () => { | ||
const fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el).not.toHaveCssClass('text-muted'); | ||
|
||
fixture.componentInstance.disabled = true; | ||
fixture.detectChanges(); | ||
expect(el).toHaveCssClass('text-muted'); | ||
}); | ||
|
||
it('should apply text-muted style for days of a different month', () => { | ||
const fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el).not.toHaveCssClass('text-muted'); | ||
|
||
fixture.componentInstance.date = {year: 2016, month: 8, day: 22}; | ||
fixture.detectChanges(); | ||
expect(el).toHaveCssClass('text-muted'); | ||
}); | ||
|
||
it('should apply selected style', () => { | ||
const fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el).not.toHaveCssClass('bg-primary'); | ||
|
||
fixture.componentInstance.selected = true; | ||
fixture.detectChanges(); | ||
expect(el).toHaveCssClass('bg-primary'); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
selector: 'test-cmp', | ||
template: | ||
'<div ngbDatepickerDayView [date]="date" [currentMonth]="currentMonth" [selected]="selected" [disabled]="disabled"></div>' | ||
}) | ||
class TestComponent { | ||
currentMonth = 7; | ||
date = {year: 2016, month: 7, day: 22}; | ||
disabled = false; | ||
selected = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Component, Input} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: '[ngbDatepickerDayView]', | ||
styles: [` | ||
:host { | ||
text-align: center; | ||
padding: 0.185rem 0.25rem; | ||
border-radius: 0.25rem; | ||
} | ||
`], | ||
host: {'[class.bg-primary]': 'selected', '[class.text-muted]': 'isMuted()'}, | ||
template: `{{ date.day }}` | ||
}) | ||
export class NgbDatepickerDayView { | ||
@Input() currentMonth: number; | ||
@Input() date: {year: number, month: number, day: number}; | ||
@Input() disabled: boolean; | ||
@Input() selected: boolean; | ||
|
||
isMuted() { return this.date.month !== this.currentMonth || this.disabled; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {NgbDatepickerI18nDefault} from './datepicker-i18n'; | ||
|
||
describe('ngb-datepicker-i18n-default', () => { | ||
|
||
const i18n = new NgbDatepickerI18nDefault(); | ||
|
||
it('should return month name', () => { | ||
expect(i18n.getMonthName(0)).toBe('Jan'); | ||
expect(i18n.getMonthName(11)).toBe('Dec'); | ||
expect(i18n.getMonthName(12)).toBe(undefined); | ||
}); | ||
|
||
it('should return weekday name', () => { | ||
expect(i18n.getWeekdayName(0)).toBe('Su'); | ||
expect(i18n.getWeekdayName(6)).toBe('Sa'); | ||
expect(i18n.getWeekdayName(7)).toBe(undefined); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {Injectable} from '@angular/core'; | ||
|
||
const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']; | ||
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | ||
|
||
@Injectable() | ||
export abstract class NgbDatepickerI18n { | ||
abstract getWeekdayName(weekday: number): string; | ||
abstract getMonthName(month: number): string; | ||
} | ||
|
||
@Injectable() | ||
export class NgbDatepickerI18nDefault extends NgbDatepickerI18n { | ||
getWeekdayName(weekday: number): string { return WEEKDAYS[weekday]; } | ||
|
||
getMonthName(month: number): string { return MONTHS[month]; } | ||
} |
Oops, something went wrong.