Skip to content

Repository files navigation

react-native-calendar-picker

npm versionnpmBuild Status

This is a Calendar Picker Component for React Native

alt tag

To use the calendar you just need to:

npm install --save react-native-calendar-picker

Note: react-native-calendar-picker v5 is a complete re-write of the calendar. This calendar is now written using ES6 syntax. I kept most of the same functionalities and added support for date ranges.

If you need the old code I saved it on a branch v4

Prerequisites

CalendarPicker requires Moment JS. Date props may be anything parseable by Moment: Javascript Date, Moment date, or ISO8601 datetime string.

Example

importReact,{Component}from'react';import{StyleSheet,Text,View}from'react-native';importCalendarPickerfrom'react-native-calendar-picker';exportdefaultclassAppextendsComponent{constructor(props){super(props);this.state={selectedStartDate: null,};this.onDateChange=this.onDateChange.bind(this);}onDateChange(date){this.setState({selectedStartDate: date,});}render(){const{ selectedStartDate }=this.state;conststartDate=selectedStartDate ? selectedStartDate.toString() : '';return(<Viewstyle={styles.container}><CalendarPickeronDateChange={this.onDateChange}/><View><Text>SELECTED DATE:{startDate}</Text></View></View>);}}conststyles=StyleSheet.create({container: {flex: 1,backgroundColor: '#FFFFFF',marginTop: 100,},});

CalendarPicker Props

PropTypeDescription
weekdaysArrayOptional. List of week days. Eg. ['Mon', 'Tue', ...] Must be 7 days
monthsArrayOptional. List of months names. Eg. ['Jan', 'Feb', ...] Must be 12 months
startFromMondayBooleanOptional. Default first day of week will be Sunday. You can set start of week from Monday by setting this to true. Default is false
allowRangeSelectionBooleanOptional. Allow to select date ranges. Default is false
previousTitleStringOptional. Title of button for previous month. Default is Previous
nextTitleStringOptional. Title of button for next month. Default is Next
selectedDayColorStringOptional. Color for selected day
selectedDayStyleViewStyleOptional. Style for selected day. May override selectedDayColor.
selectedDayTextColorStringOptional. Text color for selected day
selectedRangeStartStyleViewStyleOptional. Style for range selected start day.
selectedRangeEndStyleViewStyleOptional. Style for range selected end day.
selectedRangeStyleViewStyleOptional. Style for all days in range selection.
todayBackgroundColorStringOptional. Background color for today. Default is #cccccc
todayTextStyleTextStyleOptional. Text styling for today.
textStyleObjectOptional. Style overall text. Change fontFamily, color, etc.
customDatesStylesArrayOptional. Style individual date(s). Array of objects {date: Moment-parseable date, containerStyle: ViewStyle, style: ViewStyle, textStyle: TextStyle}
scaleFactorNumberOptional. Default (375) scales to window width
minDateDateOptional. Specifies minimum date to be selected
maxDateDateOptional. Specifies maximum date to be selected
initialDateDateOptional. Date that calendar opens to. Defaults to today.
widthNumberOptional. Width of CalendarPicker's container. Defaults to Dimensions width.
heightNumberOptional. Height of CalendarPicker's container. Defaults to Dimensions height.
swipeConfigObjectOptional. Config passed to Swiper.
enableSwipeBooleanOptional. Whether to enable swiping. Default is true
onDateChangeFunctionOptional. Callback when a date is selected. Returns Moment date as first parameter.
onMonthChangeFunctionOptional. Callback when Previous / Next month is pressed. Returns Moment date as first parameter.

Styles

Some styles will overwrite some won't. For instance:

  • If you provide textStyle with fontFamily and color, out of ranges dates will not apply your color, just fontFamily.

Order of precedence:

  • defaultColor => textStyle => selectedDayColor
  • defaultTodayBackgroundColor => todayBackgroundColor
  • defaultBackgroundColor => selectedDayColor
  • defaultTextStyles => textStyle => selectedDayTextColor

More Examples

Start from Monday, allowRangeSelection, Min and Max Dates and Styles Changes Example

alt tag

importReact,{Component}from'react';import{StyleSheet,Text,View}from'react-native';importCalendarPickerfrom'react-native-calendar-picker';exportdefaultclassAppextendsComponent{constructor(props){super(props);this.state={selectedStartDate: null,selectedEndDate: null,};this.onDateChange=this.onDateChange.bind(this);}onDateChange(date,type){if(type==='END_DATE'){this.setState({selectedEndDate: date,});}else{this.setState({selectedStartDate: date,selectedEndDate: null,});}}render(){const{ selectedStartDate, selectedEndDate }=this.state;constminDate=newDate();// TodayconstmaxDate=newDate(2017,6,3);conststartDate=selectedStartDate ? selectedStartDate.toString() : '';constendDate=selectedEndDate ? selectedEndDate.toString() : '';return(<Viewstyle={styles.container}><CalendarPickerstartFromMonday={true}allowRangeSelection={true}minDate={minDate}maxDate={maxDate}todayBackgroundColor="#f2e6ff"selectedDayColor="#7300e6"selectedDayTextColor="#FFFFFF"onDateChange={this.onDateChange}/><View><Text>SELECTED START DATE:{startDate}</Text><Text>SELECTED END DATE:{endDate}</Text></View></View>);}}conststyles=StyleSheet.create({container: {flex: 1,backgroundColor: '#FFFFFF',marginTop: 100,},});

Complex Example, Changing Fonts and Colors, Language and etc...

alt tag

importReact,{Component}from'react';import{StyleSheet,Text,View}from'react-native';importCalendarPickerfrom'react-native-calendar-picker';exportdefaultclassAppextendsComponent{constructor(props){super(props);this.state={selectedStartDate: null,selectedEndDate: null,};this.onDateChange=this.onDateChange.bind(this);}onDateChange(date,type){if(type==='END_DATE'){this.setState({selectedEndDate: date,});}else{this.setState({selectedStartDate: date,selectedEndDate: null,});}}render(){const{ selectedStartDate, selectedEndDate }=this.state;constminDate=newDate();// TodayconstmaxDate=newDate(2017,6,3);conststartDate=selectedStartDate ? selectedStartDate.toString() : '';constendDate=selectedEndDate ? selectedEndDate.toString() : '';return(<Viewstyle={styles.container}><CalendarPickerstartFromMonday={true}allowRangeSelection={true}minDate={minDate}maxDate={maxDate}weekdays={['Seg','Ter','Qua','Qui','Sex','Sab','Dom']}months={['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro']}previousTitle="Anterior"nextTitle="Próximo"todayBackgroundColor="#e6ffe6"selectedDayColor="#66ff33"selectedDayTextColor="#000000"scaleFactor={375}textStyle={{fontFamily: 'Cochin',color: '#000000',}}onDateChange={this.onDateChange}/><View><Text>SELECTED START DATE:{startDate}</Text><Text>SELECTED END DATE:{endDate}</Text></View></View>);}}conststyles=StyleSheet.create({container: {flex: 1,backgroundColor: '#FFFFFF',marginTop: 100,},});

Custom styling individual dates

alt tag

lettoday=moment();letday=today.clone();letcustomDatesStyles=[];while(day.add(1,'day').isSame(today,'month')){customDatesStyles.push({date: day.clone(),// Random colorsstyle: {backgroundColor: '#'+('#00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6)},});}render(){return(<CalendarPickertodayTextStyle={{fontWeight: 'bold'}}todayBackgroundColor={'transparent'}customDatesStyles={this.state.selectedTab==='tours' ? this.state.customTourDatesStyles : this.state.customRentalDatesStyles}minDate={today}/>);}

Suggestions?

Open Issues. Submit PRs.

Special Thanks

I would like to call out some contributors who have been helping with this project

Development

git clone git@github.com:stephy/CalendarPicker.git CalendarPicker
npm install

In Package.json modify

"main": "./CalendarPicker",

to

"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",

Running on device

npm start

Running on iOS Simulator

npm run ios

About

CalendarPicker Component for React Native

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages