React Native Date Picker with Custom Modal

March 06, 2021

React Native Date Picker with Custom Modal

In this topic we are open React Native Date Picker with Custom Modal onPress event

 open  React Native Date Picker with Custom Modal onPress event

we will open Date picker onPress on custom modal

Install

Install with npm using:

npm install @react-native-community/datetimepicker --save
cd ios
pod update

Android should auto link if you are using RN v60+

DatePicker.js

import React ,{useState} from 'react'
import { Platform,Text,View,Dimensions } from 'react-native';
import styled from 'styled-components/native';
import DateTimePicker from '@react-native-community/datetimepicker';

const DatePicker=({onClose,onChange,date,mindate})=>{
  
    const [Datestate, setDatestate] = useState(new Date(date));
  
      return (
        <ContainerDatepicker onPress={()=>onClose()}>
             {Platform.OS === 'ios' && (
             <HeaderDatePicker>
              <DateButton onPress={()=>onClose(Datestate)}>
                    <Text 
                    style={{fontWeight:'bold',color:'blue',fontSize:20}}>
                     Done
                    </Text>
              </DateButton>
              </HeaderDatePicker> 
             )}
          <DateTimePicker 
            value={Datestate}
            mode="date"
            minimumDate={new Date(1950, 0, 1)}
            maximumDate={new Date(mindate)} 
            display={Platform.OS === 'ios'?"spinner":"default"}
            onChange={(e, d) => {
              if (Platform.OS === 'ios') {
                setDatestate(d)
              } else { 
                onClose(d);
              }
           }}
           style={{ backgroundColor: 'white' }}
         />
       </ContainerDatepicker>
     );
    }
  export default DatePicker;
  
  const ContainerDatepicker = styled.TouchableOpacity`
  background-color: ${Platform.OS === 'ios' ? '#00000066' : 'transparent'};
  position: absolute;
  justify-content: flex-end; 
  width: 100%;
  height: 100%;
  z-index:1;
`;
const HeaderDatePicker = styled.View`
  width: 100%;
  padding: 16px;
  justify-content: flex-end;
  align-items: flex-end;
  background-color: white;
  border-bottom-width: 1px;
  border-color: grey;
`;
  

App.js

import React,{useState,useRef} from 'react'
import DatePicker from '../../componet/DatePicker';
import moment from 'moment';
const App = () => {
    const [showDatePicker, setshowDatePicker] = useState(false)

    const [DateValue, setDate] = useState(profile.dateofbirth||new Date()); 

  

        const showDatepicker=()=>{
             setshowDatePicker(true)
        }

     
    


    return (
        <Container>
          {showDatePicker && (
            <DatePicker
              date={DateValue}  
              mindate={moment().subtract(12, 'months')}
              onClose={date => {
                console.info(date)
                  if (date) {
                    setshowDatePicker(false)
                    setDate(date)
                   }else{
                    setshowDatePicker(false)
                   }
                 }}
               onChange={d => {
                console.info(d)
                setDate(moment(d))
              }}
            />
             )}
             <Button title="Open Date Picker" onPress={showDatepicker}/>
        </Container>
    )
}

export default App;

const Container = styled.View`
       flex:1;
       background-color:#fff;
`;
const Button = styled.Button``;

Written by Manoj Bhardwaj who lives and works in Dharamshala Himachal Pradesh (India). My stackoverflow