Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 769
Max Zavernutiy Avatar asked Jan 04 '16 08:01

Max Zavernutiy


People also ask

How do I change calendar event colors?

The steps are the same for the Google Calendar app on both Android and iOS. Tap the menu button on the top left and select Settings near the bottom. Below the calendar you want to change, tap Events. Tap Color at the top and pick a new color.

How do you use react-big-calendar?

Learn how to use react-big-calendar by viewing and forking example apps that make use of react-big-calendar on CodeSandbox. react-big-calendar: Drag-and-drop events between monthsThis example illustrates that react-big-calendar doesn't allow events to be dragged from one month to another.

How do I change colors in react?

To change background color on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.

What is react-big-calendar?

An events calendar component built for React and made for modern browsers (read: IE10+) and uses flexbox over the classic tables-ception approach. DEMO and Docs. Inspired by Full Calendar.


2 Answers

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>     ); } 
like image 91
Max Zavernutiy Avatar answered Oct 08 '22 02:10

Max Zavernutiy


Additional tip on how to style different kinds of events: In the myEvents array of event objects, I gave each object a boolean property isMine, then I defined:

<BigCalendar    // other props here    eventPropGetter={     (event, start, end, isSelected) => {       let newStyle = {         backgroundColor: "lightgrey",         color: 'black',         borderRadius: "0px",         border: "none"       };        if (event.isMine){         newStyle.backgroundColor = "lightgreen"       }        return {         className: "",         style: newStyle       };     }   } /> 
like image 45
Morris Avatar answered Oct 08 '22 03:10

Morris