React-native DatePicker The implementation of the date selection component

Source: Internet
Author: User



The implementation of this tutorial is as follows:






In order to achieve its fade-in/fade-out coverage, there is a Cancel button, here with a three-party component, you can first install:



Three-party components address: Https://github.com/eyaleizenberg/react-native-custom-action-sheet (can see, you can also go directly to my steps)






1. Run the project directory in Terminal: NPM install React-native-custom-action-sheet--save



2. Then run: NPM start



3. The specific implementation code is as follows:


import React, {Component} from ‘react’;
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  DatePickerIOS
} from ‘react-native’;

// This is a three-party component github address: https://github.com/eyaleizenberg/react-native-custom-action-sheet
var CustomActionSheet = require (‘react-native-custom-action-sheet’);

class Demo extends Component {

  state = {
    datePickerModalVisible: false, // Selector hidden mark
    chooseDate: new Date () // Date of choice
  };

  _showDatePicker () {// Toggle display and hidden markers
    this.setState ((datePickerModalVisible:! this.state.datePickerModalVisible));
  };

  _onDateChange (date) {// Change the date state
    alert (date); // pop-up alert box: show your selected date
    this.setState ({
      chooseDate: date
    });
  };

  render () {

    let datePickerModal = (// date picker component (assigns a selector or empty based on the tag)
      this.state.datePickerModalVisible?
      <CustomActionSheet
        modalVisible = {this.state.datePickerModalVisible}
        onCancel = (() => this._showDatePicker ())> // Click the cancel button to trigger the event
          <View style = {styles.datePickerContainer}>
            <DatePickerIOS
              mode = {"datetime"} // Selector mode: ‘date’ (date), ‘time’ (time), ‘datetime’ (date and time)
              minimumDate = (new Date ()) // Minimum time (the current time is set here)
              minuteInterval = {30} // Minimum time interval (30 minutes is set here)
              date = {this.state.chooseDate} // default time
              onDateChange = {this._onDateChange.bind (this)} // Call this function when the date is modified
            />
            </ View>
       </ CustomActionSheet>: null
    );

    return (
      <View style = {styles.container}>
        <TouchableHighlight
          style = {{backgroundColor: ‘cyan’, padding: 5}}
          onPress = (() => this._showDatePicker ()) // button: click trigger method
          underlayColor = ‘gray’
          >
          <Text> show DatePick </ Text>
        </ TouchableHighlight>
        {datePickerModal} // Date selection component
      </ View>
    );
  }
}

const styles = StyleSheet.create ({
  container: {
    flex: 1,
    justifyContent: ‘center’,
    alignItems: ‘center’,
    backgroundColor: ‘# F5FCFF’,
  },
  datePickerContainer: {
    flex: 1,
    borderRadius: 5,
    justifyContent: ‘center’,
    alignItems: ‘center’,
    backgroundColor: ‘white’,
    marginBottom: 10,
  },
});

AppRegistry.registerComponent (‘Demo’, () => Demo); 


Write well, run in Terminal: React-native Run-ios can see the effect














React-native DatePicker The implementation of the date selection component


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.