• @fullcalendar/react使用


    1. import React, { useState, useEffect, useRef } from 'react'
    2. import FullCalendar from '@fullcalendar/react' // must go before plugins
    3. import dayGridPlugin from '@fullcalendar/daygrid'
    4. let calendarApi = null;
    5. function Contact() {
    6. const calendarRef = useRef(null);
    7. useEffect(() => {
    8. calendarApi = calendarRef.current.getApi(); // 获取api,使用内置方法
    9. FullCalendarDate(calendarApi.view.title, 'month'); // calendarApi.view.title 获取当前时间,存储store
    10. }, []);
    11. const [fullCalendarType, setFullCalendarType] = useState("");
    12. const [eventSources, setEventSources] = useState({
    13. events: [ // put the array in the `events` property
    14. {
    15. title: 'event1',
    16. start: '2010-01-01'
    17. },
    18. {
    19. title: 'event2',
    20. start: '2010-01-05',
    21. end: '2010-01-05'
    22. },
    23. {
    24. title: 'event3',
    25. start: '2010-01-09',
    26. end: '2010-01-09',
    27. }
    28. ],
    29. color: 'black', // an option!
    30. textColor: 'yellow' // an option!
    31. });
    32. const [initialDate, setinitialDate] = useState('2010-01-01')
    33. // 今天
    34. const getToday = () => {
    35. calendarApi.today(); // 调用内置方法
    36. FullCalendarDate(calendarApi.view.title);
    37. };
    38. // 下一月
    39. const getNext = () => {
    40. calendarApi.next(); // 调用内置方法
    41. FullCalendarDate(calendarApi.view.title);
    42. };
    43. // 上一月
    44. const getPrev = () => {
    45. calendarApi.prev(); // 调用内置方法
    46. FullCalendarDate(calendarApi.view.title);
    47. };
    48. //
    49. const month = () => {
    50. calendarApi.changeView("dayGridMonth"); // 调用内置方法
    51. FullCalendarDate(calendarApi.view.title, "month");
    52. };
    53. //
    54. const week = () => {
    55. calendarApi.changeView("timeGridWeek"); // 调用内置方法
    56. FullCalendarDate(calendarApi.view.title, "week");
    57. };
    58. // 列表
    59. const list = () => {
    60. calendarApi.changeView("listDay"); // 调用内置方法
    61. FullCalendarDate(calendarApi.view.title, "list");
    62. };
    63. // 页面内容点击事件
    64. const eventClick = (eventInfo) => {
    65. // 可控制弹窗显示等操作
    66. };
    67. const FullCalendarDate = (dateTime, type) => {
    68. if (type) setFullCalendarType(type)
    69. getFullCalendarData(dateTime, type ?? fullCalendarType)
    70. }
    71. const getFullCalendarData = (dateTime, type) => {
    72. // dateTime当前时间,type类型
    73. switch (type) {
    74. case "month":
    75. console.log("请求接口")
    76. break
    77. case "week":
    78. console.log("请求接口")
    79. break
    80. case "list":
    81. console.log("请求接口")
    82. break
    83. default:
    84. }
    85. }
    86. const getevent = () => {
    87. console.log("get event api", calendarApi)
    88. }
    89. const setevent = (date, tag) => {
    90. if (date === "2010-02") {
    91. console.log("setevent 2010-02", initialDate)
    92. setEventSources({
    93. events: [ // put the array in the `events` property
    94. {
    95. title: 'event1:' + tag,
    96. start: `${date}-01`,
    97. end: `${date}-01`,
    98. },
    99. {
    100. title: 'event2' + tag,
    101. start: `${date}-02`,
    102. end: `${date}-02`,
    103. },
    104. {
    105. title: 'event3' + tag,
    106. start: `${date}-03`,
    107. end: `${date}-03`,
    108. }
    109. ],
    110. color: 'black', // an option!
    111. textColor: 'yellow' // an option!
    112. })
    113. }
    114. else if (date === "2010-03") {
    115. setEventSources({
    116. events: [ // put the array in the `events` property
    117. {
    118. title: 'event1:' + tag,
    119. start: `${date}-01`,
    120. end: `${date}-01`,
    121. },
    122. {
    123. title: 'event2' + tag,
    124. start: `${date}-02`,
    125. end: `${date}-02`,
    126. },
    127. {
    128. title: 'event3' + tag,
    129. start: `${date}-03`,
    130. end: `${date}-03`,
    131. }
    132. ],
    133. color: 'black', // an option!
    134. textColor: 'yellow' // an option!
    135. })
    136. }
    137. else {
    138. setEventSources({
    139. events: [ // put the array in the `events` property
    140. {
    141. title: 'event1:' + tag,
    142. start: `${date}-01`,
    143. end: `${date}-01`,
    144. },
    145. {
    146. title: 'event2' + tag,
    147. start: `${date}-02`,
    148. end: `${date}-02`,
    149. },
    150. {
    151. title: 'event3' + tag,
    152. start: `${date}-03`,
    153. end: `${date}-03`,
    154. }
    155. ],
    156. color: 'red', // an option!
    157. textColor: 'white' // an option!
    158. })
    159. }
    160. // setinitialDate(date === "2010-03" ? `${date}-09` : `${date}-01`)
    161. console.log("setevent initialDate", initialDate)
    162. calendarApi.gotoDate(date === "2010-03" ? `${date}-09` : `${date}-01`);
    163. }
    164. const changedata = function (n) {
    165. switch (n) {
    166. case 1:
    167. setevent("2010-02", "1111111");
    168. console.log("changedata=2010-02")
    169. break;
    170. case 2:
    171. setevent("2010-03", 22222)
    172. console.log("changedata=2010-03")
    173. break;
    174. case 3:
    175. setevent("2022-03", 3333)
    176. console.log("changedata=2022-03")
    177. break;
    178. default:
    179. break;
    180. }
    181. }
    182. return (
    183. <div>
    184. <h1>Contact</h1>
    185. <p>
    186. Aliquam iaculis a nisi sed ornare. Sed venenatis tellus vel consequat
    187. congue. In bibendum vestibulum orci et feugiat.
    188. </p>
    189. <div>
    190. <button onClick={getNext}>getNext</button>
    191. <button onClick={() => changedata(1)}>变成2</button>
    192. <button onClick={() => changedata(2)}>变成3</button>
    193. <button onClick={() => changedata(3)}>变成20221</button>
    194. <button onClick={getevent}>get event</button>
    195. </div>
    196. <FullCalendar
    197. ref={calendarRef}
    198. plugins={[dayGridPlugin]}
    199. height={600} // 高度
    200. initialView="dayGridMonth" // 默认视图
    201. headerToolbar={{
    202. left: "getToday,getPrev,getNext", // 左侧按钮展示为下方自定义按钮名称
    203. center: "title", // 时间展示
    204. right: "month,week,list" // 右侧按钮展示
    205. }}
    206. eventClick={eventClick} // 事项点击事件
    207. firstDay={1} // 默认第一行展示第一周
    208. allDayText="全天" // 全部展示文字
    209. eventSources={[eventSources]} // 数据源
    210. initialDate={initialDate}
    211. eventContent={(event) => {
    212. console.log("event", event.event.title)
    213. return <div>111</div>
    214. }}
    215. customButtons={{
    216. // 自定义按钮触发回调函数
    217. getToday: { text: "今天", click: getToday },
    218. getNext: { text: ">", click: getNext },
    219. getPrev: { text: "<", click: getPrev },
    220. month: { text: "月", click: month },
    221. week: { text: "周", click: week },
    222. list: { text: "列表", click: list }
    223. }}
    224. />
    225. </div>
    226. )
    227. }
    228. export default Contact

    参考:

    fullCalendar日历,React-Hooks简单使用 - 简书 

    Documentation | FullCalendar

  • 相关阅读:
    基于python的CLI应用程序开发(第一节):简单了解一下Typer
    什么是网络智能运维?如何保障业务7x24小时在线?
    职业性格在求职应聘和跳槽中的作用
    如何阅读 Spring Cloud OpenFein 源码
    reactor的原理与实现
    BI是什么?想要了解BI需要从哪些方面入手?
    目标检测算法改进系列之Backbone替换为Swin Transformer
    Maven安装配置-2022
    深度学习的进展
    linux之权限管理和组
  • 原文地址:https://blog.csdn.net/u014071104/article/details/133980050