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(date-picker): added isOpen and onOpenChange #3476

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

Conversation

ShrinidhiUpadhyaya
Copy link
Contributor

@ShrinidhiUpadhyaya ShrinidhiUpadhyaya commented Jul 15, 2024

Closes #3457

📝 Description

Added isOpen prop and onOpenChange event, so that they could be used to control visibility of the DatePicker popover.

⛳️ Current behavior (updates)

🚀 New behavior

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

📝 Additional Information

If the PR gets accepted please use my GitHub email-id ([email protected]) instead of my other email-id for the Co-authored-by: message.

Summary by CodeRabbit

  • New Features
    • Added isOpen and onOpenChange functionalities to the DatePicker component for improved control over the date picker's visibility.

Copy link

changeset-bot bot commented Jul 15, 2024

🦋 Changeset detected

Latest commit: 67af710

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

This PR includes changesets to release 2 packages
Name Type
@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 15, 2024

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

Name Status Preview Comments Updated (UTC)
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 16, 2024 2:32pm

Copy link

vercel bot commented Jul 15, 2024

Someone 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 15, 2024

Caution

Review failed

The head commit changed during the review from 5b9a42d to 67af710.

Walkthrough

The changes introduce the ability to manage the visibility state of the DatePicker component in the @nextui-org/date-picker package. This includes adding isOpen state management and onOpenChange handlers to control and respond to the opening and closing of the date picker. These updates aim to enhance user interaction, especially addressing issues where the DatePicker would not dismiss after selecting a date from a preset.

Changes

File Path Change Summary
.../date-picker/src/use-date-picker-base.ts Added onOpenChange property to the props object and destructured object.
.../date-picker/src/use-date-picker.ts Included handling for onOpenChange with the isOpen parameter and exposed them in the return object.
.changeset/silent-countries-drum.md Documented the addition of isOpen and onOpenChange functionalities to the DatePicker component.
.../date-picker/stories/date-picker.stories.tsx Added isOpen state and setIsOpen handler for visibility management of the date picker. Modified button handlers to update isOpen state.
.../docs/content/components/date-picker/preset.ts Added isOpen state management and modified button onPress handlers to set isOpen to false after updating the value. Included the onOpenChange handler.

Sequence Diagram(s)

N/A

Assessment against linked issues

