一、项目创建
1、创建react项目
1)、使用npx创建
npx create-react-app react-fullcalendar
cd react-fullcalendar
2)、使用vite创建
使用的后者,因为可以直接安装typescript
npm init vite@latest
cd react-fullcalendar
npm i
使用vite创建项目.png
2、安装依赖
npm i @fullcalendar/react //安装react版fullcalendar
npm i @fullcalendar/daygrid
npm i @fullcalendar/core
npm i @fullcalendar/interaction
npm i @fullcalendar/list
npm i @fullcalendar/timegrid
二、代码块
1、在src/data/event-utils.ts
import { EventInput } from '@fullcalendar/core'
let eventGuid = 0
let todayStr = new Date().toISOString().replace(/T.*$/, '') // YYYY-MM-DD of today
export const INITIAL_EVENTS: EventInput[] = [
{
id: createEventId(),
title: 'All-day event',
start: todayStr
},
{
id: createEventId(),
title: 'Timed event',
start: todayStr + 'T12:00:00'
}
]
export function createEventId() {
return String(eventGuid++)
}
2、在src/App.css
html,
body,
body > div { /* the react root */
margin: 0;
padding: 0;
}
#root{
width: 100%;
}
h2 {
margin: 0;
font-size: 16px;
}
ul {
margin: 0;
padding: 0 0 0 1.5em;
}
li {
margin: 1.5em 0;
padding: 0;
}
b { /* used for event dates/times */
margin-right: 3px;
}
.demo-app {
display: flex;
min-height: 100%;
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
font-size: 14px;
}
.demo-app-sidebar {
width: 300px;
line-height: 1.5;
background: #eaf9ff;
border-right: 1px solid #d3e2e8;
}
.demo-app-sidebar-section {
padding: 2em;
}
.demo-app-main {
flex-grow: 1;
padding: 3em;
}
.fc { /* the calendar root */
max-width: 1100px;
margin: 0 auto;
}
3、在src/components/Fullcalendar.tsx
import React from 'react'
import {
EventApi,
DateSelectArg,
EventClickArg,
EventContentArg,
formatDate,
} from '@fullcalendar/core'
import FullCalendar from '@fullcalendar/react'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import interactionPlugin from '@fullcalendar/interaction'
import { INITIAL_EVENTS, createEventId } from '../data/event-utils'
interface DemoAppState {
weekendsVisible: boolean
currentEvents: EventApi[]
}
export class Fullcalendar extends React.Component<{}, DemoAppState> {
state: DemoAppState = {
weekendsVisible: true,
currentEvents: []
}
render() {
return (
<div className='demo-app'>
{this.renderSidebar()}
<div className='demo-app-main'>
<FullCalendar
plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin]}
headerToolbar={{
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
}}
initialView='dayGridMonth'
editable={true}
selectable={true}
selectMirror={true}
dayMaxEvents={true}
weekends={this.state.weekendsVisible}
initialEvents={INITIAL_EVENTS} // alternatively, use the `events` setting to fetch from a feed
select={this.handleDateSelect}
eventContent={renderEventContent} // custom render function
eventClick={this.handleEventClick}
eventsSet={this.handleEvents} // called after events are initialized/added/changed/removed
/* you can update a remote database when these fire:
eventAdd={function(){}}
eventChange={function(){}}
eventRemove={function(){}}
*/
/>
</div>
</div>
)
}
renderSidebar() {
return (
<div className='demo-app-sidebar'>
<div className='demo-app-sidebar-section'>
<h2>Instructions</h2>
<ul>
<li>Select dates and you will be prompted to create a new event</li>
<li>Drag, drop, and resize events</li>
<li>Click an event to delete it</li>
</ul>
</div>
<div className='demo-app-sidebar-section'>
<label>
<input
type='checkbox'
checked={this.state.weekendsVisible}
onChange={this.handleWeekendsToggle}
></input>
toggle weekends
</label>
</div>
<div className='demo-app-sidebar-section'>
<h2>All Events ({this.state.currentEvents.length})</h2>
<ul>
{this.state.currentEvents.map(renderSidebarEvent)}
</ul>
</div>
</div>
)
}
handleWeekendsToggle = () => {
this.setState({
weekendsVisible: !this.state.weekendsVisible
})
}
handleDateSelect = (selectInfo: DateSelectArg) => {
let title = prompt('Please enter a new title for your event')
let calendarApi = selectInfo.view.calendar
calendarApi.unselect() // clear date selection
if (title) {
calendarApi.addEvent({
id: createEventId(),
title,
start: selectInfo.startStr,
end: selectInfo.endStr,
allDay: selectInfo.allDay
})
}
}
handleEventClick = (clickInfo: EventClickArg) => {
if (confirm(`Are you sure you want to delete the event '${clickInfo.event.title}'`)) {
clickInfo.event.remove()
}
}
handleEvents = (events: EventApi[]) => {
this.setState({
currentEvents: events
})
}
}
function renderEventContent(eventContent: EventContentArg) {
return (
<>
<b>{eventContent.timeText}</b>
<i>{eventContent.event.title}</i>
</>
)
}
function renderSidebarEvent(event: EventApi) {
return (
<li key={event.id}>
<b>{formatDate(event.start!, {year: 'numeric', month: 'short', day: 'numeric'})}</b>
<i>{event.title}</i>
</li>
)
}
4、在src/App.tsx
import './App.css'
import { Fullcalendar } from './components/Fullcalendar';
function App() {
return (
<div className="App">
<Fullcalendar/>
</div>
)
}
export default App
三、运行项目
npm run dev
image.png
网友评论