React Big Schedule

repository·master·Indexed 41 days ago

https://github.com/ansulagrawal/react-big-schedule

A powerful and intuitive scheduler and resource planning solution for React applications. It features drag-and-drop functionality, granular views (Day, Week, Month, Quarter, Year), and support for both Resource and Task views. Version 5.5.2-beta.5 provides tools for managing appointments, resource allocation, and recurring tasks via rrule, utilizing SchedulerData for state management and integration with dayjs and Ant Design for localization.

Tokens
9.7K
Snippets
26
Records
39
Agent score
52%

What's inside React Big Schedule

  1. How to use SchedulerData to configure the scheduler

    master

    The SchedulerData class is used to manage the state of the scheduler, including the current date, view type, resources, events, and locales.

    Key methods include:

    • setResources(resources: Array): Defines the resources (e.g., people, rooms) available in the scheduler. Resources can have a parentId to create hierarchies or groupOnly: true to act as a group.
    • setEvents(events: Array): Sets the list of scheduled events. Important: The event array must be sorted in ascending order by the event.start property to avoid rendering errors.
    • setSchedulerLocale(locale: string): Sets the dayjs locale for the scheduler.
    • setCalendarPopoverLocale(locale: string): Sets the Ant Design locale for the calendar popover.

    Events can include properties like resizable, movable, and startResizable (set to false to disable these interactions) and rrule for recurring tasks.

    const schedulerData = new SchedulerData(new dayjs().format(DATE_FORMAT), ViewType.Week);
    
    schedulerData.setResources([
      { id: 'r0', name: 'Resource0', groupOnly: true },
      { id: 'r1', name: 'Resource1' },
      { id: 'r2', name: 'Resource2', parentId: 'r0' },
    ]);
    
    schedulerData.setEvents([
      {
        id: 1,
        start: '2022-12-18 09:30:00',
        end: '2022-12-19 23:30:00',
        resourceId: 'r1',
        title: 'I am finished',
        bgColor: '#D9D9D9',
      },
    ]);
  2. How Resource View and Task View work

    master

    The Scheduler's rendering mode is determined by the combination of showAgenda and isEventPerspective within the SchedulerData configuration:

    1. Resource View: Rendered when both showAgenda and isEventPerspective are false. In this view, every slot (row) describes how many events a resource performs within the time window.
    2. Task View: Rendered when isEventPerspective is true. In this view, every slot describes how a big task is divided among participants. To group events into a single task slot, add groupId and groupName properties to your event objects. If these are omitted, the scheduler uses the event's id as the groupId and title as the groupName.
    3. Agenda View: Rendered when showAgenda is true. This view is read-only.
  3. Customize Scheduler Logic with Behaviors

    master

    The SchedulerData.behaviors object provides hooks to inject custom logic into the scheduler's rendering and calculation processes:

    • getEventTextFunc(schedulerData, event): Define the text displayed inside an event.
    • isNonWorkingTimeFunc(schedulerData, time): Define which time slots are considered non-working time.
    • getSummaryFunc(schedulerData, headerEvents, slotId, slotName, headerStart, headerEnd): Define the summary text displayed in scheduler cells.
    • getCustomDateFunc(schedulerData, num, date): Control the time window for 'Custom' view types.
    • getNonAgendaViewBodyCellBgColorFunc(schedulerData, slotId, header): Dynamically set cell background colors.
    • getScrollSpecialDayjsFunc(schedulerData, startDayjs, endDayjs): Define a specific date/time that the scheduler should automatically scroll to.
  4. Format Event Dates

    master

    Events in React Big Schedule support two date formats for the start and end properties:

    1. Datetime strings: YYYY-MM-DD HH:mm:ss (e.g., 2023-10-27 14:30:00).
    2. Date-only strings: YYYY-MM-DD (e.g., 2023-10-27).

    Date-only events are treated as spanning the full calendar day. Both formats are handled consistently across Month and other scheduler views.

  5. Configure Scheduler Appearance and Behavior

    master

    The SchedulerData.config object allows extensive customization of the scheduler's layout and behavior. Key configuration areas include:

    Layout & Sizing

    • schedulerWidth: Percentage width of the scheduler (e.g., '100%').
    • responsiveByParent: If true, responsiveness is based on the parent container width (requires passing parentRef to the Scheduler component).
    • schedulerMaxHeight: Sets max height. If exceeded, the header freezes and a scrollbar appears. Set to 0 to conform to parent height when using responsiveByParent.
    • underneathHeight: Reserves pixel space at the bottom of the parent container to prevent overlap with footers.
    • tableHeaderHeight, agendaResourceTableWidth, dayCellWidth, etc.: Control specific dimension values for various views.

    Event & Interaction Settings

    • startResizable / endResizable: Controls if event boundaries can be resized.
    • movable: Controls if events can be dragged.
    • creatable: Controls if new events can be created via interaction.
    • checkConflict: If true, triggers the conflictOccurred callback during creation, resizing, or moving.
    • crossResourceMove: If false, moveEvent will not change the slotId or slotName.
    • recurringEventsEnabled: Enables/disables support for recurring events via rrule strings.
    • dragAndDropEnabled: Enables/disables drag and drop functionality.
  6. Handle new event creation and conflicts in ResourceEvents

    master

    When config.creatable is enabled, users can create new events by clicking and dragging across the resource view. This triggers specific callbacks:

    1. newEvent: Called when a valid selection is made. It provides the schedulerData, slotId, slotName, startTime, endTime, and an options object containing resourceIds (supporting multi-resource events if more than one resource is selected).
    2. conflictOccurred: Called if config.checkConflict is true and the new event overlaps with existing events. It provides details about the conflicting time range and the resources involved.

    To support multi-resource event creation, ensure your newEvent handler can process the resourceIds array provided in the final argument.

    // Example of handling new events and conflicts
    const handleNewEvent = (schedulerData, slotId, slotName, startTime, endTime, undefined, { resourceIds }) => {
      console.log('Creating event for resources:', resourceIds || [slotId]);
    };
    
    const handleConflict = (schedulerData, type, eventData, dndType, slotId, slotName, startTime, endTime) => {
      console.warn('Conflict detected for:', slotId, 'at', startTime);
    };
  7. Configure responsive sizing with parentRef

    master

    To make the scheduler responsive to its container rather than the window, provide a parentRef (a React.RefObject<HTMLElement>) to the Scheduler component and ensure responsiveByParent: true is set in your schedulerData configuration.

    When these are provided, the scheduler uses a ResizeObserver to track the parent's dimensions and trigger layout updates automatically.

  8. Basic Usage Example

    master

    A complete example of initializing SchedulerData with resources, events, and locales, and rendering the Scheduler component.

    import { Scheduler, SchedulerData, ViewType, DATE_FORMAT } from "react-big-schedule";
    import dayjs from "dayjs";
    import "react-big-schedule/dist/css/style.css";
    
    const schedulerData = new SchedulerData(new dayjs().format(DATE_FORMAT), ViewType.Week);
    
    // Set locales
    schedulerData.setSchedulerLocale('pt-br');
    schedulerData.setCalendarPopoverLocale('pt_BR');
    
    // Set resources
    schedulerData.setResources([
      { id: 'r0', name: 'Resource0', groupOnly: true },
      { id: 'r1', name: 'Resource1' },
      { id: 'r2', name: 'Resource2', parentId: 'r0' },
      { id: 'r3', name: 'Resource3', parentId: 'r4' },
      { id: 'r4', name: 'Resource4', parentId: 'r2' },
    ]);
    
    // Set events (must be sorted by start time)
    schedulerData.setEvents([
      {
        id: 1,
        start: '2022-12-18 09:30:00',
        end: '2022-12-19 23:30:00',
        resourceId: 'r1',
        title: 'I am finished',
        bgColor: '#D9D9D9',
      },
      {
        id: 2,
        start: '2022-12-18 12:30:00',
        end: '2022-12-26 23:30:00',
        resourceId: 'r2',
        title: 'I am not resizable',
        resizable: false,
      },
      {
        id: 3,
        start: '2022-12-19 12:30:00',
        end: '2022-12-20 23:30:00',
        resourceId: 'r3',
        title: 'I am not movable',
        movable: false,
      },
      {
        id: 4,
        start: '2022-12-19 14:30:00',
        end: '2022-12-20 23:30:00',
        resourceId: 'r1',
        title: 'I am not start-resizable',
        startResizable: false,
      },
      {
        id: 5,
        start: '2022-12-19 15:30:00',
        end: '2022-12-20 23:30:00',
        resourceId: 'r2',
        title: 'R2 has recurring tasks every week on Tuesday, Friday',
        rrule: 'FREQ=WEEKLY;DTSTART=20221219T013000Z;BYDAY=TU,FR',
        bgColor: '#f759ab',
      },
    ]);
    
    // In your render method (ensure ancestor provides DragDropContext):
    <Scheduler
      schedulerData={schedulerData}
      prevClick={this.prevClick}
      nextClick={this.nextClick}
      onSelectDate={this.onSelectDate}
      onViewChange={this.onViewChange}
      eventItemClick={this.eventClicked}
    />;
  9. Manage events with setEvents

    master

    Use setEvents(events) to provide the event data for the scheduler.

    Requirements and Best Practices:

    • Sorting: The event array must be sorted in ascending order by the event.start property.
    • Task View: If you are using the Task View (isEventPerspective: true), it is recommended to add groupId and groupName properties to each event object to ensure they are grouped correctly into task slots.
  10. Set the Scheduler locale using setSchedulerLocale

    master

    Use setSchedulerLocale(preset) to change the language and formatting of the scheduler. This method uses dayjs locales and loads them on demand. By default, the scheduler uses English (en, United States).

    Pass a locale imported from dayjs as the preset argument.

    import * as dayjsLocale from 'dayjs/locale/pt-br';
    
    setSchedulerLocale(dayjsLocale);
  11. Manage Event Groups and Resources

    master

    Control how slots (resources or task groups) are handled in the scheduler:

    • setEventGroups(eventGroups): Manually sets the event groups (slots in task view). Ensure no duplicate eventGroup.id values exist.
    • setEventGroupsAutoGenerated(autoGenerated): Enables or disables automatic generation of event groups. If true, groups are generated based on event.groupId and event.groupName (falling back to event.id and event.title).
    • addResource(resource): Adds a new resource to SchedulerData.resources. Ensure resource.id is unique.
    • addEventGroup(eventGroup): Adds an event group to SchedulerData.eventGroups. Note: manual groups may be overridden if eventGroupsAutoGenerated is true.