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

fix: incorrect year in showMonthAndYearPickers with locale #3331

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

Conversation

wingkwong
Copy link
Member

@wingkwong wingkwong commented Jun 26, 2024

📝 Description

Currently we are using gregorian calendar with possible years in $[1900, 2099]$. However, some locales such as th-TH-u-ca-buddhist using different calendar making the years out of bound. Hence, add the corresponding offset to make sure the year is within the bound.

⛳️ Current behavior (updates)

locale: th-TH-u-ca-buddhist

image

🚀 New behavior

locale: th-TH-u-ca-buddhist

image

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

📝 Additional Information

Summary by CodeRabbit

  • New Features

    • Introduced CalendarWithLocale and DatePickerWithLocale components for enhanced localization.
  • Enhancements

    • Improved date handling with createCalendar, DateFormatter, and getGregorianYearOffset for better calendar management.
    • Updated minValue and maxValue calculations based on calendar identifiers.
  • Refactor

    • Adjusted NextUIProvider with new initialization for default dates.
  • Chores

    • Enhanced utility functions in shared utilities for better date management.

Copy link

vercel bot commented Jun 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 26, 2024 1:56pm
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 26, 2024 1:56pm

Copy link

changeset-bot bot commented Jun 26, 2024

🦋 Changeset detected

Latest commit: 5c111b9

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

This PR includes changesets to release 49 packages
Name Type
@nextui-org/calendar Patch
@nextui-org/date-input Patch
@nextui-org/system Patch
@nextui-org/shared-utils Patch
@nextui-org/date-picker Patch
@nextui-org/react Patch
@nextui-org/accordion Patch
@nextui-org/autocomplete Patch
@nextui-org/avatar Patch
@nextui-org/badge Patch
@nextui-org/breadcrumbs Patch
@nextui-org/button Patch
@nextui-org/card Patch
@nextui-org/checkbox Patch
@nextui-org/chip Patch
@nextui-org/dropdown Patch
@nextui-org/image Patch
@nextui-org/input Patch
@nextui-org/link Patch
@nextui-org/listbox Patch
@nextui-org/menu Patch
@nextui-org/modal Patch
@nextui-org/navbar Patch
@nextui-org/pagination Patch
@nextui-org/popover Patch
@nextui-org/progress Patch
@nextui-org/radio Patch
@nextui-org/ripple Patch
@nextui-org/scroll-shadow Patch
@nextui-org/select Patch
@nextui-org/skeleton Patch
@nextui-org/slider Patch
@nextui-org/snippet Patch
@nextui-org/switch Patch
@nextui-org/table Patch
@nextui-org/tabs Patch
@nextui-org/tooltip Patch
@nextui-org/user Patch
@nextui-org/aria-utils Patch
@nextui-org/framer-utils Patch
@nextui-org/code Patch
@nextui-org/divider Patch
@nextui-org/kbd Patch
@nextui-org/spacer Patch
@nextui-org/spinner Patch
@nextui-org/use-data-scroll-overflow Patch
@nextui-org/use-pagination Patch
@nextui-org/react-utils Patch
@nextui-org/use-real-shape 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
Contributor

coderabbitai bot commented Jun 26, 2024

Walkthrough

The changes introduced localization enhancements across various components (Calendar, DateInput, DatePicker) by integrating internationalization utilities and adapting component behaviors based on locale data. The primary focus was to solve issues related to incorrect calendar displays and year handling, ensuring the components respect locale-specific settings consistently.

Changes

Files Change Summary
.../calendar/__tests__/calendar.test.tsx, .../date-picker/__tests__/date-picker.test.tsx Added WithLocale components that handle localization by wrapping base components in NextUIProvider.
.../use-calendar-base.ts, .../use-date-input.ts Enhanced with imports from @internationalized/date and @nextui-org/shared-utils, adjusted minValue and maxValue based on calendar type and year offsets, and updated createCalendar function calls.
.../provider.tsx Removed unused CalendarDate import and adjusted defaultDates initialization to streamline the NextUIProvider.
.../shared-utils/src/dates.ts, .../shared-utils/src/index.ts Enhanced getGregorianYearOffset for mapping calendar identifiers to year offsets and exported from the index file.

Sequence Diagram(s)

N/A

Assessment against linked issues

Objective Addressed Explanation
Accurate display of Buddhist Year when selecting year in Date Picker (#3272).
Persistent Hijri calendar system in Calendar component (#3063).
Correct localization for Persian calendar in Date Picker (#3254, #2842).

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 Configration 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.

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: 1

Outside diff range and nitpick comments (1)
packages/components/calendar/__tests__/calendar.test.tsx (1)

Line range hint 20-455: Comprehensive Tests for Calendar With Locale

The tests for CalendarWithLocale are comprehensive and appear to cover the necessary scenarios to ensure the calendar behaves correctly across different locales, especially with the Buddhist calendar.

It's important to continue expanding these tests to cover all supported locales and calendar systems to ensure comprehensive coverage.

Would you like assistance in expanding these tests to cover more scenarios or locales?

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 720f2f5 and 5c111b9.

Files selected for processing (8)
  • .changeset/purple-singers-knock.md (1 hunks)
  • packages/components/calendar/tests/calendar.test.tsx (3 hunks)
  • packages/components/calendar/src/use-calendar-base.ts (4 hunks)
  • packages/components/date-input/src/use-date-input.ts (4 hunks)
  • packages/components/date-picker/tests/date-picker.test.tsx (3 hunks)
  • packages/core/system/src/provider.tsx (2 hunks)
  • packages/utilities/shared-utils/src/dates.ts (1 hunks)
  • packages/utilities/shared-utils/src/index.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • packages/core/system/src/provider.tsx
  • packages/utilities/shared-utils/src/index.ts
Additional context used
Biome
packages/utilities/shared-utils/src/dates.ts

[error] 20-20: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)


[error] 21-21: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)


[error] 22-22: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)

