Search code examples
javascriptreactjscalendar

Change color of react-big-calendar events


I need to make a calendar with events and I decided to use react-big-calendar. But I need to make events of different colors. So each event will have some category and each category has corresponding color. How can I change the color of the event with react? react-big-calendar same color example

Result should look something like this react-big-calendar different colors example


Solution

  • Sorry, I haven't read the documentation really well. It can be done with the help of eventPropGetter attribute. I've made it like this:

    eventStyleGetter: function(event, start, end, isSelected) {
        console.log(event);
        var backgroundColor = '#' + event.hexColor;
        var style = {
            backgroundColor: backgroundColor,
            borderRadius: '0px',
            opacity: 0.8,
            color: 'black',
            border: '0px',
            display: 'block'
        };
        return {
            style: style
        };
    },
    
    render: function () {
    
        return (
            <Layout active="plan" title="Planning">
                <div className="content-app fixed-header">
                    <div className="app-body">
                        <div className="box">
                            <BigCalendar
                                events={this.events}
                                defaultDate={new Date()}
                                defaultView='week'
                                views={[]}
                                onSelectSlot={(this.slotSelected)}
                                onSelectEvent={(this.eventSelected)}
                                eventPropGetter={(this.eventStyleGetter)}
                                />
                        </div>
                    </div>
                </div>
            </Layout>
        );
    }