Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(calendar): add custom cell content to Calendar #3554

Open
wants to merge 8 commits into
base: canary
Choose a base branch
from

Conversation

1amageek
Copy link

@1amageek 1amageek commented Jul 25, 2024

📝 Description

This PR adds the ability to render custom content in calendar cells. It introduces a new renderCellContent prop that allows developers to specify custom content for each calendar cell.

⛳️ Current behavior (updates)

Currently, the calendar cells render only the default formatted date content, and there is no way to customize the content within each cell.

🚀 New behavior

With this PR, developers can pass a renderCellContent function as a prop to the Calendar component. This function receives the date of the cell and allows custom content to be rendered within the cell. If renderCellContent is not provided, the cell will fall back to rendering the default formatted date.

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

This update enhances the flexibility and customizability of the calendar component, allowing for a wide range of use cases where specific content needs to be displayed within calendar cells.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced renderCellContent prop in both Calendar and RangeCalendar components, allowing for customized rendering of calendar cell content.
  • Documentation

    • Updated stories to showcase the new renderCellContent feature with different configurations.

Copy link

changeset-bot bot commented Jul 25, 2024

🦋 Changeset detected

Latest commit: 448ab55

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@nextui-org/calendar Minor
@nextui-org/date-picker Patch
@nextui-org/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jul 25, 2024

@1amageek is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jul 25, 2024

Walkthrough

The recent updates enhance the Calendar and RangeCalendar components by introducing a new renderCellContent prop, allowing developers to customize how each calendar cell displays content. This feature significantly improves flexibility in presenting calendar data, as well as enhances the overall user experience. Accompanying these changes are thorough updates to tests and stories, ensuring the new functionality is well-integrated and validated.

Changes

File(s) Change Summary
.changeset/popular-seals-appear.md Added renderCellContent prop to Calendar and RangeCalendar, enhancing cell content customization.
packages/components/calendar/__tests__/calendar.test.tsx, packages/components/calendar/__tests__/range-calendar.test.tsx Introduced new test suites to validate custom cell content rendering in Calendar and RangeCalendar.
packages/components/calendar/src/calendar-base.tsx, packages/components/calendar/src/calendar-cell.tsx, packages/components/calendar/src/calendar-month.tsx, packages/components/calendar/src/calendar.tsx, packages/components/calendar/src/range-calendar.tsx, packages/components/calendar/src/use-calendar.ts, packages/components/calendar/src/use-range-calendar.ts Added renderCellContent prop in respective interfaces, facilitating custom rendering of calendar cells.
packages/components/calendar/stories/calendar.stories.tsx, packages/components/calendar/stories/range-calendar.stories.tsx Created new templates for Calendar and RangeCalendar showcasing customizable cell content and layouts.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@1amageek
Copy link
Author

#3313 was closed and the PR was remade.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6d9995b and 448ab55.

Files selected for processing (12)
  • .changeset/popular-seals-appear.md (1 hunks)
  • packages/components/calendar/tests/calendar.test.tsx (1 hunks)
  • packages/components/calendar/tests/range-calendar.test.tsx (1 hunks)
  • packages/components/calendar/src/calendar-base.tsx (4 hunks)
  • packages/components/calendar/src/calendar-cell.tsx (5 hunks)
  • packages/components/calendar/src/calendar-month.tsx (2 hunks)
  • packages/components/calendar/src/calendar.tsx (2 hunks)
  • packages/components/calendar/src/range-calendar.tsx (2 hunks)
  • packages/components/calendar/src/use-calendar.ts (3 hunks)
  • packages/components/calendar/src/use-range-calendar.ts (3 hunks)
  • packages/components/calendar/stories/calendar.stories.tsx (2 hunks)
  • packages/components/calendar/stories/range-calendar.stories.tsx (2 hunks)
Additional comments not posted (28)
.changeset/popular-seals-appear.md (1)

1-5: LGTM!

The changeset correctly summarizes the new feature and updates made in the PR.

packages/components/calendar/src/calendar.tsx (2)

1-1: Import statement update looks good.

The import statement now includes CalendarDate, which is necessary for the new renderCellContent prop.


10-12: Props interface update looks good.

The Props interface now includes the optional renderCellContent prop, allowing for custom cell content rendering.

packages/components/calendar/src/range-calendar.tsx (2)

1-1: Import statement update looks good.

The import statement now includes CalendarDate, which is necessary for the new renderCellContent prop.


17-19: Props interface update looks good.

The Props interface now includes the optional renderCellContent prop, allowing for custom cell content rendering.

packages/components/calendar/src/use-range-calendar.ts (3)

20-22: LGTM!

The addition of the renderCellContent property to the Props interface is correct and aligns with the PR objectives.


27-28: LGTM!

The updated function signature for useRangeCalendar correctly integrates the renderCellContent property.


92-92: LGTM!

The renderCellContent property is correctly passed to the getBaseCalendarProps function, ensuring it is used within the calendar component.

packages/components/calendar/src/calendar-month.tsx (3)

17-17: LGTM!

The addition of the renderCellContent property to the CalendarMonthProps interface is correct and aligns with the PR objectives.


21-21: LGTM!

The updated function signature for CalendarMonth correctly integrates the renderCellContent property.


58-58: LGTM!

The renderCellContent property is correctly passed to the CalendarCell component, ensuring it is used within the calendar cells.

packages/components/calendar/src/use-calendar.ts (3)

21-21: LGTM!

The addition of the renderCellContent property to the Props interface is correct and aligns with the PR objectives.


29-29: LGTM!

The updated function signature for useCalendar correctly integrates the renderCellContent property.


100-100: LGTM!

The renderCellContent property is correctly passed to the getBaseCalendarProps function, ensuring it is used within the calendar component.