Additional comments not posted (5)
.changeset/purple-singers-knock.md (1)

1-8: Changeset Description is Clear and Relevant

The changeset description accurately reflects the updates made in the PR, specifically addressing the locale-related issues in the showMonthAndYearPickers component. This ensures proper version tracking and clarity for future maintenance.

packages/components/date-input/src/use-date-input.ts (1)

Line range hint 120-148: Enhanced Locale Handling in Date Input

The modifications to handle locale-specific year offsets are well implemented. It ensures that the date input respects the calendar system of the specified locale, particularly addressing issues with out-of-bound years.

However, ensure that these changes are thoroughly tested across different locales to verify their effectiveness.

packages/components/calendar/src/use-calendar-base.ts (1)

Line range hint 188-214: Locale-Specific Adjustments in Calendar Base

The implementation of locale-specific year offsets in the calendar base utility is well executed. This should correctly handle the year discrepancies caused by different calendar systems.

As with the date input, testing across multiple locales is crucial to ensure the robustness of these changes.

Verification successful

Locale-Specific Adjustments in Calendar Base Verified

The initialization of minValue and maxValue correctly includes the gregorianYearOffset, ensuring that the years are within bounds for different locales. The implementation should handle year discrepancies caused by different calendar systems as intended.

  • use-calendar-base.ts: minValue and maxValue include gregorianYearOffset.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify calendar base adjustments across different locales.

# Test: Check for proper functionality across a variety of locales.
rg --type typescript $'useCalendarBase'

Length of output: 73



Script:

#!/bin/bash
# Description: Verify calendar base adjustments across different locales.

# Test: Check for proper functionality across a variety of locales.
# Correcting the file type to 'ts' for TypeScript files.
rg --type ts 'useCalendarBase'

Length of output: 769



Script:

#!/bin/bash
# Description: Extract usage of useCalendarBase to verify locale-specific adjustments.

# Extract relevant code from use-calendar.ts
rg -A 10 -B 10 'useCalendarBase' packages/components/calendar/src/use-calendar.ts

# Extract relevant code from use-range-calendar.ts
rg -A 10 -B 10 'useCalendarBase' packages/components/calendar/src/use-range-calendar.ts

Length of output: 2809



Script:

#!/bin/bash
# Description: Verify if minValue and maxValue include locale-specific year offsets.

# Search for minValue and maxValue initialization in use-calendar.ts
rg 'minValue|maxValue' packages/components/calendar/src/use-calendar.ts

# Search for minValue and maxValue initialization in use-range-calendar.ts
rg 'minValue|maxValue' packages/components/calendar/src/use-range-calendar.ts

Length of output: 265



Script:

#!/bin/bash
# Description: Extract full initialization context of minValue and maxValue to verify locale-specific year offsets.

# Extract full initialization context of minValue and maxValue in use-calendar.ts
rg -A 5 -B 5 'minValue|minValue' packages/components/calendar/src/use-calendar.ts

# Extract full initialization context of minValue and maxValue in use-range-calendar.ts
rg -A 5 -B 5 'minValue|maxValue' packages/components/calendar/src/use-range-calendar.ts

Length of output: 1164



Script:

#!/bin/bash
# Description: Extract initialization of minValue and maxValue to verify locale-specific year offsets.

# Extract initialization of minValue and maxValue in use-calendar-base.ts
rg -A 5 -B 5 'minValue|maxValue' packages/components/calendar/src/use-calendar-base.ts

Length of output: 863

packages/components/date-picker/__tests__/date-picker.test.tsx (2)

28-46: New Component DatePickerWithLocale Added

The addition of DatePickerWithLocale is a good approach to handle localization within the DatePicker component. Wrapping DatePickerBase in NextUIProvider with a locale prop ensures that locale-specific settings are applied. This is crucial for maintaining consistency across different locales, especially when dealing with calendar systems that differ significantly from the Gregorian calendar.

However, it's essential to ensure that this new component is fully covered by tests, especially since it's designed to handle locale-specific logic which can be prone to edge cases.


651-677: Test Case for DatePickerWithLocale with Thai Buddhist Calendar

This test case ensures that the DatePickerWithLocale component correctly handles the Thai Buddhist calendar. The test renders the component with a specific locale (th-TH-u-ca-buddhist) and checks if the displayed month and year are correct according to the Buddhist calendar.

It's good to see that the test verifies both the name and the data-value attributes of the month and year, which is crucial for ensuring that the UI elements are not only displaying correctly but are also functionally correct.

Given the complexity of handling multiple calendar systems, this test is essential for verifying that the DatePicker behaves as expected across different locales. It might be beneficial to add similar tests for other supported calendar systems mentioned in the PR to ensure comprehensive coverage.

[APROVED]

Comment on lines +20 to +24
case "roc":
case "japanese":
case "gregory":
default:
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove Redundant Case Clauses

The cases for 'roc', 'japanese', and 'gregory' are redundant since they fall into the default case which returns 0. Removing these will simplify the switch statement without affecting functionality.

-    case "roc":
-    case "japanese":
-    case "gregory":
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
case "roc":
case "japanese":
case "gregory":
default:
return 0;
default:
return 0;
Tools
Biome

[error] 20-20: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)


[error] 21-21: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)


[error] 22-22: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)

@wingkwong wingkwong added this to the v2.4.3 milestone Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants