Skip to content

Commit 9f516cd

Browse files
authored
add dateFormatter prop to dateTimePicker (#1027)
* add dateFormatter prop to dateTimePicker * add timeFormatter prop * refactor * change if to ternary * fix space bug
1 parent 5cb6561 commit 9f516cd

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/components/dateTimePicker/index.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ class DateTimePicker extends BaseComponent {
5656
* The date format for the text display
5757
*/
5858
dateFormat: PropTypes.string,
59+
/**
60+
* A callback function to format date
61+
*/
62+
dateFormatter: PropTypes.func,
5963
/**
6064
* The time format for the text display
6165
*/
6266
timeFormat: PropTypes.string,
67+
/**
68+
* A callback function to format time
69+
*/
70+
timeFormatter: PropTypes.func,
6371
/**
6472
* Allows changing of the locale of the component (iOS only)
6573
*/
@@ -140,17 +148,22 @@ class DateTimePicker extends BaseComponent {
140148

141149
getStringValue = () => {
142150
const {value} = this.state;
143-
const {mode, dateFormat, timeFormat} = this.getThemeProps();
151+
const {mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this.getThemeProps();
144152
if (value) {
145-
const dateString =
146-
mode === MODES.DATE
147-
? dateFormat
148-
? moment(value).format(dateFormat)
149-
: value.toLocaleDateString()
150-
: timeFormat
151-
? moment(value).format(timeFormat)
152-
: value.toLocaleTimeString();
153-
return dateString;
153+
switch (mode) {
154+
case MODES.DATE:
155+
return dateFormatter
156+
? dateFormatter(value)
157+
: dateFormat
158+
? moment(value).format(dateFormat)
159+
: value.toLocaleDateString();
160+
case MODES.TIME:
161+
return timeFormatter
162+
? timeFormatter(value)
163+
: timeFormat
164+
? moment(value).format(timeFormat)
165+
: value.toLocaleTimeString();
166+
}
154167
}
155168
};
156169

@@ -191,12 +204,7 @@ class DateTimePicker extends BaseComponent {
191204
iconStyle={{tintColor: Colors.dark10}}
192205
onPress={this.toggleExpandableOverlay}
193206
/>
194-
<Button
195-
link
196-
iconSource={Assets.icons.check}
197-
useCustomTheme={useCustomTheme}
198-
onPress={this.onDonePressed}
199-
/>
207+
<Button link iconSource={Assets.icons.check} useCustomTheme={useCustomTheme} onPress={this.onDonePressed}/>
200208
</View>
201209
);
202210
}

0 commit comments

Comments
 (0)