Skip to content

Commit 1d7a341

Browse files
authored
feat(ui): calendar - start day of week
1 parent 75e42dd commit 1d7a341

File tree

10 files changed

+121
-21
lines changed

10 files changed

+121
-21
lines changed

src/date-fns/dateFnsDate.service.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('@date-fns: service checks', () => {
1212
let dateService: DateService<Date>;
1313

1414
beforeEach(() => {
15-
dateService = new DateFnsService('en', null, null);
15+
dateService = new DateFnsService('en', null);
1616
});
1717

1818
it('* should parse date according to the MM.dd.yyyy format', () => {
@@ -37,7 +37,6 @@ describe('@date-fns: service checks', () => {
3737
beforeEach(() => {
3838
dateService = new DateFnsService(
3939
'en',
40-
null,
4140
{
4241
format: FORMAT,
4342
parseOptions: {},

src/date-fns/dateFnsDate.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import {
88
I18nConfig,
99
NativeDateService,
10+
NativeDateServiceOptions,
1011
} from 'react-native-ui-kitten';
1112
// @ts-ignore
1213
import dateFnsParse, { default as rollupParse } from 'date-fns/parse';
@@ -16,7 +17,7 @@ import dateFnsFormat, { default as rollupFormat } from 'date-fns/format';
1617
const parse = rollupParse || dateFnsParse;
1718
const formatDate = rollupFormat || dateFnsFormat;
1819

19-
export interface DateFnsOptions {
20+
export interface DateFnsOptions extends NativeDateServiceOptions {
2021
format: string;
2122
parseOptions: {};
2223
formatOptions: {};
@@ -28,8 +29,8 @@ export class DateFnsService extends NativeDateService {
2829
format: `DD/MM/YYYY`,
2930
};
3031

31-
constructor(locale: string = 'en', i18n?: I18nConfig, options?: DateFnsOptions) {
32-
super(locale, i18n);
32+
constructor(locale: string = 'en', options?: DateFnsOptions) {
33+
super(locale, options);
3334
this.options = options || this.options;
3435
}
3536

src/framework/ui/calendar/calendar.component.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export type CalendarElement<D> = React.ReactElement<CalendarProps<D>>;
6262
*
6363
* @overview-example CalendarBounds
6464
*
65+
* @overview-example CalendarStartDayOfWeek
66+
*
6567
* @overview-example CalendarCustomLocale
6668
*
6769
* @example CalendarMoment

src/framework/ui/calendar/service/nativeDate.service.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('@native-date: service checks', () => {
3333
expect((<any>dateService).locale).toBe('en');
3434
});
3535

36-
it('* should be initialized with ru locale', () => {
37-
dateService = new NativeDateService('zh', i18n);
36+
it('* should be initialized with zh locale', () => {
37+
dateService = new NativeDateService('zh', { i18n });
3838
expect((<any>dateService).locale).toBe('zh');
3939
});
4040

@@ -87,13 +87,18 @@ describe('@native-date: service checks', () => {
8787
expect(dateService.getFirstDayOfWeek()).toBe(0);
8888
});
8989

90+
it('* should get custom first day of week if provided', () => {
91+
dateService = new NativeDateService('en', { startDayOfWeek: 1 });
92+
expect(dateService.getFirstDayOfWeek()).toBe(1);
93+
});
94+
9095
it('* should get month name', () => {
9196
const month = new Date(2018, 5, 15);
9297
expect(dateService.getMonthName(month)).toBe('Jun');
9398
});
9499

95100
it('* should get i18n month name', () => {
96-
dateService = new NativeDateService('zh', i18n);
101+
dateService = new NativeDateService('zh', { i18n });
97102
const month = new Date(2018, 5, 15);
98103
expect(dateService.getMonthName(month, TranslationWidth.SHORT)).toBe('6月');
99104
});
@@ -106,8 +111,16 @@ describe('@native-date: service checks', () => {
106111
expect(dateService.getDayOfWeekNames()).toEqual(EN.dayNames.short);
107112
});
108113

114+
it('* should get day of week names', () => {
115+
const startDayOfWeek = 1;
116+
dateService = new NativeDateService('en', { startDayOfWeek });
117+
118+
const { [0]: startDayOfWeekName } = dateService.getDayOfWeekNames();
119+
expect(startDayOfWeekName).toEqual(EN.dayNames.short[startDayOfWeek]);
120+
});
121+
109122
it('* should get i18n day of week names', () => {
110-
dateService = new NativeDateService('zh', i18n);
123+
dateService = new NativeDateService('zh', { i18n});
111124
expect(dateService.getDayOfWeekNames(TranslationWidth.SHORT)).toEqual(i18n.dayNames.short);
112125
});
113126

src/framework/ui/calendar/service/nativeDate.service.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ import {
1313
import { EN } from '../i18n/en';
1414

1515
export const LOCALE_DEFAULT = 'en';
16-
export const FIRST_DAY_OF_WEEK: number = 0;
16+
17+
export interface NativeDateServiceOptions {
18+
// 0 for Sunday, 1 for Monday, etc
19+
startDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
20+
i18n?: I18nConfig;
21+
}
22+
23+
const DEFAULT_OPTIONS: NativeDateServiceOptions = {
24+
startDayOfWeek: 0,
25+
};
1726

1827
/**
1928
* The `NativeDateService` is basic implementation of `DateService` using
2029
* native js date objects.
2130
*/
2231
export class NativeDateService extends DateService<Date> {
2332

24-
constructor(locale: string = LOCALE_DEFAULT, i18n?: I18nConfig) {
33+
protected options: NativeDateServiceOptions;
34+
35+
constructor(locale: string = LOCALE_DEFAULT, options?: NativeDateServiceOptions) {
2536
super();
26-
super.setLocale(i18n ? locale : LOCALE_DEFAULT);
27-
this.setFechaLocaleData(i18n || EN);
37+
this.options = { ...DEFAULT_OPTIONS, ...options };
38+
super.setLocale(this.options.i18n ? locale : LOCALE_DEFAULT);
39+
this.setFechaLocaleData(this.options.i18n || EN);
2840
}
2941

3042
public setLocale(locale: string) {
@@ -60,7 +72,7 @@ export class NativeDateService extends DateService<Date> {
6072
* and 0 if from sunday and so on.
6173
* */
6274
public getFirstDayOfWeek(): number {
63-
return FIRST_DAY_OF_WEEK;
75+
return this.options.startDayOfWeek;
6476
}
6577

6678
public getMonthName(date: Date, style: TranslationWidth = TranslationWidth.SHORT): string {
@@ -74,7 +86,10 @@ export class NativeDateService extends DateService<Date> {
7486
}
7587

7688
public getDayOfWeekNames(style: TranslationWidth = TranslationWidth.SHORT): string[] {
77-
return this.getFechaDayNames(style);
89+
const dayNames: string[] = this.getFechaDayNames(style);
90+
91+
// avoid mutation of source array
92+
return this.shiftDayOfWeekNames([...dayNames], this.options.startDayOfWeek);
7893
}
7994

8095
public format(date: Date, format: string): string {
@@ -172,7 +187,11 @@ export class NativeDateService extends DateService<Date> {
172187
return 'native';
173188
}
174189

175-
private getFechaDayNames(style: TranslationWidth) {
190+
protected shiftDayOfWeekNames<T>(value: T[], offset: number): T[] {
191+
return value.splice(offset).concat(value);
192+
}
193+
194+
private getFechaDayNames(style: TranslationWidth): string[] {
176195
switch (style) {
177196
case TranslationWidth.SHORT:
178197
return fecha.i18n.dayNamesShort;
@@ -181,7 +200,7 @@ export class NativeDateService extends DateService<Date> {
181200
}
182201
}
183202

184-
private getFechaMonthNames(style: TranslationWidth) {
203+
private getFechaMonthNames(style: TranslationWidth): string[] {
185204
switch (style) {
186205
case TranslationWidth.SHORT:
187206
return fecha.i18n.monthNamesShort;

src/framework/ui/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,11 @@ export {
204204
ViewPagerElement,
205205
} from './viewPager/viewPager.component';
206206
export { DateService } from './calendar/service/date.service';
207-
export { NativeDateService } from './calendar/service/nativeDate.service';
208-
export { TranslationWidth, I18nConfig } from './calendar/i18n/type';
207+
export {
208+
NativeDateService,
209+
NativeDateServiceOptions,
210+
} from './calendar/service/nativeDate.service';
211+
export {
212+
TranslationWidth,
213+
I18nConfig,
214+
} from './calendar/i18n/type';

src/playground/src/ui/screen/calendar/type.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import moment from 'moment';
2-
import { CalendarViewModes } from 'react-native-ui-kitten';
2+
import {
3+
CalendarViewModes,
4+
NativeDateService,
5+
} from 'react-native-ui-kitten';
36
import { MomentDateService } from '@ui-kitten/moment';
47
import { DateFnsService } from '@ui-kitten/date-fns';
58
import {
@@ -81,6 +84,15 @@ const filterCalendar: ComponentShowcaseItem = {
8184
},
8285
};
8386

87+
const mondayCalendar: ComponentShowcaseItem = {
88+
props: {
89+
...defaultCalendar.props,
90+
dateService: new NativeDateService('en', {
91+
startDayOfWeek: 1,
92+
}),
93+
},
94+
};
95+
8496
const defaultSection: ComponentShowcaseSection = {
8597
title: 'Default',
8698
items: [
@@ -144,9 +156,17 @@ const filterSection: ComponentShowcaseSection = {
144156
],
145157
};
146158

159+
const startDayOfWeekSection: ComponentShowcaseSection = {
160+
title: 'Start Day of Week',
161+
items: [
162+
mondayCalendar,
163+
],
164+
};
165+
147166
export const calendarShowcase: ComponentShowcase = {
148167
sections: [
149168
defaultSection,
169+
// startDayOfWeekSection,
150170
// customItemSection,
151171
// momentSection,
152172
// dateFnsSection,

src/playground/src/ui/screen/showcases/calendar/calendarCustomLocale.component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class CalendarCustomLocaleShowcase extends React.Component {
2323
date: new Date(),
2424
};
2525

26-
dateService = new NativeDateService('zh', i18n);
26+
dateService = new NativeDateService('zh', { i18n });
2727

2828
onSelect = (date) => {
2929
this.setState({ date });
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import { StyleSheet } from 'react-native';
3+
import {
4+
Calendar,
5+
Layout,
6+
NativeDateService,
7+
} from 'react-native-ui-kitten';
8+
9+
export class CalendarStartDayOfWeekShowcase extends React.Component {
10+
11+
state = {
12+
date: new Date(),
13+
};
14+
15+
dateService = new NativeDateService('en', { startDayOfWeek: 1 });
16+
17+
onSelect = (date) => {
18+
this.setState({ date });
19+
};
20+
21+
render() {
22+
return (
23+
<Layout style={styles.container}>
24+
<Calendar
25+
date={this.state.date}
26+
dateService={this.dateService}
27+
onSelect={this.onSelect}
28+
/>
29+
</Layout>
30+
);
31+
}
32+
}
33+
34+
const styles = StyleSheet.create({
35+
container: {
36+
padding: 16,
37+
minHeight: 376,
38+
},
39+
});

src/playground/src/ui/screen/showcases/calendar/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export { CalendarCustomLocaleShowcase } from './calendarCustomLocale.component';
55
export { CalendarFilterShowcase } from './calendarFilterShowcase.component';
66
export { CalendarMomentShowcase } from './calendarMoment.component';
77
export { CalendarSimpleUsageShowcase } from './calendarSimpleUsage.component';
8+
export { CalendarStartDayOfWeekShowcase } from './calendarStartDayOfWeek.component';
89
export { RangeCalendarSimpleUsageShowcase } from './rangeCalendarSimpleUsage.component';

0 commit comments

Comments
 (0)