React Native日历日程组件

这次介绍的这个React Native日历日程组件名叫react-native-calendars,是纯JS开发,可以适配IOS和安卓双平台。使用方便,功能强大,可以通过配置自定义样式和主题,更重要的是它支持日程显示。下面我们来看看这个组件的使用方法。

http://mp.weixin.qq.com/s/zS4VbGJh4WVxkP9xMIN3iw

安装

   
   
   
   
  1. npm install --save react-native-calendars

因为是纯JS的,所以不需要link,执行完就可以了。

使用

react-native-calendars主要包含三种子组件,分别是 Calendar(日历), CalendarList(日历列表), Agenda(日程),可以根据实际需要选择使用。下面我们分别介绍这三种组件的使用示例。

Calendar(日历)

React Native日历日程组件_第1张图片

示例代码

   
   
   
   
  1. <Calendar

  2.  // Initially visible month. Default = Date()

  3.  current={'2012-03-01'}

  4.  // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined

  5.  minDate={'2012-05-10'}

  6.  // Maximum date that can be selected, dates after maxDate will be grayed out. Default = undefined

  7.  maxDate={'2012-05-30'}

  8.  // Handler which gets executed on day press. Default = undefined

  9.  onDayPress={(day) => {console.log('selected day', day)}}

  10.  // Month format in calendar title. Formatting values: http://arshaw.com/xdate/#Formatting

  11.  monthFormat={'yyyy MM'}

  12.  // Handler which gets executed when visible month changes in calendar. Default = undefined

  13.  onMonthChange={(month) => {console.log('month changed', month)}}

  14.  // Hide month navigation arrows. Default = false

  15.  hideArrows={true}

  16.  // Replace default arrows with custom ones (direction can be 'left' or 'right')

  17.  renderArrow={(direction) => (<Arrow />)}

  18.  // Do not show days of other months in month page. Default = false

  19.  hideExtraDays={true}

  20.  // If hideArrows=false and hideExtraDays=false do not switch month when tapping on greyed out

  21.  // day from another month that is visible in calendar page. Default = false

  22.  disableMonthChange={true}

  23.  // If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.

  24.  firstDay={1}

  25.  // Hide day names. Default = false

  26.  hideDayNames={true}

  27. />

CalendarList(日历列表)

React Native日历日程组件_第2张图片

示例代码

   
   
   
   
  1. <CalendarList

  2.  // Callback which gets executed when visible months change in scroll view. Default = undefined

  3.  onVisibleMonthsChange={(months) => {console.log('now these months are visible', months);}}

  4.  // Max amount of months allowed to scroll to the past. Default = 50

  5.  pastScrollRange={50}

  6.  // Max amount of months allowed to scroll to the future. Default = 50

  7.  futureScrollRange={50}

  8.  // Enable or disable scrolling of calendar list

  9.  scrollEnabled={true}

  10.  ...calendarParams

  11. />

Agenda(日程)

React Native日历日程组件_第3张图片

示例代码

   
   
   
   
  1. <Agenda

  2.  // the list of items that have to be displayed in agenda. If you want to render item as empty date

  3.  // the value of date key kas to be an empty array []. If there exists no value for date key it is

  4.  // considered that the date in question is not yet loaded

  5.  items={

  6.    {'2012-05-22': [{text: 'item 1 - any js object'}],

  7.     '2012-05-23': [{text: 'item 2 - any js object'}],

  8.     '2012-05-24': [],

  9.     '2012-05-25': [{text: 'item 3 - any js object'},{text: 'any js object'}],

  10.    }}

  11.  // callback that gets called when items for a certain month should be loaded (month became visible)

  12.  loadItemsForMonth={(month) => {console.log('trigger items loading')}}

  13.  // callback that gets called on day press

  14.  onDayPress={(day)=>{console.log('day pressed')}}

  15.  // callback that gets called when day changes while scrolling agenda list

  16.  onDayChange={(day)=>{console.log('day changed')}}

  17.  // initially selected day

  18.  selected={'2012-05-16'}

  19.  // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined

  20.  minDate={'2012-05-10'}

  21.  // Maximum date that can be selected, dates after maxDate will be grayed out. Default = undefined

  22.  maxDate={'2012-05-30'}

  23.  // Max amount of months allowed to scroll to the past. Default = 50

  24.  pastScrollRange={50}

  25.  // Max amount of months allowed to scroll to the future. Default = 50

  26.  futureScrollRange={50}

  27.  // specify how each item should be rendered in agenda

  28.  renderItem={(item, firstItemInDay) => {return (<View />);}}

  29.  // specify how each date should be rendered. day can be undefined if the item is not first in that day.

  30.  renderDay={(day, item) => {return (<View />);}}

  31.  // specify how empty date content with no items should be rendered

  32.  renderEmptyDate={() => {return (<View />);}}

  33.  // specify how agenda knob should look like

  34.  renderKnob={() => {return (<View />);}}

  35.  // specify your item comparison function for increased performance

  36.  rowHasChanged={(r1, r2) => {return r1.text !== r2.text}}

  37.  // Hide knob button. Default = false

  38.  hideKnob={true}

  39.  // By default, agenda dates are marked if they have at least one item, but you can override this if needed

  40.  markedDates={{

  41.    '2012-05-16': {selected: true, marked: true},

  42.    '2012-05-17': {marked: true},

  43.    '2012-05-18': {disabled: true}

  44.  }}

  45.  // agenda theme

  46.  theme={{

  47.    ...calendarTheme,

  48.    agendaDayTextColor: 'yellow',

  49.    agendaDayNumColor: 'green',

  50.    agendaTodayColor: 'red',

  51.    agendaKnobColor: 'blue'

  52.  }}

  53.  // agenda container style

  54.  style={{}}

  55. />

说明

react-native-calendars组件的GitHub 地址:https://github.com/wix/react-native-calendars,更多的配置和使用方法请点击查看原文查看GitHub上的文档以及示例代码。

你可能感兴趣的:(】)