访问导航方法React-Big-Calendar和Typescript

时间:2018-06-08 16:08:33

标签: javascript reactjs typescript react-big-calendar

我很难使用带有react-big-calendar和typescript的自定义工具栏。我正在尝试访问“下一个,上一个”,“月,日,周”视图的原始方法。 我已经广泛阅读了...... https://github.com/intljusticemission/react-big-calendar/issues/623 https://github.com/intljusticemission/react-big-calendar/issues/818 http://intljusticemission.github.io/react-big-calendar/examples/index.html#prop-components

自定义UI渲染正常,没有错误。现在我需要访问原始方法,以便我可以操作日历。

主要问题是我的按钮正在开火,但实际上并没有导航任何东西。

我认为有些问题是......

- 我实际上并没有像我想的那样使用navigateMethod

- BigCalendar中的默认日期和日期实际上并没有改变,因为我每次点击都会在同一天覆盖它?

- 我需要从他们的文档中实现这个例子吗?

Custom views can be any React component, that implements the following interface:

interface View {
  static title(date: Date, { formats: DateFormat[], culture: string?, ...props }): string
  static navigate(date: Date, action: 'PREV' | 'NEXT' | 'DATE'): Date
}

有没有人有一个我可以看的例子???

这是我的完整源代码。

import React from 'react'
import { MainContent } from '../../../common/templates/partials'
import BigCalendar from 'react-big-calendar'
import ToolBar from 'react-big-calendar'
import Icon from 'app/common/components/Icon'
import moment from 'moment'
const styles = require('./CalendarUI.scss')

BigCalendar.momentLocalizer(moment) // or globalizeLocalizer

const events = [
  {
    start: new Date(),
    end: new Date(),
    title: 'Some title',
  },
]

class CustomToolbar extends ToolBar {

  render() {
    // tslint:disable-next-line:no-console
    console.log(this.props, this)
    /* tslint:disable-next-line */
    const {label, onNavigate} = this.props as any
    return (
      <div className="rbc-toolbar">
        <div>
          {/* tslint:disable-next-line  */}
          <button onClick={() => this.props.onNavigate ? onNavigate(null as any, 'PREV') : undefined}>
            <Icon icon="B" />
          </button>
          <label className="label-date">{label}</label>
          {/* tslint:disable-next-line  */}
          <button onClick={() => this.props.onNavigate ? onNavigate(null, 'NEXT') : undefined}>
            <Icon icon="A" />
          </button>
        </div>

        <div>
        <span className="rbc-btn-group">
          <button>Month</button>
          <button>Day</button>
          <button>Week</button>
        </span>

        <button className="btn btn-back">
          <Icon icon="R" />
        </button>
        <button className="btn btn-back">
          <Icon icon="meet_now" />
        </button>
        </div>
      </div>
    )
  }
}

const logger = (data: string) =>
  // tslint:disable-next-line:no-console
  console.log(data)
const CalendarUI = () => (
  <MainContent>
    <div className={styles.calendarContainer}>
      <BigCalendar
        defaultDate={moment().toDate()}
        defaultView="month"
        events={events}
        components={{ toolbar: CustomToolbar }}
        startAccessor="startDate"
        endAccessor="endDate"
        onView={logger}
        date={moment().toDate()}
      />
    </div>
  </MainContent>
)

export default CalendarUI

2 个答案:

答案 0 :(得分:0)

对TS部件不满意,但是我最近自己实现了自定义工具栏。我复制了原始的工具栏,然后对其进行了调整以满足我的要求。我的自定义内容并不那么重要,但这应该向您展示他们最初是如何实现navigate位的。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import ToolbarDateHeader from './ToolbarDateHeader.component';
import { Icon, Button, ButtonGroup, ButtonToolbar } from '../app';

const navigate = {
  PREVIOUS: 'PREV',
  NEXT: 'NEXT',
  TODAY: 'TODAY',
  DATE: 'DATE'
};

const propTypes = {
  view: PropTypes.string.isRequired,
  views: PropTypes.arrayOf(PropTypes.string).isRequired,
  label: PropTypes.node.isRequired,
  localizer: PropTypes.object,
  onNavigate: PropTypes.func.isRequired,
  onView: PropTypes.func.isRequired
};

