@bitnoi.se/react-scheduler

repository·master·Indexed 19 days ago

https://github.com/bitnoise/react-scheduler

A lightweight, TypeScript-oriented, high-performance React component for creating Gantt charts. It features customizable zoom levels (weeks, days, hours), multi-language support via LocaleProvider, and built-in light and dark themes. The library provides a Scheduler component and associated types like SchedulerData and Config for managing resource tiles and grid layouts. Includes integration guides for RemixJS, NextJS App Router, and NextJS Pages Router.

Tokens
5.6K
Snippets
22
Records
26
Agent score
67%

What's inside @bitnoi.se/react-scheduler

  1. Quickstart: Implement the Scheduler component

    master

    To use the Scheduler, follow these three steps:

    1. Import required styles: The component requires its CSS to render correctly.
    2. Import the component: Import Scheduler and SchedulerData from @bitnoi.se/react-scheduler.
    3. Provide data and props: Pass your data and event handlers to the <Scheduler /> component.

    Note: The example below uses dayjs for date calculations, but you are free to use any date library of your choice.

    import "@bitnoi.se/react-scheduler/dist/style.css";
    import { Scheduler, SchedulerData } from "@bitnoi.se/react-scheduler";
    import dayjs from "dayjs";
    
    export default function Component() {
      // ... implementation logic ...
      return (
        <Scheduler
          data={filteredData}
          isLoading={isLoading}
          onRangeChange={handleRangeChange}
          onTileClick={(clickedResource) => console.log(clickedResource)}
          onItemClick={(item) => console.log(item)}
          onFilterData={() => {}}
          onClearFilterData={() => {}}
          config={{ zoom: 0 }}
        />
      );
    }
  2. Install @bitnoi.se/react-scheduler

    master

    You can install the package using either yarn or npm.

    # yarn
    yarn add '@bitnoi.se/react-scheduler'
    
    # npm
    npm install '@bitnoi.se/react-scheduler'
    ```bash
    # yarn
    yarn add '@bitnoi.se/react-scheduler'
    # npm
    npm install '@bitnoi.se/react-scheduler'
    ```埋
  3. Set up the development environment

    master

    To set up the project locally for development and testing, ensure you are using the Node version specified in the .nvmrc file. Follow these steps:

    1. Clone the repository.
    2. Install dependencies using your preferred package manager (e.g., yarn install).
    3. Start the development server.
    4. Access the application at http://localhost:5173.
    git clone git@github.com:Bitnoise/react-scheduler.git
    yarn install
    yarn dev
  4. Configure the Scheduler via the config object

    master

    The config prop accepts a Config object to customize the scheduler's behavior and appearance.

    Property NameTypeDefaultDescription
    zoom0 or 1 or 200: weeks, 1: days, 2: hours
    filterButtonStatenumber0< 0: hide filter button; 0: no filters set; > 0: filters active (enables onClearFilterData)
    maxRecordsPerPagenumber50Number of items from SchedulerData visible per page
    langen, lt or plenScheduler's language
    includeTakenHoursOnWeekendsInDayViewbooleanfalseShow weekends as taken when a resource is longer than a week
    showTooltipbooleantrueShow tooltip when hovering over tiles
    translationsLocaleType[]undefinedOption to add specific language translations
    showThemeTogglebooleanfalseShow toggle button to switch between light/dark mode
    defaultThemelight or darklightScheduler's default theme
  5. Integrate Scheduler with RemixJS

    master

    When using @bitnoi.se/react-scheduler with RemixJS, you must add the package to serverDependenciesToBundle in your remix.config.js to ensure it is bundled correctly.

    // remix.config.js
    /** @type  {import('@remix-run/dev').AppConfig} */
    module.exports = {
    	// ...
    	serverDependenciesToBundle: [..., "@bitnoi.se/react-scheduler"],
    };
  6. Customize Scheduler dimensions

    master

    The Scheduler component is positioned absolutely to occupy all available space in its parent. To give it fixed dimensions, wrap it in a container with position: relative.

    export const StyledSchedulerFrame = styled.div`
      position: relative;
      height: 40vh;
      width: 40vw;
    `;
    
    <StyledSchedulerFrame>
        <Scheduler  {...}/>
    </StyledSchedulerFrame>
  7. Integrate Scheduler with NextJS (Pages Router)

    master

    When using the NextJS Pages Router, you must import the Scheduler component using next/dynamic with ssr: false to prevent server-side rendering issues.

    import dynamic from "next/dynamic";
    const Scheduler = dynamic(() => import("@bitnoi.se/react-scheduler").then((mod) => mod.Scheduler), {
      ssr: false
    });
  8. Integrate Scheduler with NextJS (App Router)

    master

    When using the NextJS App Router, the Scheduler component must be wrapped in a client component using the use client directive.

    "use client"
    import { Scheduler, SchedulerProps } from "@bitnoi.se/react-scheduler";
    
    export default function SchedulerClient(props: SchedulerProps) {
    	return <Scheduler {...props} />;
    }
  9. Customize Scheduler translations

    master

    You can provide custom translations by passing an array of LocaleType objects to the translations property in the config object. Each object requires an id, a lang (the Translation object), a translateCode (for localStorage), and dayjsTranslations.

    import enDayjsTranslations from "dayjs/locale/en";
    
    const langs: LocaleType[] = [
      {
        id: "en",
        lang: {
          feelingEmpty: "I feel so empty...",
          free: "Free",
          loadNext: "Next",
          loadPrevious: "Previous",
          over: "over",
          taken: "Taken",
          topbar: {
            filters: "Filters",
            next: "next",
            prev: "prev",
            today: "Today",
            view: "View"
          },
          search: "search",
          week: "week"
        },
        translateCode: "en-EN",
        dayjsTranslations: enDayjsTranslations
      }
    ];
    
    <Scheduler
      // ...
      config={{
        lang: "en",
        translations: langs
      }}
    />;
  10. Scheduler Component Props

    master

    The Scheduler component accepts the following props:

    Property NameTypeDescription
    isLoadingbooleanShows loading indicators on the scheduler
    onRangeChangefunctionRuns whenever the user reaches the end of the currently rendered canvas (provides updated startDate and endDate)
    onTileClickfunctionCallback triggered when a resource tile is clicked (provides clicked resource data)
    onItemClickfunctionCallback triggered when a left column item is clicked (provides clicked item data)
    onFilterDatafunctionCallback firing when the filter button is clicked
    onClearFilterDatafunctionCallback firing when the clear filters button is clicked (visible only when filterButtonState > 0)
    configConfigObject containing scheduler configuration properties
  11. Define SchedulerData structure

    master

    The SchedulerData is an array of chart rows. Each row follows this shape:

    Property NameTypeDescription
    idstringUnique row ID
    labelSchedulerRowLabelRow's label (e.g., person's name, icon, etc.)
    dataArray<ResourceItem>Array of resources (tiles) for this row

    Each ResourceItem in the data array represents a tile on the grid:

    Property NameTypeDescription
    idstringUnique resource ID
    titlestringTitle displayed on the resource tile
    subtitlestring (optional)Subtitle displayed on the resource tile
    descriptionstring (optional)Description displayed on the resource tile
    startDateDateStart date for calculating position
    endDateDateEnd date for calculating position
    occupancynumberNumber of seconds the resource takes up (shown in tooltip)
    bgColorstring (optional)Tile color
  12. Apply GlobalStyle for CSS resets

    master

    The GlobalStyle component is a createGlobalStyle instance that applies essential CSS resets and typography specifically to the element with the ID defined by prefixId (reactSchedulerOutsideWrapper). This ensures that box-sizing, font-family, and line-height are correctly scoped to the scheduler's container and its children.

    import { GlobalStyle } from '@bitnoi.se/react-scheduler';
    
    function App() {
      return (
        <>
          <GlobalStyle />
          <div id="reactSchedulerOutsideWrapper">
            {/* Scheduler content */}
          </div>
        </>
      );
    }