From acc9f1df1055c773a2459ad117e62c3b62ec900b Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 3 Nov 2022 11:40:36 +0800 Subject: [PATCH] fix(calendar): events cannot support moment (#1017) * fix(calendar): events cannot support moment * fix: avoid manipulating raw values --- .../antd/calendar/Calendar.tsx | 41 +++++++------------ .../antd/calendar/DeleteEvent.tsx | 3 +- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/calendar/Calendar.tsx b/packages/core/client/src/schema-component/antd/calendar/Calendar.tsx index 8beeaf4c7..afae49a1d 100644 --- a/packages/core/client/src/schema-component/antd/calendar/Calendar.tsx +++ b/packages/core/client/src/schema-component/antd/calendar/Calendar.tsx @@ -84,32 +84,32 @@ const useEvents = (dataSource: any, fieldNames: any, date: Date, view: typeof We startDate.startOf('week'); endDate.endOf('week'); } - const push = (fields?: Record) => { + const push = (eventStart: moment.Moment = start) => { // 必须在这个月的开始时间和结束时间,切在日程的开始时间之后 - const eventStart: moment.Moment = fields?.start || start; if (eventStart.isBefore(start) || !eventStart.isBetween(startDate, endDate)) { return; } - const event = { - id: get(item, fieldNames.id || 'id'), - title: get(item, fieldNames.title) || t('Untitle'), - end: eventStart.add(intervalTime, 'millisecond'), - ...fields, - start: eventStart, - }; let out = false; const res = exclude?.some((d) => { if (d.endsWith('_after')) { d = d.replace(/_after$/, ''); out = true; - return event.start.isSameOrAfter(d); + return eventStart.isSameOrAfter(d); } else { - return event.start.isSame(d); + return eventStart.isSame(d); } }); if (res) return out; + + const event = { + id: get(item, fieldNames.id || 'id'), + title: get(item, fieldNames.title) || t('Untitle'), + start: eventStart.toDate(), + end: eventStart.add(intervalTime, 'millisecond').toDate(), + }; + events.push(event); }; @@ -121,21 +121,15 @@ const useEvents = (dataSource: any, fieldNames: any, date: Date, view: typeof We .date(startDate.date()) .day(start.day()); while (nextStart.isBefore(endDate)) { - if ( - push({ - start: nextStart.clone(), - }) - ) { + if (push(nextStart.clone())) { break; } nextStart.add(1, 'week'); } } else if (cron === 'every_month') { - push({ - start: start.clone().year(dateM.year()).month(dateM.month()), - }); + push(start.clone().year(dateM.year()).month(dateM.month())); } else if (cron === 'every_year') { - push({ start: start.clone().year(dateM.year()) }); + push(start.clone().year(dateM.year())); } else { push(); if (!cron) return; @@ -149,12 +143,7 @@ const useEvents = (dataSource: any, fieldNames: any, date: Date, view: typeof We }); while (interval.hasNext()) { const { value } = interval.next(); - if ( - push({ - start: moment(value.toDate()), - }) - ) - break; + if (push(moment(value.toDate()))) break; } } catch (err) { console.error(err); diff --git a/packages/core/client/src/schema-component/antd/calendar/DeleteEvent.tsx b/packages/core/client/src/schema-component/antd/calendar/DeleteEvent.tsx index cb688a036..704de4023 100644 --- a/packages/core/client/src/schema-component/antd/calendar/DeleteEvent.tsx +++ b/packages/core/client/src/schema-component/antd/calendar/DeleteEvent.tsx @@ -1,5 +1,6 @@ import { observer } from '@formily/react'; import { Modal, Radio, Space, Typography } from 'antd'; +import moment from 'moment'; import React, { useState } from 'react'; import { createPortal } from 'react-dom'; import { useTranslation } from 'react-i18next'; @@ -11,7 +12,7 @@ const { Text } = Typography; export const DeleteEvent = observer(() => { const { visible, setVisible } = useActionContext(); const { exclude = [], cron, ...record } = useRecord(); - const startDate = record.__parent.__event.start.format(); + const startDate = moment(record.__parent.__event.start).format(); const filterByTk = useFilterByTk(); const { resource, service, __parent } = useBlockRequestContext(); const [value, onChange] = useState(startDate);