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

Added option to customize dates separator #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class AppComponent {
presetNames: ['This Month', 'Last Month', 'This Week', 'Last Week', 'This Year', 'Last Year', 'Start', 'End'],
dateFormat: 'yMd',
outputFormat: 'DD/MM/YYYY',
startOfWeek: 1
startOfWeek: 1,
dateSeparator: '_',
};
}
}
Expand All @@ -73,6 +74,7 @@ export interface NgDateRangePickerOptions {
dateFormat: string;
outputFormat: string;
startOfWeek: number;
dateSeparator: string;
}
```

Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<option>grape</option>
<option>red</option>
<option>gray</option>
<option>orange</option>
</select>
</span>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class AppComponent implements OnInit {
presetNames: ['This Month', 'Last Month', 'This Week', 'Last Week', 'This Year', 'Last Year', 'Start', 'End'],
dateFormat: 'yMd',
outputFormat: 'DD/MM/YYYY',
startOfWeek: 0
startOfWeek: 0,
dateSeparator: '_'
};
}
}
3 changes: 2 additions & 1 deletion src/ng-daterangepicker/ng-daterangepicker.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
'theme-cyan': options.theme === 'cyan',
'theme-grape': options.theme === 'grape',
'theme-red': options.theme === 'red',
'theme-gray': options.theme === 'gray' }">
'theme-gray': options.theme === 'gray',
'theme-orange': options.theme === 'orange' }">

<div class="input-section" (click)="toggleCalendar($event, 'from')">
<span class="label-txt">{{options.presetNames[6]}}</span>
Expand Down
19 changes: 13 additions & 6 deletions src/ng-daterangepicker/ng-daterangepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import * as dateFns from 'date-fns';

export interface NgDateRangePickerOptions {
theme: 'default' | 'green' | 'teal' | 'cyan' | 'grape' | 'red' | 'gray';
theme: 'default' | 'green' | 'teal' | 'cyan' | 'grape' | 'red' | 'gray' | 'orange';
range: 'tm' | 'lm' | 'lw' | 'tw' | 'ty' | 'ly';
dayNames: string[];
presetNames: string[];
dateFormat: string;
outputFormat: string;
startOfWeek: number;
dateSeparator: string;
}

export interface IDay {
Expand Down Expand Up @@ -55,7 +56,8 @@ export class NgDateRangePickerComponent implements ControlValueAccessor, OnInit,
presetNames: ['This Month', 'Last Month', 'This Week', 'Last Week', 'This Year', 'Last Year', 'Start', 'End'],
dateFormat: 'yMd',
outputFormat: 'DD/MM/YYYY',
startOfWeek: 0
startOfWeek: 0,
dateSeparator: '-'
}

private onTouchedCallback: () => void = () => { };
Expand Down Expand Up @@ -143,7 +145,7 @@ export class NgDateRangePickerComponent implements ControlValueAccessor, OnInit,
}

this.days = prevMonthDays.concat(days);
this.value = `${dateFns.format(this.dateFrom, this.options.outputFormat)}-${dateFns.format(this.dateTo, this.options.outputFormat)}`;
this.value = `${dateFns.format(this.dateFrom, this.options.outputFormat)}${this.options.dateSeparator}${dateFns.format(this.dateTo, this.options.outputFormat)}`;
}

toggleCalendar(e: MouseEvent, selection: 'from' | 'to'): void {
Expand Down Expand Up @@ -189,32 +191,37 @@ export class NgDateRangePickerComponent implements ControlValueAccessor, OnInit,

selectRange(range: 'tm' | 'lm' | 'lw' | 'tw' | 'ty' | 'ly'): void {
let today = dateFns.startOfDay(new Date());

// set current year, this is overwritten only in 'Last year' selection
dateFns.setYear(today, today.getFullYear());
switch (range) {
case 'tm':
this.date = dateFns.setMonth(today, today.getMonth());
this.dateFrom = dateFns.startOfMonth(today);
this.dateTo = dateFns.endOfMonth(today);
break;
case 'lm':
today = dateFns.subMonths(today, 1);
this.date = today = dateFns.subMonths(today, 1);
this.dateFrom = dateFns.startOfMonth(today);
this.dateTo = dateFns.endOfMonth(today);
break;
case 'lw':
today = dateFns.subWeeks(today, 1);
this.date = today;
this.dateFrom = dateFns.startOfWeek(today, {weekStartsOn: this.options.startOfWeek});
this.dateTo = dateFns.endOfWeek(today, {weekStartsOn: this.options.startOfWeek});
break;
case 'tw':
this.date = dateFns.setMonth(today, today.getMonth());
this.dateFrom = dateFns.startOfWeek(today, {weekStartsOn: this.options.startOfWeek});
this.dateTo = dateFns.endOfWeek(today, {weekStartsOn: this.options.startOfWeek});
break;
case 'ty':
this.date = dateFns.setYear(today, today.getFullYear());
this.dateFrom = dateFns.startOfYear(today);
this.dateTo = dateFns.endOfYear(today);
break;
case 'ly':
today = dateFns.subYears(today, 1);
this.date = today = dateFns.subYears(today, 1);
this.dateFrom = dateFns.startOfYear(today);
this.dateTo = dateFns.endOfYear(today);
break;
Expand Down
32 changes: 32 additions & 0 deletions src/ng-daterangepicker/ng-daterangepicker.sass
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,35 @@
border-color: $gray
&:hover, &.is-active
background: $gray


&.theme-orange
&.is-active
border-color: $orange
.input-section
.label-txt
color: $orange
.cal-icon
svg
path
fill: $orange
.calendar
border-color: $orange
&:after
border-top-color: $orange
border-left-color: $orange
.calendar-container
.days
.day
&.is-within-range
background: lighten($orange, 20)
.day-num
&:hover, &.is-active
background: $orange
.side-container
.side-container-buttons
.side-button
border-color: $orange
&:hover, &.is-active
background: $orange

1 change: 1 addition & 0 deletions src/styles/colors.sass
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ $cyan: #0b7285
$grape: #862e9c
$red: #c92a2a
$gray: #212529
$orange: #f26b2a