react-native-date-picker
Advanced tools
Comparing version 2.7.0 to 2.7.1
@@ -1,15 +0,14 @@ | ||
import React from 'react'; | ||
import { DatePickerIOS, requireNativeComponent, StyleSheet } from 'react-native'; | ||
import React from 'react' | ||
import { StyleSheet, requireNativeComponent } from 'react-native' | ||
import moment from 'moment' | ||
import { throwIfInvalidProps } from "./propChecker" | ||
import { throwIfInvalidProps } from './propChecker' | ||
const NativeDatePicker = requireNativeComponent(`DatePickerManager`, DatePickerAndroid, { nativeOnly: { onChange: true } }); | ||
const NativeDatePicker = requireNativeComponent( | ||
`DatePickerManager`, | ||
DatePickerAndroid, | ||
{ nativeOnly: { onChange: true } } | ||
) | ||
class DatePickerAndroid extends React.PureComponent { | ||
static defaultProps = { | ||
mode: 'datetime', | ||
minuteInterval: 1, | ||
}; | ||
render() { | ||
@@ -32,16 +31,21 @@ if (__DEV__) throwIfInvalidProps(this.props) | ||
const jsDate = this._fromIsoWithTimeZoneOffset(e.nativeEvent.date).toDate() | ||
this.props.onDateChange(jsDate) | ||
this.props.onDateChange && this.props.onDateChange(jsDate) | ||
} | ||
_maximumDate = () => this.props.maximumDate && this._toIsoWithTimeZoneOffset(this.props.maximumDate); | ||
_maximumDate = () => | ||
this.props.maximumDate && | ||
this._toIsoWithTimeZoneOffset(this.props.maximumDate) | ||
_minimumDate = () => this.props.minimumDate && this._toIsoWithTimeZoneOffset(this.props.minimumDate); | ||
_minimumDate = () => | ||
this.props.minimumDate && | ||
this._toIsoWithTimeZoneOffset(this.props.minimumDate) | ||
_date = () => this._toIsoWithTimeZoneOffset(this.props.date); | ||
_date = () => this._toIsoWithTimeZoneOffset(this.props.date) | ||
_fromIsoWithTimeZoneOffset = date => { | ||
if (this.props.timeZoneOffsetInMinutes === undefined) | ||
return moment(date) | ||
if (this.props.timeZoneOffsetInMinutes === undefined) return moment(date) | ||
return moment.utc(date).subtract(this.props.timeZoneOffsetInMinutes, 'minutes') | ||
return moment | ||
.utc(date) | ||
.subtract(this.props.timeZoneOffsetInMinutes, 'minutes') | ||
} | ||
@@ -53,5 +57,7 @@ | ||
return moment.utc(date).add(this.props.timeZoneOffsetInMinutes, 'minutes').toISOString() | ||
return moment | ||
.utc(date) | ||
.add(this.props.timeZoneOffsetInMinutes, 'minutes') | ||
.toISOString() | ||
} | ||
} | ||
@@ -63,7 +69,5 @@ | ||
height: 180, | ||
} | ||
}, | ||
}) | ||
DatePickerAndroid.propTypes = DatePickerIOS.propTypes; | ||
export default DatePickerAndroid; | ||
export default DatePickerAndroid |
@@ -1,124 +0,17 @@ | ||
/** | ||
* 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 | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
import React from 'react' | ||
import { StyleSheet, View, requireNativeComponent } from 'react-native' | ||
import { throwIfInvalidProps } from './propChecker' | ||
'use strict'; | ||
const RCTDatePickerIOS = requireNativeComponent('RNDatePicker') | ||
import React from 'react'; | ||
import { StyleSheet, View, requireNativeComponent } from 'react-native'; | ||
import { throwIfInvalidProps } from "./propChecker" | ||
const invariant = require('fbjs/lib/invariant'); | ||
export default class DatePickerIOS extends React.Component { | ||
_picker = null | ||
import type { ViewProps } from 'ViewPropTypes'; | ||
const RCTDatePickerIOS = requireNativeComponent('RNDatePicker'); | ||
type Event = Object; | ||
type Props = $ReadOnly<{| | ||
...ViewProps, | ||
/** | ||
* The currently selected 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. | ||
*/ | ||
locale ?: ? string, | ||
/** | ||
* Maximum date. | ||
* | ||
* Restricts the range of possible date/time values. | ||
*/ | ||
maximumDate ?: ? Date, | ||
/** | ||
* Minimum date. | ||
* | ||
* Restricts the range of possible date/time values. | ||
*/ | ||
minimumDate ?: ? Date, | ||
/** | ||
* The interval at which minutes can be selected. | ||
*/ | ||
minuteInterval ?: ? (1 | 2 | 3 | 4 | 5 | 6 | 10 | 12 | 15 | 20 | 30), | ||
/** | ||
* The date picker mode. | ||
*/ | ||
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: Event) => void, | ||
/** | ||
* Date change handler. | ||
* | ||
* This is called when the user changes the date or time in the UI. | ||
* The first and only argument is a Date object representing the new | ||
* date and time. | ||
*/ | ||
onDateChange: (date: Date) => void, | ||
/** | ||
* Timezone offset in minutes. | ||
* | ||
* By default, the date picker will use the device's timezone. With this | ||
* parameter, it is possible to force a certain timezone offset. For | ||
* instance, to show times in Pacific Standard Time, pass -7 * 60. | ||
*/ | ||
timeZoneOffsetInMinutes ?: ? number, | ||
|}>; | ||
/** | ||
* 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<Props> { | ||
static DefaultProps = { | ||
mode: 'datetime', | ||
}; | ||
// $FlowFixMe How to type a native component to be able to call setNativeProps | ||
_picker: ?React.ElementRef<typeof RCTDatePickerIOS> = null; | ||
componentDidUpdate() { | ||
if (this.props.date) { | ||
const propsTimeStamp = this.props.date.getTime(); | ||
const propsTimeStamp = this.props.date.getTime() | ||
if (this._picker) { | ||
this._picker.setNativeProps({ | ||
date: propsTimeStamp, | ||
}); | ||
}) | ||
} | ||
@@ -128,30 +21,20 @@ } | ||
_onChange = (event: Event) => { | ||
const nativeTimeStamp = event.nativeEvent.timestamp; | ||
_onChange = event => { | ||
const nativeTimeStamp = event.nativeEvent.timestamp | ||
this.props.onDateChange && | ||
this.props.onDateChange(new Date(nativeTimeStamp)); | ||
this.props.onChange && this.props.onChange(event); | ||
}; | ||
this.props.onDateChange(new Date(nativeTimeStamp)) | ||
} | ||
render() { | ||
const props = this.props; | ||
const props = this.props | ||
if (__DEV__) throwIfInvalidProps(props) | ||
invariant( | ||
props.date || props.initialDate, | ||
'A selected date or initial date should be specified.', | ||
); | ||
return ( | ||
<RCTDatePickerIOS | ||
testID={this.props.testID} | ||
key={this.props.textColor} // preventing "Today" string keep old text color when text color changes | ||
ref={picker => { | ||
this._picker = picker; | ||
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} | ||
@@ -172,3 +55,3 @@ maximumDate={ | ||
/> | ||
); | ||
) | ||
} | ||
@@ -182,2 +65,2 @@ } | ||
}, | ||
}); | ||
}) |
@@ -11,12 +11,2 @@ import { Component } from 'react' | ||
/** | ||
* 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. | ||
@@ -54,11 +44,2 @@ */ | ||
* 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. | ||
* | ||
* This is called when the user changes the date or time in the UI. | ||
* The first and only argument is a Date object representing the new | ||
@@ -77,10 +58,10 @@ * date and time. | ||
timeZoneOffsetInMinutes?: number | ||
/** | ||
* Android picker is fading towords this background color. { color, 'none' } | ||
*/ | ||
* Android picker is fading towards this background color. { color, 'none' } | ||
*/ | ||
fadeToColor?: string | ||
/** | ||
* Changes the text color. | ||
* Changes the text color. | ||
*/ | ||
@@ -90,4 +71,2 @@ textColor?: string | ||
class DatePicker extends Component<Props> {} | ||
export default DatePicker | ||
export default class DatePicker extends Component<Props> {} |
17
index.js
@@ -1,6 +0,15 @@ | ||
import { Platform } from 'react-native'; | ||
import DatePickerIOS from './DatePickerIOS'; | ||
import DatePickerAndroid from './DatePickerAndroid'; | ||
import { Platform } from 'react-native' | ||
import DatePickerIOS from './DatePickerIOS' | ||
import DatePickerAndroid from './DatePickerAndroid' | ||
import propTypes from './propTypes' | ||
import defaultProps from './defaultProps' | ||
const DatePicker = Platform.select({ | ||
android: DatePickerAndroid, | ||
ios: DatePickerIOS, | ||
}) | ||
export default Platform.OS === 'ios' ? DatePickerIOS : DatePickerAndroid | ||
DatePicker.defaultProps = defaultProps | ||
DatePicker.propTypes = propTypes | ||
export default DatePicker |
{ | ||
"name": "react-native-date-picker", | ||
"version": "2.7.0", | ||
"version": "2.7.1", | ||
"description": "A Cross Platform React Native Picker", | ||
@@ -29,3 +29,6 @@ "main": "index.js", | ||
"react-native-date-picker" | ||
] | ||
], | ||
"devDependencies": { | ||
"prettier": "^1.18.2" | ||
} | ||
} |
export function throwIfInvalidProps(props) { | ||
checks.forEach(check => check.validate(props)) | ||
checks.forEach(check => check.validate(props)) | ||
} | ||
class PropCheck { | ||
constructor(isInvalid, errorText) { | ||
this.isInvalid = isInvalid | ||
this.errorText = errorText | ||
constructor(isInvalid, errorText) { | ||
this.isInvalid = isInvalid | ||
this.errorText = errorText | ||
} | ||
validate = props => { | ||
if (this.isInvalid(props)) { | ||
throw new Error( | ||
`${this.errorText} Check usage of react-native-date-picker.` | ||
) | ||
} | ||
validate = (props) => { | ||
if (this.isInvalid(props)) { | ||
throw new Error(`${this.errorText} Check usage of react-native-date-picker.`) | ||
} | ||
} | ||
} | ||
} | ||
const widthCheck = new PropCheck( | ||
(props) => props && props.style && props.style.width && typeof props.style.width !== "number", | ||
"Invalid style: width. Width needs to be a number. Percentages or other values are not supported." | ||
props => | ||
props && | ||
props.style && | ||
props.style.width && | ||
typeof props.style.width !== 'number', | ||
'Invalid style: width. Width needs to be a number. Percentages or other values are not supported.' | ||
) | ||
const heightCheck = new PropCheck( | ||
(props) => props && props.style && props.style.height && typeof props.style.height !== "number", | ||
"Invalid style: height. Height needs to be a number. Percentages or other values are not supported." | ||
props => | ||
props && | ||
props.style && | ||
props.style.height && | ||
typeof props.style.height !== 'number', | ||
'Invalid style: height. Height needs to be a number. Percentages or other values are not supported.' | ||
) | ||
const modeCheck = new PropCheck( | ||
(props) => props && props.mode && !["datetime", "date", "time"].includes(props.mode), | ||
"Invalid mode. Valid modes: 'datetime', 'date', 'time'" | ||
props => | ||
props && props.mode && !['datetime', 'date', 'time'].includes(props.mode), | ||
"Invalid mode. Valid modes: 'datetime', 'date', 'time'" | ||
) | ||
const checks = [ | ||
widthCheck, | ||
heightCheck, | ||
modeCheck, | ||
] | ||
const checks = [widthCheck, heightCheck, modeCheck] |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
87521
1
57
251
1