packages/components/calendar/src/calendar-cell.tsx (5)

20-20: Prop addition approved.

The renderCellContent prop is a valuable addition, enhancing the flexibility of the CalendarCell component.


24-31: Destructuring approved.

The destructuring of originalProps to include renderCellContent is correctly implemented.


47-48: Usage of otherProps approved.

Forwarding otherProps to useCalendarCell ensures that all remaining props are handled correctly.


54-72: Usage of otherProps.date approved.

Using otherProps.date maintains consistency with the destructured props and ensures correct determination of cell states.


101-101: Rendering logic approved.

The rendering logic correctly uses renderCellContent if provided, and falls back to the default formatted date otherwise.

packages/components/calendar/src/calendar-base.tsx (3)

36-36: Prop addition approved.

The renderCellContent prop is a valuable addition, enhancing the flexibility of the CalendarBase component.


52-52: Destructuring approved.

The destructuring of props to include renderCellContent is correctly implemented.


104-104: Usage of renderCellContent approved.

Passing the renderCellContent prop to CalendarMonth ensures that custom cell content rendering is propagated correctly.

packages/components/calendar/stories/calendar.stories.tsx (2)

260-287: Template function approved.

The CustomCellTemplate function enhances the flexibility of the calendar display by allowing for distinct styling and width settings.


408-419: New export approved.

The CustomCellContent export expands the functionality of the calendar component by providing more options for rendering and styling.

packages/components/calendar/stories/range-calendar.stories.tsx (2)

291-318: Verify consistency and maintainability of styles and class names.

Ensure that the inline styles and class names used in the RangeCalendar components are consistent with the rest of the application and maintainable in the long term.


441-452: LGTM! Verify the correctness of the renderCellContent function.

The code changes are approved.

However, ensure that the renderCellContent function correctly formats the date and integrates well with the rest of the application.

packages/components/calendar/__tests__/calendar.test.tsx (1)

467-489: LGTM! The test logic is clear and effective.

The test suite correctly verifies that the Calendar component can render custom content in its cells.

packages/components/calendar/__tests__/range-calendar.test.tsx (1)

752-777: LGTM! The test logic is clear and effective.

The test suite correctly verifies that the RangeCalendar component can render custom content in its cells.

@wingkwong wingkwong added this to the v2.5.0 milestone Jul 25, 2024
@wingkwong wingkwong self-assigned this Aug 4, 2024
Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Sorry for the late review. Overall it looks good and it could be much cleaner. Here's my comments.

  1. in use-calendar.ts, please include renderCellContent in context so that you can retrieve from it instead of passing it from different places.
  2. please add renderCellContent in use-calendar-base.ts.
  3. since you've introduced a new prop, please also update the documentation.
  4. I think we may also show the dot when a cell is selected to make it look better
    image

If you have any questions, free feel to ping me at discord. My ID is same as my github one.

@@ -464,4 +464,27 @@ describe("Calendar", () => {
expect(year).toHaveAttribute("data-value", "2567");
});
});
describe("Custom cell content", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a new line above

@@ -18,13 +18,15 @@ interface Props extends UseCalendarBaseProps {
* Props for the button picker, which is used to select the month, year and expand the header.
*/
buttonPickerProps?: ButtonProps;
renderCellContent?: (date: CalendarDate) => React.ReactNode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line and retrieve renderCellContent from useCalendarBase

}

export type UseCalendarProps<T extends DateValue> = Props & AriaCalendarProps<T>;

export function useCalendar<T extends DateValue>({
buttonPickerProps: buttonPickerPropsProp,
className,
renderCellContent,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line and retrieve renderCellContent from useCalendarBase

@@ -14,10 +14,11 @@ export interface CalendarMonthProps extends HTMLNextUIProps<"table">, CalendarPr
startDate: CalendarDate;
currentMonth: number;
direction: number;
renderCellContent?: (date: CalendarDate) => React.ReactNode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line. see comment in L58

}

export function CalendarMonth(props: CalendarMonthProps) {
const {startDate, direction, currentMonth} = props;
const {startDate, direction, currentMonth, renderCellContent} = props;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo this line - see comment in L58

@@ -98,6 +101,7 @@ export function CalendarBase(props: CalendarBaseProps) {
key={`calendar-month-${i}`}
currentMonth={currentMonth.month}
direction={direction}
renderCellContent={renderCellContent}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can directly retrieve renderCellContent from context in calendar-cell.tsx. therefore, there is no need to pass it all the way from base.

@@ -3,6 +3,7 @@ import type {As, HTMLNextUIProps} from "@nextui-org/system";
import type {ButtonProps} from "@nextui-org/button";
import type {HTMLAttributes, ReactNode, RefObject} from "react";

import {CalendarDate} from "@internationalized/date";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line. see comment in L104.

@@ -54,6 +55,7 @@ export function CalendarMonth(props: CalendarMonthProps) {
currentMonth={startDate}
date={date}
isPickerVisible={isHeaderExpanded}
renderCellContent={renderCellContent}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can directly retrieve renderCellContent from context in calendar-cell.tsx. therefore, there is no need to pass it from here.

Comment on lines +10 to +12
interface Props<T extends DateValue> extends Omit<UseCalendarProps<T>, "isHeaderWrapperExpanded"> {
renderCellContent?: (date: CalendarDate) => React.ReactNode;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo the changes in this file

@@ -95,6 +97,7 @@ export function useCalendar<T extends DateValue>({
errorMessageProps: getErrorMessageProps(errorMessageProps),
className: slots.base({class: baseStyles}),
errorMessage,
renderCellContent,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line - no need to pass to base. we retrieve from context instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants