Giter Site home page Giter Site logo

react-native-persian-date-picker's Introduction

React-native-persian-date-picker

Jalali (Persian) date picker component for react native

this package uses reanimated-bottom-sheet and @react-native-picker/picker.

|

Installation

  1. Open a Terminal in the project root and run:
npm i react-native-persian-date-picker2

Now we need to install native bottom sheet behavior and @react-native-picker/picker.

  1. So run the following:
npm install reanimated-bottom-sheet react-native-reanimated react-native-gesture-handler @react-native-picker/picker
  1. the reanimated-bottom-sheet uses react-native-reanimated and react-native-gesture-handler so for more deatils about the installation of these two package visit react-native-reanimated install and visit install react-native-gesture-handler(you probably have installed this package already, it's a famous package)

Usage

import PersianDatePicker from 'react-native-persian-date-picker2';
...
...

<PersianDatePicker
        visible={datePickerVisibility ? true : false }
        onConfirm={(value)=>{console.log(value)}}
        defaultValue={[1370,7,5]}
        startYear={1330}
        endYear={1400}
        containerStyle={{}}
        pickercontainerStyle={{}}
        pickerWrapperStyle={{}}
        pickerItemStyle={{}}
        submitTextStyle={{}}
      />
 

example

import React, { useEffect, useState } from 'react';
import { View,Image,
   StyleSheet,TouchableOpacity} from 'react-native';
import PersianDatePicker from 'react-native-persian-date-picker2';

export default function App() {
  
  const [datePickerVisibility, setDatePickerVisibility] = React.useState(false);
  const [birthDate, setBirthDate] = React.useState({
    string : "",
    year:null,
    month:null,
    day:null
  });
  
  const onBirthDatePickerConfirm = (objVal)=>{
          let dataString = objVal.value[0] + '/' + objVal.value[1] + '/' + objVal.value[2];
          setBirthDate({
            string : dataString,
            year: objVal.value[0],
            month: objVal.value[1],
            day: objVal.value[2]
          })
          setDatePickerVisibility(false);
          return true;
        }

  return (
    <>
    
    <TouchableOpacity onPress={()=>{setDatePickerVisibility(true);}} >
        {(birthDate.year) ? <View style={[styles.PersianDatePicker]}>
          <Text style={styles.PersianDatePickerText}>
            {(birthDate.year+"").toFarsiDigits()}
          </Text>
          <Text style={styles.PersianDatePickerText}>
            {(birthDate.month+"").toFarsiDigits()}
          </Text>
          <Text style={styles.PersianDatePickerText}>
            {(birthDate.day+"").toFarsiDigits()}
          </Text>
        </View>
          :
          <View style={[styles.PersianDatePicker]}><Text style={styles.PersianDatePickerText} >تاریخ تولد</Text></View>
        }
      </TouchableOpacity>
    
    <PersianDatePicker
        visible={datePickerVisibility}
        onConfirm={onBirthDatePickerConfirm}
        startYear={1330}
        endYear={1400}
        containerStyle={{}}
        pickercontainerStyle={{}}
        pickerWrapperStyle={{}}
        pickerItemStyle={{}}
        submitTextStyle={{}}
        defaultValue={[1370,7,5]}
      />
      
    </>
  );
}

const toFarsiDigits = function (str) {
  return str.replace(/[0-9]/g, function (w) {
      var persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
      return persian[w];
});

const styles = StyleSheet.create({
  PersianDatePicker:{
    flexDirection:'row',
    justifyContent:'space-between',
    padding:7,
    paddingRight:1,
    paddingLeft:1,
    borderWidth:1,
    borderColor:'rgba(255,255,255,0.8)',
    borderRadius:6,
    marginBottom:30,
    backgroundColor:'transparent',
    marginTop:37,
  },
  PersianDatePickerText:{
    flex:1,
    padding:0,
    fontSize:14,
    textAlign:'center',
    color:'rgba(255,255,255,1)',
  }
});
    

Props

name required default description
visible yes true visibility of bottom sheet
onConfirm yes (val)=>{} the function that call after confirm , the val is object that contain : {value : [year,month,day]} , **> if the function return false the bottom sheet will not close
defaultValue no null should be array like [1370,7,5]
startYear no 1330
endYear no 1400
containerStyle no null style object of sheet container
pickercontainerStyle no null style object of each three picker container : container > wrapper > picker
pickerWrapperStyle no null style object of each three picker wrapper
pickerItemStyle no null style object of each three picker item
pickerProps no null see picker docs
submitLabel no تایید bottom confirm label
submitStyle no null style object of submit btn
submitTextStyle no null style object of submit text btn
borderRadius no 20 border Radius of sheet
bottomSheetProps no null see sheet docs
caption no null simple header caption component ex : <View><Text>this is caption</Text></View>

Todo

It's not finished and some work has to be done yet.

  1. Time picker
  2. Default jalali date value with moment
  3. More config
  4. Cleanup code (e.g. measuring of components)

Contributing

Please share your comment and tests with me by @hamid and twitter. 😉😌

react-native-persian-date-picker's People

Contributors

hamid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.