export default class Toolbar extends Component {
  static propTypes = propTypes;
  render() {
    let {
      localizer: { messages },
      label,
      date
    } = this.props;

    return (
      <ButtonToolbar>
        <ButtonGroup>
          <Button onClick={this.navigate.bind(null, navigate.TODAY)}>
            {messages.today}
          </Button>
          <Button onClick={this.navigate.bind(null, navigate.PREVIOUS)}>
            <Icon glyph="caret-left" />
          </Button>
          <Button onClick={this.navigate.bind(null, navigate.NEXT)}>
            <Icon glyph="caret-right" />
          </Button>
        </ButtonGroup>

        <ToolbarDateHeader date={date} onChange={this.toThisDay}>
          {label}
        </ToolbarDateHeader>

        <ButtonGroup className="pull-right">
          {this.viewNamesGroup(messages)}
        </ButtonGroup>
      </ButtonToolbar>
    );
  }

  toThisDay = date => {
    this.props.onView('day');
    // give it just a tick to 'set' the view, prior to navigating to the proper date
    setTimeout(() => {
      this.props.onNavigate(navigate.DATE, date);
    }, 100);
  };

  navigate = action => {
    this.props.onNavigate(action);
  };

  view = view => {
    this.props.onView(view);
  };

  viewNamesGroup(messages) {
    let viewNames = this.props.views;
    const view = this.props.view;

    if (viewNames.length > 1) {
      return viewNames.map(name => (
        <Button
          key={name}
          className={cn({
            active: view === name,
            'btn-primary': view === name
          })}
          onClick={this.view.bind(null, name)}
        >
          {messages[name]}
        </Button>
      ));
    }
  }
}

答案 1 :(得分:0)

您好,我想出了创建CustomToolbar组件的方法。参见代码

*** RUN TIME ERROR(S) ***
Traceback (most recent call last):
  File "source.py", line 8, in <module>
    result = percent_of_goal(0, 1000)
  File "source.py", line 4, in percent_of_goal
    perc = goal/donation
ZeroDivisionError: division by zero

这是使用此组件的BigCalendar的代码

import * as React from 'react';
import {css} from 'office-ui-fabric-react'

export interface ICustomTooolbarProps
{
  view: string,
  views: string[],
  label: any,
  localizer: any,
  onNavigate: (action: any) => void,
  onView: (view: any) => void,
  onViewChange: (view: any) => void,
  messages: any,
}

export let navigateContants = {
  PREVIOUS: 'PREV',
  NEXT: 'NEXT',
  TODAY: 'TODAY',
  DATE: 'DATE',
}

export let views = {
  MONTH: 'month',
  WEEK: 'week',
  WORK_WEEK: 'work_week',
  DAY: 'day',
  AGENDA: 'agenda',
}


const CustomToolbar: React.SFC<ICustomTooolbarProps> = (props) => {
    
  debugger;
 /*   const {
      localizer: { props.messages },
      label,
    } = props*/

    function navigate (action) {
      props.onNavigate(action)
    }
  
    function viewItem (view){
      props.onViewChange(view)
    }
  
    function viewNamesGroup(messages) {
      let viewNames = props.views
      const view = props.view
  
      if (viewNames.length > 1) {
        return viewNames.map(name => (
          <button
            type="button"
            key={name}
            className={css({ 'rbc-active': view === name })}
            onClick={viewItem.bind(null, name)}
          >
            {messages[name]}
          </button>
        ))
      }
    }
  

                        
    return (
      <div className="rbc-toolbar">
        <span className="rbc-btn-group">
          <button
            type="button"
            onClick={navigate.bind(null, navigateContants.TODAY)}
          >
            Current month
          </button>
          <button
            type="button"
            onClick={navigate.bind(null, navigateContants.PREVIOUS)}
          >
            Previous month
          </button>
          <button
            type="button"
            onClick={navigate.bind(null, navigateContants.NEXT)}
          >
            Next month
          </button>
        </span>

        <span className="rbc-toolbar-label">{props.label}</span>

        <span className="rbc-btn-group">{viewNamesGroup(props.messages)}</span>
      </div>
    )

}

export  default CustomToolbar;

希望它会有所帮助;)