1.0.0 • Published 4 years ago

react-native-reanimated-datepicker v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

react-native-reanimated-datepicker

React Native DatePicker component for both Android and iOS, using new react native community date & time pickers

Install

npm install react-native-reanimated-datepicker --save

Or

yarn add react-native-reanimated-datepicker

Once the above package is installed, please install these other packages

yarn add react-native-gesture-handler
yarn add react-native-reanimated
yarn add react-native-interactable-reanimated

Or

npm install react-native-gesture-handler --save
npm install react-native-reanimated --save
npm install react-native-interactable-reanimated --save

If you have react-native-gesture-handler and react-native-reanimated already installed just install react-native-interactable-reanimated

yarn add react-native-interactable-reanimated

Or

npm install react-native-interactable-reanimated --save

Usage

import React, { Component } from "react";
import DateTimePicker from "../src/index";
import { View, TouchableOpacity, Text, Platform } from "react-native";
import moment from "moment";

class index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      date: new Date(),
      showDatepicker: false,
    };
  }

  onChange = (event, selectedDate) => {
    if (Platform.OS === "ios") {
      this.setState({
        date: moment(selectedDate, "YYYY-MM-DDTHH:mm:ss.sssZ").toDate(),
      });
    } else if (Platform.OS === "android") {
      //Here its a slight different to manage, because this function is called when ok or cancel button is pressed
      //Check if the selectedDate is undefined.
      if (selectedDate === undefined) {
        this.setState({
          showDatepicker: false,
        });
      } else {
        this.setState({
          date: moment(selectedDate, "YYYY-MM-DDTHH:mm:ss.sssZ").toDate(),
          showDatepicker: false,
        });
      }
    }
  };

  //NOTE: For iOS Only
  handleCancelButtonPressed = () => {
    this.setState({
      showDatepicker: false,
    });
  };

  //NOTE: For iOS Only
  handleConfirmButtonPressed = () => {
    this.setState({
      showDatepicker: false,
    });
  };

  render() {
    const { date, showDatepicker } = this.state;
    return (
      <View
        style={{
          flex: 1,
          justifyContent: "center",
          alignContent: "center",
          alignItems: "center",
        }}>
        <TouchableOpacity
          onPress={() => {
            this.setState({
              showDatepicker: true,
            });
          }}>
          <>
            <Text style={{ textAlign: "center" }}>Open date picker</Text>
            <Text style={{ textAlign: "center" }}>
              Selected Date: {date.toDateString()}
            </Text>
          </>
        </TouchableOpacity>
        {showDatepicker && (
          <DateTimePicker
            mode="date"
            display="default"
            handleDateChanged={this.onChange}
            date={this.state.date}
            cancelBtnText="Cancel"
            handleCancelBtnPress={this.handleCancelButtonPressed}
            confirmBtnText="Done"
            handleConfirmButtonPress={this.handleConfirmButtonPressed}
            iosBottomSheetInitialPosition="40%"
            iosBottomSheetSnapPoints={["40%"]}
            iosBottomSheetBackdrop={true}
            iosBottomSheetBackDropDismissByPress={false}
          />
        )}
      </View>
    );
  }
}

export default index;

You can check example folder for more details.

Properties

PropDefaultTypeRequiredDescription
date-string | dateYesSpecify the display date of DatePicker. string type value must match the specified YYYY-MM-DDTHH:mm:ss.SSSZ ISO 8601 format
ref (iOS only)-React RefYesCreate a React.createRef() and pass it in the ref of the datepicker for iOS bottom Sheet
mode'date'enumNoThe enum of date, datetime and time
display'default'enumNoThe enum of default, calendar, clock and spinner (For Android) The enum of default, spinner, compact (iOS 14 only) and inline (iOS 14 only) (For iOS)
confirmBtnText'Done'stringNoSpecify the text of confirm btn in ios.
cancelBtnText'Cancel'stringNoSpecify the text of cancel btn in ios.
minDate-dateNoShould be a date object
maxDate-dateNoShould be a date object
minuteInterval0numberNoSpecify the minutes interval (iOS only).
timeZoneOffsetInMinutes0numberNoAllows changing of the timeZone of the date picker. By default it uses the device's time zone. (iOS Only)
is24Hour-booleanNoSet the TimePicker is24Hour flag. The default value depend on format. Only work in Android
iosBottomSheetContainerStyles (iOS only)-objectNoiOS Bottom Sheet container styles
iosBottomSheetHeaderStyles (iOS only)-objectNoiOS Bottom Sheet main header styles
iosBottomSheetContentStyles (iOS only)-objectNoiOS Bottom Sheet main content body styles
iosBottomSheetInitialPosition (iOS only)40%stringNoiOS Bottom Sheet initial snap point, the bottom sheet will snap to this point after rendering
iosBottomSheetSnapPoints (iOS only)"40%"Array <string | number>NoiOS Bottom Sheet snap points, the bottom sheet will snap onto these snap points
iosBottomSheetBackdrop (iOS only)trueboolNoiOS Bottom Sheet backdrop, which will display bottom sheet like an overlay on the screen
iosBottomSheetBackDropDismissByPress (iOS only)falseboolNoiOS Bottom Sheet will dismiss if touched out the bottom sheet
iosBottomSheetCustomHeader (iOS only)-React NodeNoYou can render your own custom bottom sheet if you do not want to use the one provided in the bundle

Events

EventRequiredDescription
handleDateChangedYesThis event is fired when the date is changed, In this callback you can have an event and date. Now you can use these two the way you want. onChange = (event, selectedDate), function onChange(event, selectedDate)
handleCancelBtnPressYesYou can perform any action on the cancel button is pressed, this callback will be fired by the bottom sheet. (If default header is used pass this prop)
handleConfirmButtonPressYesYou can perform any action on the confirm/done button is pressed, this callback will be fired by the bottom sheet. (If default header is used pass this prop)

Bottom Sheet Methods (iOS Only)

These methods will only be provided if Ref is set for the datepicker

MethodParamsDescription
snapTonumberBottom Sheet provides this method to snap bottom sheet to any point on the screen
dismissBottomSheet-Bottom Sheet provides this method to dismiss the bottom sheet