-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
- Loading branch information
1 parent
9556e4b
commit c9164dd
Showing
31 changed files
with
1,632 additions
and
3 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
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
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-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" [minDate]="minDate" [maxDate]="maxDate"></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> |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
import {Component} from '@angular/core'; | ||
|
||
const now = new Date(); | ||
|
||
@Component({ | ||
selector: 'ngbd-datepicker-basic', | ||
template: require('./datepicker-basic.html') | ||
}) | ||
export class NgbdDatepickerBasic { | ||
|
||
model; | ||
|
||
minDate = {year: 2000, month: 0, date: 7}; | ||
maxDate = {year: 2020, month: 5, date: 15}; | ||
|
||
selectToday() { | ||
this.model = {year: now.getFullYear(), month: now.getMonth(), date: 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,81 @@ | ||
import {TestBed, ComponentFixture} from '@angular/core/testing'; | ||
import {createGenericTestComponent} from '../util/tests'; | ||
|
||
import {Component} from '@angular/core'; | ||
|
||
import {NgbDatepickerModule} from './datepicker.module'; | ||
import {MonthViewModel, DayViewModel} from './datepicker-view-model'; | ||
import {NgbDatepickerDayView} from './datepicker-day-view'; | ||
import {NgbDate} from './ngb-date'; | ||
|
||
const createTestComponent = (html: string) => | ||
createGenericTestComponent(html, TestComponent) as ComponentFixture<TestComponent>; | ||
|
||
function getElement(element: HTMLElement): HTMLElement { | ||
return <HTMLElement>element.querySelector('[ngb-datepicker-day-view]'); | ||
} | ||
|
||
describe('ngb-datepicker-day-view', () => { | ||
|
||
beforeEach(() => { | ||
TestBed.overrideModule(NgbDatepickerModule, {set: {exports: [NgbDatepickerDayView]}}); | ||
TestBed.configureTestingModule({declarations: [TestComponent], imports: [NgbDatepickerModule]}); | ||
}); | ||
|
||
it('should display date', () => { | ||
const fixture = | ||
createTestComponent('<div ngb-datepicker-day-view [day]="day" [month]="month" [selected]="selected"></div>'); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el.innerText).toBe('22'); | ||
|
||
fixture.componentInstance.day.date = new NgbDate(2016, 7, 25); | ||
fixture.detectChanges(); | ||
expect(el.innerText).toBe('25'); | ||
}); | ||
|
||
it('should apply text-muted style for disabled days', () => { | ||
const fixture = | ||
createTestComponent('<div ngb-datepicker-day-view [day]="day" [month]="month" [selected]="selected"></div>'); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el).not.toHaveCssClass('text-muted'); | ||
|
||
fixture.componentInstance.day.disabled = true; | ||
fixture.detectChanges(); | ||
expect(el).toHaveCssClass('text-muted'); | ||
}); | ||
|
||
it('should apply text-muted style for days of a different month', () => { | ||
const fixture = | ||
createTestComponent('<div ngb-datepicker-day-view [day]="day" [month]="month" [selected]="selected"></div>'); | ||
|
||
const el = getElement(fixture.nativeElement); | ||
expect(el).not.toHaveCssClass('text-muted'); | ||
|
||
fixture.componentInstance.day.date = new NgbDate(2016, 8, 22); | ||
fixture.detectChanges(); | ||
expect(el).toHaveCssClass('text-muted'); | ||
}); | ||
|
||
it('should apply selected style', () => { | ||
const fixture = | ||
createTestComponent('<div ngb-datepicker-day-view [day]="day" [month]="month" [selected]="selected"></div>'); | ||
|
||
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: ''}) | ||
class TestComponent { | ||
day: DayViewModel = {date: new NgbDate(2016, 7, 22), disabled: false}; | ||
|
||
selected = false; | ||
|
||
month: MonthViewModel = {year: 2016, number: 7, weekdays: [0], weeks: []}; | ||
} |
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,20 @@ | ||
import {Component, Input} from '@angular/core'; | ||
import {MonthViewModel, DayViewModel} from './datepicker-view-model'; | ||
|
||
@Component({ | ||
selector: '[ngb-datepicker-day-view]', | ||
styles: [` | ||
:host { | ||
text-align: center; | ||
padding: 0.185rem 0.25rem; | ||
border-radius: 0.25rem; | ||
} | ||
`], | ||
host: {'[class.bg-primary]': 'selected', '[class.text-muted]': 'day.date.month !== month.number || day.disabled'}, | ||
template: `{{ day.date.date }}` | ||
}) | ||
export class NgbDatepickerDayView { | ||
@Input() month: MonthViewModel; | ||
@Input() day: DayViewModel; | ||
@Input() 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,21 @@ | ||
import {NgbDatepickerI18n} from './datepicker-i18n'; | ||
|
||
describe('ngb-datepicker-i18n', () => { | ||
|
||
it('should return month name', () => { | ||
const i18n = new NgbDatepickerI18n(); | ||
|
||
expect(i18n.getMonthName(0)).toBe('Jan'); | ||
expect(i18n.getMonthName(11)).toBe('Dec'); | ||
expect(i18n.getMonthName(12)).toBe(undefined); | ||
}); | ||
|
||
it('should return weekday name', () => { | ||
const i18n = new NgbDatepickerI18n(); | ||
|
||
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,11 @@ | ||
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 class NgbDatepickerI18n { | ||
getWeekdayName(weekday: number): string { return WEEKDAYS[weekday]; } | ||
|
||
getMonthName(month: number): string { return MONTHS[month]; } | ||
} |
Oops, something went wrong.