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(datepicker): datePicker now supports formatting of date #1314

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

dhavalveera
Copy link
Contributor

@dhavalveera dhavalveera commented Mar 25, 2024

🛠️ Enhancement: Added Date Formatting Support to DatePicker

This PR introduces date formatting support to the DatePicker component by leveraging the date-fns package. Users can now customize date formats such as dd-MMM-yyyy or dd MMM yyyy according to their preferences.

outputs/examples:

dd-MMM-yyyy // output => 11-Mar-2024

image

dd MMM yyyy // output => 11 Mar 2024

image

dd-MM-yyyyy // output => 11-03-2024

image

Summary by CodeRabbit

  • New Features

    • Introduced a Datepicker component with configurable date formats and localization support.
    • Added documentation for date format and localization settings in the Datepicker component.
  • Improvements

    • Enhanced Datepicker tests to support various date formats.
    • Updated Datepicker to use consistent date formatting and locale settings through date-fns.
  • Dependencies

    • Added date-fns version 3.6.0 to support date formatting and localization.

Copy link

stackblitz bot commented Mar 25, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

vercel bot commented Mar 25, 2024

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

Name Status Preview Comments Updated (UTC)
flowbite-react ❌ Failed (Inspect) Jun 26, 2024 1:56am
flowbite-react-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 26, 2024 1:56am

Copy link
Contributor

coderabbitai bot commented Mar 25, 2024

Walkthrough

The recent changes enhance the Datepicker component by adding localization and customizable date formats, leveraging date-fns. This improves the component's usability across different locales and allows users to specify the desired date format. In essence, the updates focus on internationalization and formatting flexibility, enhancing both functionality and user experience.

Changes

Files / Groups Change Summary
apps/web/examples/datepicker/datepicker.format.tsx Introduced a Datepicker component with specific input format support, for both client and server-side rendering.
apps/web/examples/datepicker/datepicker.localization.tsx Updated to support localization with language attribute and label names for the Datepicker component.
apps/web/content/docs/components/datepicker.mdx Added documentation on using inputFormat prop and localization, emphasizing date formatting and date-fns usage.
apps/web/examples/datepicker/index.ts Added export of format from datepicker.format.
packages/ui/package.json Added date-fns dependency version 3.6.0.
packages/ui/src/.../Datepicker.spec.tsx, Datepicker.stories.tsx Updated test cases and stories to include date format and localization.
packages/ui/src/components/Datepicker/Datepicker.tsx Added AvailableLocales type, inputFormat, and localization support to the Datepicker component.
packages/ui/src/components/Datepicker/helpers.ts Enhanced getFormattedDate to use date-fns for date formatting and localization, added AvailableLocales type.
packages/ui/src/components/Datepicker/helpers.spec.tsx Updated tests for getFormattedDate with date format string support, ensuring diverse locale formatting.
packages/ui/src/components/Datepicker/DatepickerContext.tsx Changed language property to AvailableLocales type in DatepickerContextProps.

Poem

In the code so bright and true,
Datepicker gained a skill anew,
With formats diverse and locales wide,
Days are shown with global pride.
Thanks to date-fns we align,
Dates sparkle like a user's sign.
🐰✨


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.

