更改 FullCalendar 中事件的宽度(React JS)

发布于 2025-01-15 21:05:56 字数 383 浏览 2 评论 0原文

我喜欢在 React JS 上下文中更改事件的宽度。

此处描述的类似问题:

不幸的是,在引用的问题中什么也没有提到了如何在react环境中解决这个问题。

I like to change the width of an event in context of React JS.

Similiar questions described here:

Unfortunately, in the quoted questions is nothing mentioned how to solve this in a react environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-22 21:05:56

我想出了如何去做。 eventRender 不再存在 (v4),而是不同的“事件渲染挂钩”(v5):

现在,根据您想要实现的目标,在 React JS 中有两种方法可以实现此目的。 (注意:我使用了 TypeScript

将 CSS 更改应用于所有事件

我们可以使用 styled 为任何事件创建我们自己的 .css 定义并使用作为包装器 (StyleWrapper)

import React from 'react';
import FullCalendar from '@fullcalendar/react';
import timeGridPlugin from '@fullcalendar/timegrid';
import styled from '@emotion/styled';
export interface ISampleProps {}

//our Wrapper that will go around FullCalendar
export const StyleWrapper = styled.div`
    .fc-event {
        width: 98px !important;
    }
`;
//Reacct Functional Component
const Sample: React.FunctionComponent<ISampleProps> = (props) => {
    
    const events = [ 
       /*some events */  
    ];

    return (
        <>
            <div>
                <StyleWrapper>
                    <FullCalendar
                        plugins={[timeGridPlugin]}
                        initialView="timeGridWeek"
                        events={events}
                    />
                </StyleWrapper>
            </div>
        </>
    );
};

export default Sample;

将特定 CSS 应用于特定事件

通过这种方式,您可以准确地告诉 FullCalendar 事件的外观取决于您添加到的自定义道具 事件。您自定义的 props 将被添加到 extendedProps 中,它将在我们的事件渲染钩子 eventClassNames 中使用

//same imports from earlier (but you don't need "styled" for this one)
const Sample: React.FunctionComponent<ISampleProps> = (props) => {

     function eventAddStyle(arg: any) {
        //all self-created props are under "extendedProps"
        if (arg.event.extendedProps.demanding) {
            return ['maxLevel']; //maxLevel and lowLevel are two CSS classes defined in a .css file 
        } else {
            return ['lowLevel'];
        }
    }  

    const events = [ 
        {
            id: 'a',
            title: 'This is just an example',
            start: '2022-03-19T12:30:00',
            end: '2022-03-19T16:30:00',
            backgroundColor: '#74AAEB',
            demanding: true //our self-created props
        },
        {
            id: 'b',
            title: 'This is another example',
            start: '2022-03-17T08:00:00',
            end: '2022-03-17T11:30:00',
            demanding: false // our self-created props
        }, 
    ];
    return (
        <>
            <div>
                <FullCalendar
                    plugins={[timeGridPlugin]}
                    initialView="timeGridWeek"
                    eventClassNames={eventAddStyle}
                    events={events}
                />
            </div>
        </>
    );
};

export default Sample;

I figured it out how to do it. eventRender does no longer exist (v4) but instead different "event render hooks" (v5):

Now, depending what you want to achieve, there are two ways to do this in React JS. (Note: I used TypeScript)

Applying CSS change to all events

We can use styled to create our own .css definition for any event and use that as a wrapper (StyleWrapper)

import React from 'react';
import FullCalendar from '@fullcalendar/react';
import timeGridPlugin from '@fullcalendar/timegrid';
import styled from '@emotion/styled';
export interface ISampleProps {}

//our Wrapper that will go around FullCalendar
export const StyleWrapper = styled.div`
    .fc-event {
        width: 98px !important;
    }
`;
//Reacct Functional Component
const Sample: React.FunctionComponent<ISampleProps> = (props) => {
    
    const events = [ 
       /*some events */  
    ];

    return (
        <>
            <div>
                <StyleWrapper>
                    <FullCalendar
                        plugins={[timeGridPlugin]}
                        initialView="timeGridWeek"
                        events={events}
                    />
                </StyleWrapper>
            </div>
        </>
    );
};

export default Sample;

Apply specific CSS to specific events

With this way, you can tell FullCalendar exactly how an event has to look like depending self-defined props you add to an event. Your self-defined props will be added to extendedProps which will be used in our event render hook eventClassNames

//same imports from earlier (but you don't need "styled" for this one)
const Sample: React.FunctionComponent<ISampleProps> = (props) => {

     function eventAddStyle(arg: any) {
        //all self-created props are under "extendedProps"
        if (arg.event.extendedProps.demanding) {
            return ['maxLevel']; //maxLevel and lowLevel are two CSS classes defined in a .css file 
        } else {
            return ['lowLevel'];
        }
    }  

    const events = [ 
        {
            id: 'a',
            title: 'This is just an example',
            start: '2022-03-19T12:30:00',
            end: '2022-03-19T16:30:00',
            backgroundColor: '#74AAEB',
            demanding: true //our self-created props
        },
        {
            id: 'b',
            title: 'This is another example',
            start: '2022-03-17T08:00:00',
            end: '2022-03-17T11:30:00',
            demanding: false // our self-created props
        }, 
    ];
    return (
        <>
            <div>
                <FullCalendar
                    plugins={[timeGridPlugin]}
                    initialView="timeGridWeek"
                    eventClassNames={eventAddStyle}
                    events={events}
                />
            </div>
        </>
    );
};

export default Sample;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文