Skip to content

Move moment to be optional dependency #2485

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

Merged
merged 1 commit into from
Feb 15, 2023
Merged
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 @@ -46,7 +46,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-redash": "^12.0.3",
Expand Down Expand Up @@ -100,6 +99,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 @@ -151,6 +150,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 @@ -179,13 +184,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
@@ -1,6 +1,7 @@
export {default as DateTimePickerPackage} from './DateTimePickerPackage';
export {default as FlashListPackage} from './FlashListPackage';
export {default as BlurViewPackage} from './BlurViewPackage';
export {default as MomentPackage} from './MomentPackage';
export {default as NetInfoPackage} from './NetInfoPackage';
export {default as HapticFeedbackPackage} from './HapticFeedbackPackage';
export {default as SvgPackage} from './SvgPackage';
Expand Down