

React Calendar: Rendering JSX in Events
source link: https://code.daypilot.org/79731/react-calendar-rendering-jsx-in-events
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Downloads
Features
Render custom JSX components in React Calendar events
You can also use active areas to insert rich content and icons
Includes a trial version of DayPilot Pro for JavaScript (see License below)
License
Licensed for testing and evaluation purposes. Please see the license agreement included in the sample project. You can use the source code of the tutorial if you are a licensed user of DayPilot Pro for JavaScript. Buy a license.
How to Add an Icon using Active Areas
This example adds an icon to the calendar event box using active areas. Active area is a built-in mechanism for adding custom elements at specified locations with Calendar (events, grid cells, time headers, etc.). You can define visibility (always visible or visible on hover), CSS class, trigger special actions (add drag handles, open bubbles with details on hover) or define you own click event handler. Event active areas are supported during image export as well.
onBeforeEventRender: args => { // ... args.data.areas = [ {right: 6, top: 6, width: 17, height: 17, image: "info-17-inverted-rounded-semi.svg", onClick: args=> this.showDetails(args.source)}, ]; },
How to Add an Icon using JSX
You can define custom event content using JSX in onBeforeEventDomAdd
event handler:
onBeforeEventDomAdd: args => { args.element = <div> {args.e.data.text} <div style={{position: "absolute", right: "25px", top: "5px", height: "17px", width: "17px"}} onClick={()=>this.showDetails(args.e)}> <img src={"info-17-semi.svg"} alt={"Info icon"}/> </div> </div>; }
Full Source Code
Calendar.js
import React, {Component} from 'react'; import {DayPilot, DayPilotCalendar} from "daypilot-pro-react"; class Calendar extends Component { constructor(props) { super(props); this.state = { startDate: "2022-02-02", viewType: "Week", cellHeight: 30, timeRangeSelectedHandling: "Enabled", onTimeRangeSelected: function (args) { DayPilot.Modal.prompt("Create a new event:", "Event 1").then(function(modal) { var dp = args.control; dp.clearSelection(); if (!modal.result) { return; } dp.events.add(new DayPilot.Event({ start: args.start, end: args.end, id: DayPilot.guid(), text: modal.result })); }); }, onBeforeEventRender: args => { args.data.backColor = "#93c47d"; args.data.barHidden = true; args.data.fontColor = "white"; args.data.borderColor = "darker"; args.data.areas = [ {right: 6, top: 6, width: 17, height: 17, image: "info-17-inverted-rounded-semi.svg", onClick: args=> this.showDetails(args.source)}, ]; }, onBeforeEventDomAdd: args => { args.element = <div> {args.e.data.text} <div style={{position: "absolute", right: "25px", top: "5px", height: "17px", width: "17px"}} onClick={()=>this.showDetails(args.e)}> <img src={"info-17-semi.svg"} alt={"Info icon"}/> </div> </div>; } }; } showDetails(e) { DayPilot.Modal.alert(e.data.text); } componentDidMount() { // load resource and event data this.setState({ events: [ { id: 1, text: "Event 1", start: "2022-02-01T09:00:00", end: "2022-02-01T14:00:00", }, { id: 2, text: "Event 2", start: "2022-02-02T10:00:00", end: "2022-02-02T15:00:00", } ] }); } render() { return ( <div> <DayPilotCalendar {...this.state} ref={component => { this.calendar = component && component.control; }} /> </div> ); } } export default Calendar;
Recommend
-
30
Styled-JSX for React: How and Why How and why you should use styled-jsx for your React SPA and SSR apps.
-
7
Prelude This article is an interactive version of Facebook official tutorial JSX in depth using the KLIPSE plugin t...
-
14
New JSX Enhancements in React 17What’s New for JSX in React 17 and Why You Should Care
-
20
How to insert React components or direct JSX to Scheduler row headers.
-
9
Core React Concept: JSXHello Fellow Codenewbies ?, Let's say that we want to create an element in DOM that has not existed yet and append it to a div with ID "root". Then this is what we would do in vanilla Javascript: H...
-
13
Nick Scialli • June 14, 2021 • ? 3 minute readSometimes we want to have content in React be wrapped in another component, and sometimes we don’t. In this post, we create a Wrapper th...
-
4
What is a JSX Transform? JSX, as we know, is an HTML-like syntax that is used by React components to render in the browser. Example JSX - import React from 'react'; function Greet() { return <h1>Hell...
-
26
The React Scheduler allows defining the callout (bubble) content as a React JSX component. The component will be created automatically when the callout is activated and removed and unmounted when the callout is hidden. ...
-
16
How JSX (React) Works Under the Hood You’ve undoubtedly heard of JSX if you’ve worked with React. In this blog, we’ll see how JSX translates to r...
-
6
React JSX语法再实践 1月 7, 2022 143点热度 0人点赞...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK