React Big Calendar how to style a single day in the month view

后端 未结 1 903
甜味超标
甜味超标 2021-02-10 05:05

So as seen on the picture, I want to style individual events.

Example of how it should look

With the slotpropgetter it\'s possible to conditionally render styles

相关标签:
1条回答
  • 2021-02-10 06:00

    The components prop can be used to individually change how parts of the calendar are rendered:

    import React, {Children} from 'react';
    import BigCalendar from 'react-big-calendar';
    import moment from 'moment';
    
    BigCalendar.momentLocalizer(moment);
    
    const CURRENT_DATE = moment().toDate();
    
    // example implementation of a wrapper
    const ColoredDateCellWrapper = ({children, value}) =>
        React.cloneElement(Children.only(children), {
            style: {
                ...children.style,
                backgroundColor: value < CURRENT_DATE ? 'lightgreen' : 'lightblue',
            },
        });
    
    const MyCalendar = props => (
        <div style={{height: '100vh', margin: '10px'}}>
            <BigCalendar
                events={[]}
                startAccessor="startDate"
                endAccessor="endDate"
                components={{
                    // you have to pass your custom wrapper here
                    // so that it actually gets used
                    dateCellWrapper: ColoredDateCellWrapper,
                }}
            />
        </div>
    );
    

    Working Example:

    It has the following type definition:

    {
      event?: ReactClass<any>,
      eventWrapper?: ReactClass<any>,
      dayWrapper?: ReactClass<any>,
      dateCellWrapper?: ReactClass<any>,
      toolbar?: ReactClass<any>,
      agenda?: {
        date?: ReactClass<any>,
        time?: ReactClass<any>,
        event?: ReactClass<any>
      },
      day?: {
        header?: ReactClass<any>,
        event?: ReactClass<any>
      },
      week?: {
        header?: ReactClass<any>,
        event?: ReactClass<any>
      },
      month?: {
        header?: ReactClass<any>,
        dateHeader?: ReactClass<any>,
        event?: ReactClass<any>
      }
    }
    
    0 讨论(0)
提交回复
热议问题