Skip to content
Source
Sign inSign up
TanStack/time

[RFC] feat(react-time): useCalendar hook #7

Closedbart-krakowski(GitHub profile, opens in a new tab)wants to merge 61 commits intov0 fromrefs/pull/7/headopened

RFC: useCalendar hook

Summary

This RFC proposes the creation of a custom calendar hook, useCalendar, designed to manage calendar functionalities including event handling, view mode toggling, and period navigation.

Motivation

The motivation behind this RFC is to provide a reusable and efficient calendar hook that can be integrated into various components, ensuring consistent and simplified management of calendar functionalities. This hook aims to support different view modes, handle multi-day events, and provide utility functions for navigating and manipulating calendar periods.

TODO:

  • Drag and drop events
  • Resize events

Example Usage

const CalendarComponent = ({ events }) => {
  const {
    firstDayOfPeriod,
    currentPeriod,
    goToPreviousPeriod,
    goToNextPeriod,
    goToCurrentPeriod,
    goToSpecificPeriod,
    changeViewMode,
    days,
    daysNames,
    viewMode,
    getEventProps,
    currentTimeMarkerProps,
    groupDaysBy,
  } = useCalendar({
    weekStartsOn: 1,
    viewMode: { value: 1, unit: 'month' },
    locale: 'en-US',
    onChangeViewMode: (newViewMode) => console.log('View mode changed:', newViewMode),
  });

  return (
    <div className="calendar-container">
      <div className="calendar-header">
        <button onClick={goToPreviousPeriod}>Previous</button>
        <button onClick={goToCurrentPeriod}>Today</button>
        <button onClick={goToNextPeriod}>Next</button>
      </div>
      <div className="calendar-view-mode">
        <button onClick={() => changeViewMode({ value: 1, unit: 'month' })}>Month View</button>
        <button onClick={() => changeViewMode({ value: 1, unit: 'week' })}>Week View</button>
        <button onClick={() => changeViewMode({ value: 3, unit: 'day' })}>3-Day View</button>
        <button onClick={() => changeViewMode({ value: 1, unit: 'day' })}>1-Day View</button>
      </div>
      <table className="calendar-table">
        {viewMode.unit === 'month' && (
          groupDaysBy(days, 'months').map((month, monthIndex) => (
            <tbody key={monthIndex} className="calendar-month">
              <tr>
                <th colSpan={7} className="calendar-month-name">
                  {month[0]?.date.toLocaleString('default', { month: 'long' })}{' '}
                  {month[0]?.date.year}
                </th>
              </tr>
              <tr>
                {daysNames.map((dayName, index) => (
                  <th key={index} className="calendar-day-name">
                    {dayName}
                  </th>
                ))}
              </tr>
              {groupDaysBy(month, 'weeks').map((week, weekIndex) => (
                <tr key={weekIndex} className="calendar-week">
                  {week.map((day) => (
                    <td
                      key={day.date.toString()}
                      className={`calendar-day ${day.isToday ? 'today' : ''
                        } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                    >
                      <div className="calendar-date">{day.date.day}</div>
                      <div className="calendar-events">
                        {day.events.map((event) => (
                          <div
                            key={event.id}
                            className="calendar-event"
                            style={getEventProps(event.id)?.style}
                          >
                            {event.title}
                          </div>
                        ))}
                      </div>
                    </td>
                  ))}
                </tr>
              ))}
            </tbody>
          ))
        )}

        {viewMode.unit === 'week' && (
          <tbody className="calendar-week">
            <tr>
              {daysNames.map((dayName, index) => (
                <th key={index} className="calendar-day-name">
                  {dayName}
                </th>
              ))}
            </tr>
            {groupDaysBy(days, 'weeks').map((week, weekIndex) => (
              <tr key={weekIndex}>
                {week.map((day) => (
                  <td
                    key={day.date.toString()}
                    className={`calendar-day ${day.isToday ? 'today' : ''
                      } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                  >
                    <div className="calendar-date">{day.date.day}</div>
                    <div className="calendar-events">
                      {day.events.map((event) => (
                        <div
                          key={event.id}
                          className="calendar-event"
                          style={getEventProps(event.id)?.style}
                        >
                          {event.title}
                        </div>
                      ))}
                    </div>
                  </td>
                ))}
              </tr>
            ))}
          </tbody>
        )}

        {viewMode.unit === 'day' && (
          <tbody className="calendar-day">
            <tr>
              {daysNames.map((dayName, index) => (
                <th key={index} className="calendar-day-name">
                  {dayName}
                </th>
              ))}
            </tr>
            <tr>
              {days.map((day) => (
                <td
                  key={day.date.toString()}
                  className={`calendar-day ${day.isToday ? 'today' : ''
                    } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                >
                  <div className="calendar-date">{day.date.day}</div>
                  <div className="calendar-events">
                    {day.events.map((event) => (
                      <div
                        key={event.id}
                        className="calendar-event"
                        style={getEventProps(event.id)?.style}
                      >
                        {event.title}
                      </div>
                    ))}
                  </div>
                </td>
              ))}
            </tr>
          </tbody>
        )}
      </table>
    </div>
  );
};
    1. Wow, you've been busy. I still have to look at your latest commits here, but wanted to throw out an observation.

      You had a lot of custom logic (in my initial look) in your hook that would be better served as utility methods that could be reused across multiple platform interfaces. Let's consider moving core bits down (like I was doing with the formatter work), and have the React interface work as a consumer utility of the core, handling things like memoization and the like. I'm not overly familiar with what code will look like in other interfaces, but I imagine that they would all ultimately be trying to generate the same types of data and general state management.

    2. Wow, you've been busy. I still have to look at your latest commits here, but wanted to throw out an observation.

      You had a lot of custom logic (in my initial look) in your hook that would be better served as utility methods that could be reused across multiple platform interfaces. Let's consider moving core bits down (like I was doing with the formatter work), and have the React interface work as a consumer utility of the core, handling things like memoization and the like. I'm not overly familiar with what code will look like in other interfaces, but I imagine that they would all ultimately be trying to generate the same types of data and general state management.

      Of course. If the general idea and API are good I'd be happy to move the logic to the core package.

Sign in to join this conversation.