packages/ui/package.json Outdated Show resolved Hide resolved
packages/ui/package.json Outdated Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3d546f7 and fb35e8c.
Files ignored due to path filters (1)
  • packages/ui/package.json is excluded by: !**/*.json
Files selected for processing (7)
  • apps/web/content/docs/components/datepicker.mdx (1 hunks)
  • apps/web/examples/datepicker/datepicker.format.tsx (1 hunks)
  • apps/web/examples/datepicker/index.ts (1 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.spec.tsx (5 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.stories.tsx (1 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.tsx (4 hunks)
  • packages/ui/src/components/Datepicker/helpers.ts (3 hunks)
Additional comments: 5
apps/web/examples/datepicker/index.ts (1)
  • 2-2: The addition of the format export from datepicker.format is correctly implemented.
apps/web/examples/datepicker/datepicker.format.tsx (1)
  • 1-42: The new example file datepicker.format.tsx correctly demonstrates the usage of the inputFormat prop with the Datepicker component. The structure and export of the example are consistent with the project's standards.
packages/ui/src/components/Datepicker/Datepicker.stories.tsx (1)
  • 64-77: The addition of the FormattedDate story in Datepicker.stories.tsx correctly showcases the new inputFormat prop with a sample format. This enhances the documentation and provides a practical example of the new feature.
apps/web/content/docs/components/datepicker.mdx (1)
  • 22-29: The addition of the documentation section for setting the date format using the inputFormat prop is clear and informative. The note on format conventions is particularly helpful for guiding users in correctly specifying date formats.
packages/ui/src/components/Datepicker/Datepicker.tsx (1)
  • 100-100: The addition of the inputFormat property to the DatepickerProps interface and its usage in DatepickerRender are correctly implemented. The default value and its application in date formatting calls enhance the component's flexibility and align with the PR objectives.

Also applies to: 121-121, 206-206, 209-209, 277-277

packages/ui/src/components/Datepicker/Datepicker.spec.tsx Outdated Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fb35e8c and 97c0d72.
Files selected for processing (1)
  • packages/ui/src/components/Datepicker/helpers.spec.tsx (1 hunks)

packages/ui/src/components/Datepicker/helpers.spec.tsx Outdated Show resolved Hide resolved
packages/ui/src/components/Datepicker/helpers.spec.tsx Outdated Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 97c0d72 and 876771a.
Files ignored due to path filters (1)
  • packages/ui/package.json is excluded by: !**/*.json
