-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(datepicker): add timepicker support #6275
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
Changes from 4 commits
a141173
809938f
470fb81
a938891
091f871
6b71290
f7a54a3
59d3e0f
1b0bc89
52082e2
d99d098
a4d3616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <div class="row"> | ||
| <div class="col-xs-12 col-12 col-md-4 form-group"> | ||
| <input type="text" | ||
| placeholder="Datepicker" | ||
| class="form-control" | ||
| [bsConfig]="{withTimepicker: true, rangeInputFormat : 'MMMM Do YYYY, h:mm:ss a', dateInputFormat: 'MMMM Do YYYY, h:mm:ss a'}" | ||
| bsDatepicker> | ||
| </div> | ||
| <div class="col-xs-12 col-12 col-md-4 form-group"> | ||
| <input type="text" | ||
| placeholder="Daterangepicker" | ||
| class="form-control" | ||
| [bsConfig]="{withTimepicker: true, rangeInputFormat : 'MMMM Do YYYY, h:mm:ss a', dateInputFormat: 'MMMM Do YYYY, h:mm:ss a'}" | ||
| bsDaterangepicker> | ||
| </div> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Component } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| // eslint-disable-next-line @angular-eslint/component-selector | ||
| selector: 'demo-datepicker-with-timepicker', | ||
| templateUrl: './with-timepicker.html' | ||
| }) | ||
|
|
||
| export class DemoDatepickerWithTimepickerComponent { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import { CommonModule } from '@angular/common'; | ||
| import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
| import { FormsModule } from '@angular/forms'; | ||
| import { ComponentLoaderFactory } from 'ngx-bootstrap/component-loader'; | ||
| import { PositioningService } from 'ngx-bootstrap/positioning'; | ||
|
|
||
| import { TooltipModule } from 'ngx-bootstrap/tooltip'; | ||
| import { TimepickerModule } from 'ngx-bootstrap/timepicker'; | ||
|
|
||
| import { BsDatepickerInputDirective } from './bs-datepicker-input.directive'; | ||
| import { BsDatepickerDirective } from './bs-datepicker.component'; | ||
|
|
@@ -35,7 +37,7 @@ import { BsTimepickerViewComponent } from './themes/bs/bs-timepicker-view.compon | |
| import { BsYearsCalendarViewComponent } from './themes/bs/bs-years-calendar-view.component'; | ||
|
|
||
| @NgModule({ | ||
| imports: [CommonModule, TooltipModule], | ||
| imports: [CommonModule, TooltipModule, FormsModule, TimepickerModule.forRoot()], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. injecting forms here seem is a bit too much, you can do it without it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| declarations: [ | ||
| BsCalendarLayoutComponent, | ||
| BsCurrentDateViewComponent, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,14 @@ export function bsDatepickerReducer(state: BsDatepickerState = initialDatepicker | |
| view: state.view | ||
| }; | ||
|
|
||
| const _time = (state.selectedTime || [])[0]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this code to a helper function if (state.selectedTime && state.selectedTime[0]) {
const _time = state.selectedTime[0];
newState.selectedDate.setHours(_time.getHours());
newState.selectedDate.setMinutes(_time.getMinutes());
newState.selectedDate.setSeconds(_time.getSeconds());
newState.selectedDate.setMilliseconds(_time.getMilliseconds());
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added one util function |
||
| if (_time) { | ||
| newState.selectedDate.setHours(_time.getHours()); | ||
| newState.selectedDate.setMinutes(_time.getMinutes()); | ||
| newState.selectedDate.setSeconds(_time.getSeconds()); | ||
| newState.selectedDate.setMilliseconds(_time.getMilliseconds()); | ||
| } | ||
|
|
||
| const mode = state.view.mode; | ||
| const _date = action.payload || state.view.date; | ||
| const date = getViewDate(_date, state.minDate, state.maxDate); | ||
|
|
@@ -97,6 +105,13 @@ export function bsDatepickerReducer(state: BsDatepickerState = initialDatepicker | |
| return Object.assign({}, state, newState); | ||
| } | ||
|
|
||
| case BsDatepickerActions.SELECT_TIME: { | ||
| const {date, index} = action.payload; | ||
| const selectedTime = [...state.selectedTime || []] ; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do proper checks and not |
||
| selectedTime[index] = date; | ||
| return Object.assign({}, state, { selectedTime }); | ||
| } | ||
|
|
||
| case BsDatepickerActions.SET_OPTIONS: { | ||
| if (!state.view) { | ||
| return state; | ||
|
|
@@ -115,11 +130,13 @@ export function bsDatepickerReducer(state: BsDatepickerState = initialDatepicker | |
| // if new value is array we work with date range | ||
| if (isArray(newState.value)) { | ||
| newState.selectedRange = newState.value; | ||
| newState.selectedTime = newState.value.map((i: Date) => i); | ||
| } | ||
|
|
||
| // if new value is a date -> datepicker | ||
| if (newState.value instanceof Date) { | ||
| newState.selectedDate = newState.value; | ||
| newState.selectedTime = [newState.value]; | ||
| } | ||
|
|
||
| // provided value is not supported :) | ||
|
|
@@ -140,6 +157,16 @@ export function bsDatepickerReducer(state: BsDatepickerState = initialDatepicker | |
| view: state.view | ||
| }; | ||
|
|
||
| newState.selectedRange.forEach((dte: Date, index: number) => { | ||
| const _time = (state.selectedTime || [])[index]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above about |
||
| if (_time) { | ||
| dte.setHours(_time.getHours()); | ||
| dte.setMinutes(_time.getMinutes()); | ||
| dte.setSeconds(_time.getSeconds()); | ||
| dte.setMilliseconds(_time.getMilliseconds()); | ||
| } | ||
| }); | ||
|
|
||
| const mode = state.view.mode; | ||
| const _date = action.payload && action.payload[0] || state.view.date; | ||
| const date = getViewDate(_date, state.minDate, state.maxDate); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it with optional type
enforce non nullable is no go