Objective (Issue) Addressed Explanation
Fix bug where DatePicker does not dismiss after selecting a date from Preset (#3457)
Ensure onOpenChange triggers properly when DatePicker visibility changes (#3457)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b762141 and 1a2e393.

Files selected for processing (2)
  • packages/components/date-picker/src/use-date-picker-base.ts (2 hunks)
  • packages/components/date-picker/src/use-date-picker.ts (2 hunks)
Additional comments not posted (5)
packages/components/date-picker/src/use-date-picker.ts (3)

91-91: Integration of onOpenChange prop.

The onOpenChange prop is correctly destructured from the useDatePickerBase return object.


99-102: Ensure onOpenChange is called appropriately.

The onOpenChange function is called with the isOpen parameter, and the onClose function is triggered when isOpen is false. This ensures the visibility of the DatePicker popover is managed correctly.


233-234: Expose isOpen and onOpenChange in the return object.

The isOpen state and onOpenChange function are exposed in the return object, aligning with the PR objectives to manage the DatePicker's visibility programmatically.

packages/components/date-picker/src/use-date-picker-base.ts (2)

146-146: Integration of onOpenChange prop.

The onOpenChange prop is correctly destructured from the props object.


301-301: Expose onOpenChange in the return object.

The onOpenChange function is exposed in the return object, aligning with the PR objectives to manage the DatePicker's visibility programmatically.

@wingkwong wingkwong self-assigned this Jul 16, 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.

  1. missing changeset
  2. how does it fix the linked issue? can you update the preset example also?

@ShrinidhiUpadhyaya
Copy link
Contributor Author

@wingkwong For the changeset, should it be a minor or a patch?

@wingkwong
Copy link
Member

mark it patch first

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 1a2e393 and e53186f.

Files selected for processing (2)
  • .changeset/silent-countries-drum.md (1 hunks)
  • packages/components/date-picker/stories/date-picker.stories.tsx (3 hunks)
Additional comments not posted (4)
.changeset/silent-countries-drum.md (1)

1-5: Changeset file is correctly formatted and documented.

The changeset specifies a patch update for the "@nextui-org/date-picker" package and provides a clear description of what was added, linking to the relevant issue.

packages/components/date-picker/stories/date-picker.stories.tsx (3)

200-200: State initialization for visibility control added.

The addition of isOpen state using React.useState is correctly implemented to manage the visibility of the DatePicker.


260-283: Logic to manage DatePicker visibility on date selection.

The onPress handlers for the buttons are correctly updating the isOpen state to close the DatePicker after a date is selected. This aligns with the expected behavior described in the issue.


296-299: Props for controlling DatePicker visibility added.

The isOpen and onOpenChange props are correctly passed to the DatePicker component, allowing external control over its visibility.

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.

can u also update the examples in docs as well

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 e53186f and 67af710.

Files selected for processing (1)
  • apps/docs/content/components/date-picker/preset.ts (3 hunks)
Additional comments not posted (3)
apps/docs/content/components/date-picker/preset.ts (3)

69-92: Review the changes to button onPress handlers.

The onPress handlers for the buttons "Today", "Next week", and "Next month" now include setting isOpen to false, which is intended to close the DatePicker after a date is selected. This change directly addresses the bug described in the issue and should resolve the problem of the DatePicker not dismissing as expected.


9-9: Confirm the initial state for isOpen.

The initial state for isOpen is set to false, which is appropriate as the DatePicker should not be visible initially. However, consider if there are scenarios where the DatePicker should be open on component mount based on props or previous user actions.

Verification successful

Confirm the initial state for isOpen.

The initial state for isOpen is correctly set to false, and there are no conditions or props found that suggest it should be set to true initially.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if there are any props or conditions that should set isOpen to true initially.

# Test: Search for any conditions or props that might set isOpen initially.
rg --type tsx -A 5 $'isOpen={isOpen}'

Length of output: 144


Script:

#!/bin/bash
# Description: Verify if there are any props or conditions that should set isOpen to true initially.

# Test: Search for any conditions or props that might set isOpen initially.
rg --type tsx -A 5 'isOpen=\{isOpen\}'

Length of output: 68


Script:

#!/bin/bash
# Description: Verify if there are any props or conditions that should set isOpen to true initially.

# Test: Search for any conditions or props that might set isOpen initially.
rg -A 5 'isOpen=\{isOpen\}'

Length of output: 11737


105-108: Review the integration of isOpen and onOpenChange props in the DatePicker.

The isOpen and onOpenChange props are correctly integrated into the DatePicker component. This setup allows external control over the visibility of the DatePicker, which enhances its flexibility and usability in various contexts.

Additionally, ensure that onOpenChange is properly handling all scenarios where the DatePicker should close or open, beyond just button presses.

Verification successful

Integration of isOpen and onOpenChange props in the DatePicker is correctly handled.

The isOpen and onOpenChange props are properly integrated into the DatePicker component, allowing for external control over its visibility. The setIsOpen function is used comprehensively across various scenarios to manage the isOpen state, ensuring that the DatePicker opens and closes as expected.

  • onOpenChange is used in multiple components to handle state changes effectively.
  • setIsOpen is defined using React.useState or useState and correctly updates the isOpen state.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if `onOpenChange` handles all scenarios for opening and closing the DatePicker.

# Test: Search for all usages of `onOpenChange` to ensure comprehensive handling.
rg --type tsx -A 5 $'onOpenChange={setIsOpen}'

Length of output: 168


Script:

#!/bin/bash
# Description: Verify if `onOpenChange` handles all scenarios for opening and closing the DatePicker.

# Test: Search for all usages of `onOpenChange` to ensure comprehensive handling.
rg --type tsx -A 5 'onOpenChange=\{setIsOpen\}'

Length of output: 77


Script:

#!/bin/bash
# Description: Verify if `onOpenChange` handles all scenarios for opening and closing the DatePicker.

# Test: Search for all usages of `onOpenChange` to ensure comprehensive handling.
rg --glob '*.tsx' -A 5 'onOpenChange=\{setIsOpen\}'

Length of output: 3303


Script:

#!/bin/bash
# Description: Verify the comprehensive handling of `onOpenChange` by examining the definition and usage of `setIsOpen`.

# Test: Search for the definition of `setIsOpen` to understand its implementation.
rg --glob '*.tsx' 'const setIsOpen'

# Test: Search for all usages of `setIsOpen` to ensure comprehensive handling.
rg --glob '*.tsx' 'setIsOpen('

Length of output: 141


Script:

#!/bin/bash
# Description: Verify the comprehensive handling of `onOpenChange` by examining the definition and usage of `setIsOpen`.

# Test: Search for the definition of `setIsOpen` to understand its implementation.
rg --glob '*.tsx' 'setIsOpen'

# Test: Search for all usages of `setIsOpen` to ensure comprehensive handling.
rg --glob '*.tsx' 'setIsOpen\('

Length of output: 3728

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.

please include test cases

@ShrinidhiUpadhyaya
Copy link
Contributor Author

please include test cases

I was testing while adding the test cases and i noticed a bug. Consider the below code where you toggle isOpen using a component like Button in the below code.

const [isOpen, setIsOpen] = React.useState<boolean>(false);

<Button
   onClick={() => {
      console.log("Toggle Outside", isOpen);
      setIsOpen(!isOpen);
  }}
>
  Toggle Outside
</Button>

Bugs:-

  1. onOpenChange is not triggered when you click on Button to show the date picker, but the DatePicker is shown.
  2. onOpenChange gets triggered when you click on Button to hide the date picker, but the DatePicker is not hidden, it flickers for a second and stays.

Note:-

  1. You could add the above code to Presets template in DatePicker to test.
  2. I observed similar bug inside the Popover component in the OpenChange template.

Could you tell me how you want me to proceed further? Do we need to fix this or do you want me to write the test cases only for the rendering of DatePicker

@wingkwong
Copy link
Member

Please fix the bug as well.

I observed similar bug inside the Popover component in the OpenChange template.

I couldn't reproduce in canary branch nor production. onOpenChange is triggered no matter I open it or close it.

@ShrinidhiUpadhyaya
Copy link
Contributor Author

Please fix the bug as well.

I observed similar bug inside the Popover component in the OpenChange template.

I couldn't reproduce in canary branch nor production. onOpenChange is triggered no matter I open it or close it.

Could you please check by changing the OpenChangeTemplate in popover.stories.tsx.

The code looks something like this

const OpenChangeTemplate = (args: PopoverProps) => {
  const [isOpen, setIsOpen] = React.useState(false);

  return (
    <div className="flex flex-col gap-2">
      <Button
        className="mb-10"
        onClick={() => {
          console.log("Toggle Outside", isOpen);
          setIsOpen(!isOpen);
        }}
      > 
        Toggle Outside
      </Button>
      <Popover
        {...args}
        isOpen={isOpen}
        style={{
          zIndex: 10,
        }}
        onOpenChange={(open) => {
          console.log("Popover: onOpenChange", open);
          setIsOpen(open);
        }}
      >
        <PopoverTrigger>
          <Button>Open Popover</Button>
        </PopoverTrigger>
        <PopoverContent>
          <div className="px-1 py-2">
            <div className="text-sm font-bold">Popover Content</div>
            <div className="text-xs">This is a content of the popover</div>
          </div>
        </PopoverContent>
      </Popover>
      <p className="text-sm">isOpen: {isOpen ? "true" : "false"}</p>
    </div>
  );
};

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.

[BUG] - Unable to dismiss DatePicker after date selected by Preset
3 participants