From 2d8deb21377fa66c9a5542cf074ae2fc32f9782f Mon Sep 17 00:00:00 2001 From: Henning Hall Date: Fri, 27 Dec 2019 11:36:13 +0100 Subject: [PATCH] Re-add prop-types. Remove unsupported ios props. --- DatePickerAndroid.js | 6 +----- DatePickerIOS.js | 35 +---------------------------------- defaultProps.js | 4 ++++ index.d.ts | 21 +-------------------- index.js | 12 +++++++++++- propTypes.js | 23 +++++++++++++++++++++++ 6 files changed, 41 insertions(+), 60 deletions(-) create mode 100644 defaultProps.js create mode 100644 propTypes.js diff --git a/DatePickerAndroid.js b/DatePickerAndroid.js index 99800e1..6436de4 100644 --- a/DatePickerAndroid.js +++ b/DatePickerAndroid.js @@ -10,10 +10,6 @@ const NativeDatePicker = requireNativeComponent( ) class DatePickerAndroid extends React.PureComponent { - static defaultProps = { - mode: 'datetime', - minuteInterval: 1, - } render() { if (__DEV__) throwIfInvalidProps(this.props) @@ -32,7 +28,7 @@ class DatePickerAndroid extends React.PureComponent { _onChange = e => { const jsDate = this._fromIsoWithTimeZoneOffset(e.nativeEvent.date).toDate() - this.props.onDateChange(jsDate) + this.props.onDateChange && this.props.onDateChange(jsDate) } _maximumDate = () => diff --git a/DatePickerIOS.js b/DatePickerIOS.js index e3452eb..c7c99d3 100644 --- a/DatePickerIOS.js +++ b/DatePickerIOS.js @@ -1,36 +1,10 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * - * This is a controlled component version of RCTDatePickerIOS - * - */ - -'use strict' - import React from 'react' import { StyleSheet, View, requireNativeComponent } from 'react-native' import { throwIfInvalidProps } from './propChecker' -const invariant = require('fbjs/lib/invariant') const RCTDatePickerIOS = requireNativeComponent('RNDatePicker') -/** - * Use `DatePickerIOS` to render a date/time picker (selector) on iOS. This is - * a controlled component, so you must hook in to the `onDateChange` callback - * and update the `date` prop in order for the component to update, otherwise - * the user's change will be reverted immediately to reflect `props.date` as the - * source of truth. - */ export default class DatePickerIOS extends React.Component { - static DefaultProps = { - mode: 'datetime', - } - - // $FlowFixMe How to type a native component to be able to call setNativeProps _picker = null componentDidUpdate() { @@ -48,7 +22,6 @@ export default class DatePickerIOS extends React.Component { const nativeTimeStamp = event.nativeEvent.timestamp this.props.onDateChange && this.props.onDateChange(new Date(nativeTimeStamp)) - this.props.onChange && this.props.onChange(event) } render() { @@ -62,13 +35,7 @@ export default class DatePickerIOS extends React.Component { this._picker = picker }} style={[styles.datePickerIOS, props.style]} - date={ - props.date - ? props.date.getTime() - : props.initialDate - ? props.initialDate.getTime() - : undefined - } + date={props.date ? props.date.getTime() : undefined} locale={props.locale ? props.locale : undefined} maximumDate={ props.maximumDate ? props.maximumDate.getTime() : undefined diff --git a/defaultProps.js b/defaultProps.js new file mode 100644 index 0000000..73372a3 --- /dev/null +++ b/defaultProps.js @@ -0,0 +1,4 @@ +export default { + mode: 'datetime', + minuteInterval: 1, +} \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 5a7db15..5e7e2fe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,16 +7,6 @@ interface Props extends ViewProps { */ date?: Date - /** - * Provides an initial value that will change when the user starts selecting - * a date. It is useful for simple use-cases where you do not want to deal - * with listening to events and updating the date prop to keep the - * controlled state in sync. The controlled state has known bugs which - * causes it to go out of sync with native. The initialDate prop is intended - * to allow you to have native be source of truth. - */ - initialDate?: Date - /** * The date picker locale. */ @@ -46,15 +36,6 @@ interface Props extends ViewProps { */ mode?: 'date' | 'time' | 'datetime' - /** - * Date change handler. - * - * This is called when the user changes the date or time in the UI. - * The first and only argument is an Event. For getting the date the picker - * was changed to, use onDateChange instead. - */ - onChange?: (event: object) => void - /** * Date change handler. * @@ -74,7 +55,7 @@ interface Props extends ViewProps { timeZoneOffsetInMinutes?: number /** - * Android picker is fading towords this background color. { color, 'none' } + * Android picker is fading towards this background color. { color, 'none' } */ fadeToColor?: string diff --git a/index.js b/index.js index af7b397..b1c1948 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,15 @@ import { Platform } from 'react-native' import DatePickerIOS from './DatePickerIOS' import DatePickerAndroid from './DatePickerAndroid' +import propTypes from './propTypes' +import defaultProps from './defaultProps' -export default Platform.OS === 'ios' ? DatePickerIOS : DatePickerAndroid +const DatePicker = Platform.select({ + android: DatePickerAndroid, + ios: DatePickerIOS, +}) + +DatePicker.defaultProps = defaultProps +DatePicker.propTypes = propTypes + +export default DatePicker diff --git a/propTypes.js b/propTypes.js new file mode 100644 index 0000000..13c3732 --- /dev/null +++ b/propTypes.js @@ -0,0 +1,23 @@ +import { Platform, ViewPropTypes } from 'react-native' +import PropTypes from 'prop-types' + +const androidProptypes = { + fadeToColor: PropTypes.string, +} + +const DateType = PropTypes.instanceOf(Date) + +export default { + ...(Platform === 'android' ? androidProptypes : {}), + date: DateType.isRequired, + onChange: PropTypes.func, + minimumDate: DateType, + maximumDate: DateType, + mode: PropTypes.oneOf(['date', 'time', 'datetime']), + minuteInterval: 1 | 2 | 3 | 4 | 5 | 6 | 10 | 12 | 15 | 20 | 30, + locale: PropTypes.string, + textColor: PropTypes.string, + timeZoneOffsetInMinutes: PropTypes.number, + testID: ViewPropTypes.testID, + style: ViewPropTypes.style, +}