【问题标题】:access to navigate methods React-Big-Calendar and Typescript访问导航方法 React-Big-Calendar 和 Typescript
【发布时间】:2018-11-18 17:55:20
【问题描述】:

我很难使用带有 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

【问题讨论】:

    标签: javascript reactjs typescript react-big-calendar


    【解决方案1】:

    我发现它是创建一个 CustomToolbar 组件。查看以下代码:

    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 const navigateContants = {
        PREVIOUS: 'PREV',
        NEXT: 'NEXT',
        TODAY: 'TODAY',
        DATE: 'DATE'
    };
    
    export const views = {
        MONTH: 'month',
        WEEK: 'week',
        WORK_WEEK: 'work_week',
        DAY: 'day',
        AGENDA: 'agenda'
    };
    
    const CustomToolbar: React.SFC<ICustomTooolbarProps> = (props) => {
        function navigate(action) {
            props.onNavigate(action);
        }
    
        function viewItem(view) {
            props.onViewChange(view);
        }
    
        function viewNamesGroup(messages) {
            const 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;
    

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

    <BigCalendar
        popup
        onNavigate={(focusDate, flipUnit, prevOrNext) => this.updateCalendarData(focusDate, prevOrNext)}
        onSelectEvent={(data) => this.showModalInfo(data)}
        events={this.state.data}
        views={['month', 'week', 'agenda']}
        components={{ toolbar: CustomToolbar }} // <- Custom toolbar
    />

    希望对你有帮助;)

    【讨论】:

      【解决方案2】:

      对 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>
            ));
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-26
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 2018-03-11
        • 2022-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多