Skip to content

Commit bc381b8

Browse files
committed
feat: add deprecation warning for onChange prop
1 parent 129388e commit bc381b8

6 files changed

Lines changed: 39 additions & 8 deletions

File tree

example/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ export const App = () => {
139139
};
140140

141141
const onValueChange = (selectedDate) => {
142+
if (Platform.OS === 'android') {
143+
setShow(false);
144+
}
142145
setDate(selectedDate);
143146
};
144147

src/DateTimePickerAndroid.android.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
createNeutralEvtParams,
2626
} from './eventCreators';
2727
import {processColor} from 'react-native';
28+
import {warnIfOnChangeIsUsed} from './utils';
2829

2930
function open(props: AndroidNativeProps) {
3031
const {
@@ -54,6 +55,7 @@ function open(props: AndroidNativeProps) {
5455
startOnYearSelection,
5556
} = props;
5657
validateAndroidProps(props);
58+
warnIfOnChangeIsUsed(onChange);
5759
invariant(originalValue, 'A date or time must be specified as `value` prop.');
5860

5961
const valueTimestamp = originalValue.getTime();

src/datetimepicker.android.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ export default function RNDateTimePickerAndroid(
8181
// as an alternative, use the DateTimePickerAndroid whose reason for existence is described in
8282
// https://github.com/react-native-datetimepicker/datetimepicker/pull/327#issuecomment-723160992
8383
// eslint-disable-next-line react-hooks/exhaustive-deps
84-
[onChange, onValueChange, onDismiss, onNeutralButtonPress, valueTimestamp, mode],
84+
[
85+
onChange,
86+
onValueChange,
87+
onDismiss,
88+
onNeutralButtonPress,
89+
valueTimestamp,
90+
mode,
91+
],
8592
);
8693

8794
return null;

src/datetimepicker.ios.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
* @flow strict-local
1111
*/
1212
import RNDateTimePicker from './picker';
13-
import {dateToMilliseconds, sharedPropsValidation} from './utils';
13+
import {
14+
dateToMilliseconds,
15+
sharedPropsValidation,
16+
warnIfOnChangeIsUsed,
17+
} from './utils';
1418
import {
1519
IOS_DISPLAY,
1620
EVENT_TYPE_SET,
@@ -63,7 +67,14 @@ export default function Picker({
6367
disabled = false,
6468
...other
6569
}: IOSNativeProps): React.Node {
66-
sharedPropsValidation({value, timeZoneOffsetInMinutes, timeZoneName, minimumDate, maximumDate});
70+
sharedPropsValidation({
71+
value,
72+
timeZoneOffsetInMinutes,
73+
timeZoneName,
74+
minimumDate,
75+
maximumDate,
76+
});
77+
warnIfOnChangeIsUsed(onChange);
6778

6879
const display = getDisplaySafe(providedDisplay);
6980

src/datetimepicker.windows.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
*/
77
'use strict';
88

9-
import {
10-
requireNativeComponent,
11-
StyleSheet,
12-
} from 'react-native';
9+
import {requireNativeComponent, StyleSheet} from 'react-native';
1310
import type {
1411
WindowsNativeProps,
1512
WindowsDatePickerChangeEvent,
1613
DateTimePickerEvent,
1714
} from './types';
1815
import * as React from 'react';
1916
import {EVENT_TYPE_SET, WINDOWS_MODE} from './constants';
20-
import {sharedPropsValidation} from './utils';
17+
import {sharedPropsValidation, warnIfOnChangeIsUsed} from './utils';
2118

2219
const styles = StyleSheet.create({
2320
rnDatePicker: {
@@ -38,6 +35,7 @@ export default function RNDateTimePickerQWE(
3835
props: WindowsNativeProps,
3936
): React.Node {
4037
sharedPropsValidation({value: props?.value});
38+
warnIfOnChangeIsUsed(props.onChange);
4139

4240
const localProps = {
4341
accessibilityLabel: props.accessibilityLabel,

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ export function sharedPropsValidation({
6767
);
6868
}
6969
}
70+
71+
let hasWarnedOnChange = false;
72+
export function warnIfOnChangeIsUsed(onChange: ?Function) {
73+
if (__DEV__ && onChange && !hasWarnedOnChange) {
74+
hasWarnedOnChange = true;
75+
console.warn(
76+
'DateTimePicker: `onChange` is deprecated. Use `onValueChange`, `onDismiss`, and `onNeutralButtonPress` instead.',
77+
);
78+
}
79+
}

0 commit comments

Comments
 (0)