Browse Source

Merge pull request #138 from henninghall/prop-types-fixes

Re-add prop-types. Remove unsupported ios props.
master
Henning Hall 5 years ago
committed by GitHub
parent
commit
ddc47d53a5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 60 deletions
  1. +1
    -5
      DatePickerAndroid.js
  2. +1
    -34
      DatePickerIOS.js
  3. +4
    -0
      defaultProps.js
  4. +1
    -20
      index.d.ts
  5. +11
    -1
      index.js
  6. +23
    -0
      propTypes.js

+ 1
- 5
DatePickerAndroid.js View File

@ -10,10 +10,6 @@ const NativeDatePicker = requireNativeComponent(
) )
class DatePickerAndroid extends React.PureComponent { class DatePickerAndroid extends React.PureComponent {
static defaultProps = {
mode: 'datetime',
minuteInterval: 1,
}
render() { render() {
if (__DEV__) throwIfInvalidProps(this.props) if (__DEV__) throwIfInvalidProps(this.props)
@ -32,7 +28,7 @@ class DatePickerAndroid extends React.PureComponent {
_onChange = e => { _onChange = e => {
const jsDate = this._fromIsoWithTimeZoneOffset(e.nativeEvent.date).toDate() const jsDate = this._fromIsoWithTimeZoneOffset(e.nativeEvent.date).toDate()
this.props.onDateChange(jsDate)
this.props.onDateChange && this.props.onDateChange(jsDate)
} }
_maximumDate = () => _maximumDate = () =>

+ 1
- 34
DatePickerIOS.js View File

@ -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 React from 'react'
import { StyleSheet, View, requireNativeComponent } from 'react-native' import { StyleSheet, View, requireNativeComponent } from 'react-native'
import { throwIfInvalidProps } from './propChecker' import { throwIfInvalidProps } from './propChecker'
const invariant = require('fbjs/lib/invariant')
const RCTDatePickerIOS = requireNativeComponent('RNDatePicker') 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 { 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 _picker = null
componentDidUpdate() { componentDidUpdate() {
@ -48,7 +22,6 @@ export default class DatePickerIOS extends React.Component {
const nativeTimeStamp = event.nativeEvent.timestamp const nativeTimeStamp = event.nativeEvent.timestamp
this.props.onDateChange && this.props.onDateChange &&
this.props.onDateChange(new Date(nativeTimeStamp)) this.props.onDateChange(new Date(nativeTimeStamp))
this.props.onChange && this.props.onChange(event)
} }
render() { render() {
@ -62,13 +35,7 @@ export default class DatePickerIOS extends React.Component {
this._picker = picker this._picker = picker
}} }}
style={[styles.datePickerIOS, props.style]} 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} locale={props.locale ? props.locale : undefined}
maximumDate={ maximumDate={
props.maximumDate ? props.maximumDate.getTime() : undefined props.maximumDate ? props.maximumDate.getTime() : undefined

+ 4
- 0
defaultProps.js View File

@ -0,0 +1,4 @@
export default {
mode: 'datetime',
minuteInterval: 1,
}

+ 1
- 20
index.d.ts View File

@ -7,16 +7,6 @@ interface Props extends ViewProps {
*/ */
date?: Date 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. * The date picker locale.
*/ */
@ -46,15 +36,6 @@ interface Props extends ViewProps {
*/ */
mode?: 'date' | 'time' | 'datetime' 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. * Date change handler.
* *
@ -74,7 +55,7 @@ interface Props extends ViewProps {
timeZoneOffsetInMinutes?: number timeZoneOffsetInMinutes?: number
/** /**
* Android picker is fading towords this background color. { color, 'none' }
* Android picker is fading towards this background color. { color, 'none' }
*/ */
fadeToColor?: string fadeToColor?: string

+ 11
- 1
index.js View File

@ -1,5 +1,15 @@
import { Platform } from 'react-native' import { Platform } from 'react-native'
import DatePickerIOS from './DatePickerIOS' import DatePickerIOS from './DatePickerIOS'
import DatePickerAndroid from './DatePickerAndroid' 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

+ 23
- 0
propTypes.js View File

@ -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,
}

Loading…
Cancel
Save