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?
Result should look something like this
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.
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.
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.
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.
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> ); }
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 }; } } />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With