Files selected for processing (5)
  • apps/web/examples/datepicker/datepicker.format.tsx (1 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.spec.tsx (5 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.tsx (3 hunks)
  • packages/ui/src/components/Datepicker/helpers.spec.tsx (1 hunks)
  • packages/ui/src/components/Datepicker/helpers.ts (3 hunks)
Files skipped from review as they are similar to previous changes (4)
  • apps/web/examples/datepicker/datepicker.format.tsx
  • packages/ui/src/components/Datepicker/Datepicker.spec.tsx
  • packages/ui/src/components/Datepicker/Datepicker.tsx
  • packages/ui/src/components/Datepicker/helpers.spec.tsx
Additional comments: 1
packages/ui/src/components/Datepicker/helpers.ts (1)
  • 1-1: The import of DateFNSFormat from 'date-fns' is correctly added to support date formatting functionality.

packages/ui/src/components/Datepicker/helpers.ts Outdated Show resolved Hide resolved
@SutuSebastian
Copy link
Collaborator

btw, what code editor are u using @dhavalveera ? It seems to me that u are not formatting the code on-save, or at all.

@dhavalveera
Copy link
Contributor Author

btw, what code editor are u using @dhavalveera ? It seems to me that u are not formatting the code on-save, or at all.

I'm using VSCode, and when I do save (Ctrl + S) it saves the files for me, but I'll check again

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 876771a and 3a690dd.
Files selected for processing (9)
  • apps/web/content/docs/components/datepicker.mdx (1 hunks)
  • apps/web/examples/datepicker/datepicker.format.tsx (1 hunks)
  • apps/web/examples/datepicker/datepicker.localization.tsx (1 hunks)
  • apps/web/examples/datepicker/index.ts (1 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.spec.tsx (5 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.stories.tsx (1 hunks)
  • packages/ui/src/components/Datepicker/Datepicker.tsx (4 hunks)
  • packages/ui/src/components/Datepicker/helpers.spec.tsx (1 hunks)
  • packages/ui/src/components/Datepicker/helpers.ts (3 hunks)
Files skipped from review as they are similar to previous changes (5)
  • apps/web/examples/datepicker/datepicker.format.tsx
  • apps/web/examples/datepicker/index.ts
  • packages/ui/src/components/Datepicker/Datepicker.spec.tsx
  • packages/ui/src/components/Datepicker/Datepicker.stories.tsx
  • packages/ui/src/components/Datepicker/Datepicker.tsx
Additional comments: 11
apps/web/examples/datepicker/datepicker.localization.tsx (3)
  • 10-10: The update to the language attribute and label attributes for the Datepicker component enhances consistency and ensures compatibility with the date-fns package.
  • 18-18: The consistency in updating the language attribute and label attributes across different parts of the file ensures uniformity in the component's usage.
  • 23-23: The consistency in updating the language attribute and label attributes across different parts of the file ensures uniformity in the component's usage.
apps/web/content/docs/components/datepicker.mdx (2)
  • 22-30: The addition of a section for setting the date format using the inputFormat prop, along with notes on date format conventions and the reliance on date-fns, is crucial for informing users about the new formatting capabilities of the Datepicker component.
  • 38-38: The note on the reliance of the Datepicker component on date-fns for localization, and guiding users to refer to available localizations from date-fns, is important for correctly utilizing the localization feature.
packages/ui/src/components/Datepicker/helpers.ts (3)
  • 1-2: The addition of imports for DateFNSFormat and DateFNSLocale is necessary for enabling the new date formatting feature, aligning with the objective of providing customizable date formatting capabilities.
  • 4-5: Defining a type for available locales enhances type safety and clarity in the code, supporting the new date formatting feature by specifying the types of locales that can be used.
  • 105-116: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [108-127]

The modification of the getFormattedDate function to include a dateFormat parameter and utilize DateFNSFormat for date formatting aligns with best practices for localization support. The approach to determining the locale ensures that the correct locale object is used for formatting.

packages/ui/src/components/Datepicker/helpers.spec.tsx (3)
  • 157-157: The update to include an additional argument for the date format string in the getFormattedDate function call is necessary for testing the new formatting capabilities. The specified format matches the expected output, ensuring the test correctly assesses the functionality.
  • 164-164: The update to include an additional argument for the date format string in the getFormattedDate function call, along with specified options, is necessary for testing the new formatting capabilities. The approach is consistent with the objective of testing the new feature.
  • 170-170: The update to test the localization feature by specifying a different language code is important for ensuring the feature works as expected across different locales. This is crucial for testing the localization aspect of the new feature.

Copy link

changeset-bot bot commented Mar 30, 2024

⚠️ No Changeset found

Latest commit: dd540ce

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

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 Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3a690dd and 73e6290.
Files selected for processing (1)
  • packages/ui/package.json (1 hunks)
Additional Context Used
Additional comments not posted (1)
packages/ui/package.json (1)

80-80: The addition of date-fns version 3.6.0 to the dependencies is correctly implemented and aligns with the PR's objectives. Using a fixed version ensures stability but consider the trade-offs between fixed versions and version ranges for future maintenance.

@rluders
Copy link
Collaborator

rluders commented Apr 17, 2024

@dhavalveera could you please rebase it with main? I want to get this one approved. :)

@dhavalveera
Copy link
Contributor Author

@dhavalveera could you please rebase it with main? I want to get this one approved. :)

@rluders --- this conflicts, can't be done from my end, as I don't have the write access to this repository.

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 95a0aeb and b5ccdcd.

Files selected for processing (1)
  • packages/ui/package.json (1 hunks)
Additional comments not posted (2)
packages/ui/package.json (2)

64-64: The addition of date-fns with a fixed version number 3.6.0 aligns with the PR's objectives to enhance date formatting capabilities in the DatePicker component.


64-64: The addition of clean-package is a good practice for keeping the package lightweight for production. Ensure that the postpublish script correctly restores removed sections.

Verification successful

The postpublish script is correctly configured to run clean-package restore, which should restore the removed sections. However, we should verify the clean-package script's configuration to ensure it is set up properly.


The clean-package script is correctly configured in the scripts section and is set to run during prepack. The postpublish script is also correctly set to restore the removed sections.

  • packages/ui/package.json
    • "prepack": "clean-package"
    • "postpublish": "clean-package restore"
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct configuration of `clean-package` in the `postpublish` script.

# Test: Check the postpublish script for restoring the removed sections.
rg --type json 'postpublish' packages/ui/package.json

Length of output: 100



Script:

#!/bin/bash
# Description: Verify the configuration of the `clean-package` script in the `scripts` section.

# Test: Check the scripts section for the `clean-package` script configuration.
rg --type json 'clean-package' packages/ui/package.json

Length of output: 177

@dhavalveera
Copy link
Contributor Author

Hello @SutuSebastian , currently I'm facing an issue is like date-fns have different locales and the new Intl.DateTimeFormat() accepts the locales differently.

e.g. for date-fns it have locales like enUS, but whereas for Intl.DateTimeFormat() have locales like en-US due to which many Date Picker Helper Function & Test Cases are failing, how to handle this, can you please help/guide me on this, please 🙏

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

Outside diff range and nitpick comments (1)
packages/ui/src/components/Datepicker/helpers.ts (1)

Line range hint 108-128: **** Incorrect default parameter position and potential locale handling issue.

  1. Parameter Order: The default parameter language should not precede required parameters. This can lead to confusion and errors in function calls.
  2. Locale Handling: The way locales are handled might lead to unexpected behaviors if the locale code does not match exactly with DateFNSLocale entries.
- export const getFormattedDate = (
-   language: AvailableLocales = "enUS",
-   date: Date,
-   options?: Intl.DateTimeFormatOptions,
-   dateFormat: string = "dd-MMM-yyyy",
- ): string => {
+ export const getFormattedDate = (
+   date: Date,
+   dateFormat: string = "dd-MMM-yyyy",
+   language: AvailableLocales = "enUS",
+   options?: Intl.DateTimeFormatOptions
+ ): string => {

-   const getLocale =
-     language === "enUS" ? DateFNSLocale["enUS"] : Object.values(DateFNSLocale).find((l) => l.code === language);
+   const getLocale = DateFNSLocale[language] || DateFNSLocale.enUS;
  • Parameter Order: Rearrange parameters to place the default parameter last.
  • Locale Handling: Simplify the locale retrieval logic to directly access locales from DateFNSLocale and provide a fallback to enUS if the specified locale is not found.
Tools
Biome

[error] 109-109: This default parameter should follow the last required parameter or should be a required parameter. (lint/style/useDefaultParameterLast)

The last required parameter is here:

A default parameter that precedes a required parameter cannot be omitted at call site.
Unsafe fix: Turn the parameter into a required parameter.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b5ccdcd and 6b25cf8.

Files ignored due to path filters (1)
  • bun.lockb is excluded by !**/bun.lockb
Files selected for processing (2)
  • package.json (1 hunks)
  • packages/ui/src/components/Datepicker/helpers.ts (3 hunks)
Files skipped from review due to trivial changes (1)
  • package.json
Additional context used
Learnings (1)
packages/ui/src/components/Datepicker/helpers.ts (1)
User: rluders
PR: themesberg/flowbite-react#1314
File: packages/ui/src/components/Datepicker/helpers.ts:112-119
Timestamp: 2024-03-25T13:03:57.322Z
Learning: The `getFormattedDate` function in `packages/ui/src/components/Datepicker/helpers.ts` should use `DateFNSFormat` directly with the date and format string for proper localization support, mapping language codes to locale objects from `date-fns/locale`.
Biome
packages/ui/src/components/Datepicker/helpers.ts

[error] 109-109: This default parameter should follow the last required parameter or should be a required parameter. (lint/style/useDefaultParameterLast)

The last required parameter is here:

A default parameter that precedes a required parameter cannot be omitted at call site.
Unsafe fix: Turn the parameter into a required parameter.

Additional comments not posted (1)
packages/ui/src/components/Datepicker/helpers.ts (1)

5-5: **** Good use of TypeScript for defining type aliases.

Defining AvailableLocales as a type alias for locale keys in date-fns/locale ensures type safety and better integration with the date-fns library.

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6b25cf8 and dd540ce.

Files selected for processing (1)
  • packages/ui/package.json (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/ui/package.json

@rsessine
Copy link

rsessine commented Aug 7, 2024

Any news on this ?

@gsdev01
Copy link

gsdev01 commented Sep 24, 2024

any update ?

@SutuSebastian
Copy link
Collaborator

needs a re-work after #1190 is merged

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

Successfully merging this pull request may close these issues.

5 participants