Skip to content

Make moment an optional dependency #2220

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"hoist-non-react-statics": "^3.0.0",
"lodash": "^4.17.21",
"memoize-one": "^5.0.5",
"moment": "^2.24.0",
"prop-types": "^15.5.10",
"react-freeze": "^1.0.0",
"react-native-color": "0.0.10",
Expand Down Expand Up @@ -95,6 +94,7 @@
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.67.0",
"mocha": "^5.0.0",
"moment": "^2.24.0",
"object-hash": "^3.0.0",
"prettier-eslint": "12.0.0",
"react": "17.0.2",
Expand Down
13 changes: 9 additions & 4 deletions src/components/dateTimePicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import moment from 'moment';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {StyleProp, StyleSheet, ViewStyle} from 'react-native';
import {DateTimePickerPackage as RNDateTimePicker} from '../../optionalDependencies';
import {DateTimePickerPackage as RNDateTimePicker, MomentPackage as moment} from '../../optionalDependencies';
import {useDidUpdate} from 'hooks';
import {Colors} from '../../style';
import Assets from '../../assets';
Expand Down Expand Up @@ -147,6 +146,12 @@ function DateTimePicker(props: DateTimePickerPropsInternal) {
}
}, []);

useEffect(() => {
if (!moment && (dateFormat || timeFormat)) {
console.error(`RNUILib DateTimePicker component with date/time format requires installing "moment" dependency`);
}
}, [dateFormat, timeFormat]);

useDidUpdate(() => {
setValue(propsValue);
}, [propsValue]);
Expand Down Expand Up @@ -175,13 +180,13 @@ function DateTimePicker(props: DateTimePickerPropsInternal) {
case MODES.DATE:
return dateFormatter
? dateFormatter(value)
: dateFormat
: dateFormat && moment
? moment(value).format(dateFormat)
: value.toLocaleDateString();
case MODES.TIME:
return timeFormatter
? timeFormatter(value)
: timeFormat
: timeFormat && moment
? moment(value).format(timeFormat)
: value.toLocaleTimeString();
}
Expand Down
6 changes: 6 additions & 0 deletions src/optionalDependencies/MomentPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let MomentPackage: typeof import('moment') | undefined;
try {
MomentPackage = require('moment');
} catch (error) {}

export default MomentPackage;
1 change: 1 addition & 0 deletions src/optionalDependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export {PickerPackage, CommunityPickerPackage} from './PickerPackage';
export {default as SvgPackage} from './SvgPackage';
export {createShimmerPlaceholder} from './ShimmerPackage';
export {default as LinearGradientPackage} from './LinearGradientPackage';
export {default as MomentPackage} from './MomentPackage';