Skip to content

Commit 705dc97

Browse files
nutritiousrejectartyorsh
authored andcommitted
feat(components): date-service - add ability to specify date format
1 parent 8b33c91 commit 705dc97

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

Diff for: src/components/ui/calendar/service/nativeDate.service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export const LOCALE_DEFAULT = 'en';
1717
export interface NativeDateServiceOptions {
1818
// 0 for Sunday, 1 for Monday, etc
1919
startDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
20+
format?: string;
2021
i18n?: I18nConfig;
2122
}
2223

2324
const DEFAULT_OPTIONS: NativeDateServiceOptions = {
25+
format: 'DD/MM/YYYY',
2426
startDayOfWeek: 0,
2527
};
2628

@@ -93,7 +95,7 @@ export class NativeDateService extends DateService<Date> {
9395
}
9496

9597
public format(date: Date, format: string): string {
96-
return fecha.format(date, format);
98+
return fecha.format(date, format || this.options.format);
9799
}
98100

99101
/**

Diff for: src/components/ui/datepicker/baseDatepicker.component.tsx

-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535

3636
type IconProp = (style: StyleType) => IconElement;
3737

38-
3938
export interface BaseDatepickerProps<D = Date> extends StyledComponentProps,
4039
TouchableOpacityProps,
4140
BaseCalendarProps<D> {
@@ -57,8 +56,6 @@ interface State {
5756
visible: boolean;
5857
}
5958

60-
const FULL_DATE_FORMAT_STRING: string = 'DD/MM/YYYY';
61-
6259
export abstract class BaseDatepickerComponent<P, D = Date> extends React.Component<BaseDatepickerProps<D> & P, State> {
6360

6461
static defaultProps: Partial<BaseDatepickerProps> = {
@@ -99,10 +96,6 @@ export abstract class BaseDatepickerComponent<P, D = Date> extends React.Compone
9996

10097
protected abstract renderCalendar(): CalendarElement<D> | RangeCalendarElement<D>;
10198

102-
protected formatDateToString(date: D): string {
103-
return this.props.dateService.format(date, FULL_DATE_FORMAT_STRING);
104-
}
105-
10699
private getComponentStyle = (style: StyleType): StyleType => {
107100
const {
108101
textMarginHorizontal,
@@ -356,4 +349,3 @@ const styles = StyleSheet.create({
356349
textAlign: 'left',
357350
},
358351
});
359-

Diff for: src/components/ui/datepicker/datepicker.component.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export type DatepickerElement<D = Date> = React.ReactElement<DatepickerProps<D>>
112112
*
113113
* @example DatepickerCustomLocale
114114
*
115+
* @example DatepickerDateFormat
116+
*
115117
* @example DatepickerMoment
116118
*/
117119

@@ -151,7 +153,7 @@ export class DatepickerComponent<D = Date> extends BaseDatepickerComponent<Datep
151153

152154
protected getComponentTitle(): string {
153155
if (this.props.date) {
154-
return this.formatDateToString(this.props.date);
156+
return this.props.dateService.format(this.props.date, null);
155157
} else {
156158
return this.props.placeholder;
157159
}

Diff for: src/components/ui/datepicker/rangeDatepicker.component.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export class RangeDatepickerComponent<D = Date> extends BaseDatepickerComponent<
136136
const { startDate, endDate } = this.props.range;
137137

138138
if (startDate || endDate) {
139-
const start: string = startDate ? this.formatDateToString(startDate) : '';
140-
const end: string = endDate ? this.formatDateToString(endDate) : '';
139+
const start: string = startDate ? this.props.dateService.format(startDate, null) : '';
140+
const end: string = endDate ? this.props.dateService.format(endDate, null) : '';
141141

142142
return `${start} - ${end}`;
143143
} else {

Diff for: src/date-fns/dateFnsDate.service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const parse = rollupParse || dateFnsParse;
1818
const formatDate = rollupFormat || dateFnsFormat;
1919

2020
export interface DateFnsOptions extends NativeDateServiceOptions {
21-
format: string;
2221
parseOptions: {};
2322
formatOptions: {};
2423
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { StyleSheet } from 'react-native';
3+
import {
4+
Datepicker,
5+
Layout,
6+
NativeDateService,
7+
} from '@ui-kitten/components';
8+
9+
const dateService = new NativeDateService('en', { format: 'DD.MM.YYYY' });
10+
11+
export const DatepickerDateFormatShowcase = () => {
12+
13+
const [selectedDate, setSelectedDate] = React.useState(null);
14+
15+
return (
16+
<Layout style={styles.container}>
17+
<Datepicker
18+
placeholder='Pick Date'
19+
date={selectedDate}
20+
onSelect={setSelectedDate}
21+
dateService={dateService}
22+
/>
23+
</Layout>
24+
);
25+
};
26+
27+
const styles = StyleSheet.create({
28+
container: {
29+
minHeight: 376,
30+
},
31+
});

0 commit comments

Comments
 (0)