nextui icon indicating copy to clipboard operation
nextui copied to clipboard

fix(date-picker): month and year picker scroller

Open buchananwill opened this issue 1 year ago โ€ข 15 comments

Closes #3785

๐Ÿ“ Description

Fixing the month/year UI sync bug.

โ›ณ๏ธ Current behavior (updates)

The month and year were incorrectly updating due to a UI rectangle intersection error. This has been removed and the data is updated directly from the value attribute.

๐Ÿš€ New behavior

Please describe the behavior or changes this PR adds

๐Ÿ’ฃ Is this a breaking change (Yes/No):

No.

๐Ÿ“ Additional Information

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced the AccordionTree component for hierarchical data representation.
    • Added an aria label for the month/year picker toggle button to enhance accessibility.
    • Enhanced keyboard interaction capabilities in the calendar picker component.
    • Added a new custom hook for managing key press states and scroll events.
  • Bug Fixes
    • Improved user interface and data synchronization for the month/year picker component.
  • Refactor
    • Simplified scrolling behavior and focused date management in the calendar picker component.
  • Chores
    • Updated versions for @nextui-org/accordion and @nextui-org/calendar packages.
    • Added a new test case for the month and year picker functionality in the DatePicker component.

buchananwill avatar Sep 22 '24 23:09 buchananwill

๐Ÿฆ‹ Changeset detected

Latest commit: 214385fd9b329bcc62d86b38d825bdcc62a19fcf

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 Patch
@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

changeset-bot[bot] avatar Sep 22 '24 23:09 changeset-bot[bot]

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

A member of the Team first needs to authorize it.

vercel[bot] avatar Sep 22 '24 23:09 vercel[bot]

Walkthrough

This pull request introduces updates to three packages: @nextui-org/accordion, @nextui-org/calendar, and @nextui-org/date-picker. The @nextui-org/accordion package has received a minor version update, while the @nextui-org/calendar package has been patched. A new component, AccordionTree, has been added to support hierarchical data. Additionally, significant refactoring has been performed on the useCalendarPicker function, simplifying the scrolling behavior and focused date management. Accessibility improvements have also been made to the date picker component.

Changes

File Path Change Summary
.changeset/serious-snails-count.md Documented minor version update for @nextui-org/accordion and patch update for @nextui-org/calendar. Noted a fix related to the month/year picker component.
packages/components/accordion/src/accordion-tree.tsx Introduced AccordionTree component with interfaces AccordionTreeItemProps and AccordionTreeProps for hierarchical data support.
packages/components/calendar/src/calendar-header.tsx Added constant monthAndYearPickerToggle for the aria-label of the month/year picker toggle button, enhancing accessibility.
packages/components/calendar/src/calendar-picker-item.tsx Updated CalendarPickerItem to include onKeyUp prop for handling key-up events.
packages/components/calendar/src/calendar-picker.tsx Introduced onPickerItemKeyUp event handler for enhanced keyboard interaction in the calendar picker.
packages/components/calendar/src/use-calendar-picker.ts Removed handleListScroll function and debounced scroll event listeners, simplifying the logic for updating the focused date in the calendar picker component.
packages/components/date-picker/tests/date-picker.test.tsx Introduced a new test case for the month and year picker functionality, verifying button interactions and visibility of the dialog.

Assessment against linked issues

Objective Addressed Explanation
Ensure highlighted, focused, and selected values are consistent in Date Picker (#3785) โ“ Changes simplify scrolling but do not explicitly confirm resolution of the issue.

Possibly related PRs

  • #2845: Addresses issues in the DatePicker component, specifically related to the month and year pickers, which aligns with the main PR's focus on enhancing the date picker functionality.
  • #3088: Prevents changes in the year and month when toggling the month and year pickers, directly relating to the updates made in the main PR regarding the date picker component.
  • #3331: Resolves a bug related to incorrect year display in the showMonthAndYearPickers component, which is pertinent to the main PR's updates on the date picker.
  • #3416: Resolves a width clipping issue in the DatePicker, which is relevant to the overall functionality and user experience improvements noted in the main PR.
  • #3773: Enhances the DatePicker by ensuring that classNames from calendarProps are correctly applied, which is related to the styling updates in the main PR.

Suggested labels

๐Ÿ“‹ Scope : Docs

Suggested reviewers

  • wingkwong
  • jrgarciadev
  • ryo-manba

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.

coderabbitai[bot] avatar Sep 22 '24 23:09 coderabbitai[bot]

  1. BEFORE: https://github.com/nextui-org/nextui/issues/3785#issuecomment-2366931481 AFTER:

https://github.com/user-attachments/assets/82b59fea-ef1c-434d-92af-58f87cdbc65f

  1. Issue: https://github.com/nextui-org/nextui/issues/3785
  2. I've parked the accordion-tree on a different branch and delete that file in this commit.
  3. I added a test that checks the tabindex is correctly being modified, as that is uniquely 0 for the focused month/year in the picker.

buchananwill avatar Sep 23 '24 11:09 buchananwill

just keep one changeset file where no accordion is mentioned

Is that correct now?

buchananwill avatar Sep 23 '24 11:09 buchananwill

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 Sep 25, 2024 0:44am
nextui-storybook-v2 โœ… Ready (Inspect) Visit Preview ๐Ÿ’ฌ Add feedback Sep 25, 2024 0:44am

vercel[bot] avatar Sep 24 '24 17:09 vercel[bot]

Please suggest an alternative way to mark this component so the test can find it.

On Wed, 25 Sept 2024, 14:04 ีกำ„ีก, @.***> wrote:

@.**** requested changes on this pull request.

In packages/components/calendar/src/calendar-header.tsx https://github.com/nextui-org/nextui/pull/3789#discussion_r1775134096:

@@ -99,6 +101,7 @@ export function CalendarHeader(props: CalendarHeaderProps) { return showMonthAndYearPickers ? ( <Button {...headerProps}

  •  aria-label={monthAndYearPickerToggle}
    

we don't hardcode aria-label internally. We can take it from users' side tho.

โ€” Reply to this email directly, view it on GitHub https://github.com/nextui-org/nextui/pull/3789#pullrequestreview-2328101810, or unsubscribe https://github.com/notifications/unsubscribe-auth/BBL2M6NRIQAJMCWW6L6TKF3ZYKYEDAVCNFSM6AAAAABOU7COTKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDGMRYGEYDCOBRGA . You are receiving this because you were mentioned.Message ID: @.***>

buchananwill avatar Sep 25 '24 13:09 buchananwill

In the test, you can simply get the element by container.querySelector("[data-slot='header']")

wingkwong avatar Sep 25 '24 13:09 wingkwong

Please include the content of this comment in the PR description. https://github.com/nextui-org/nextui/pull/3789#issuecomment-2367949681

ryo-manba avatar Sep 27 '24 11:09 ryo-manba

@buchananwill please click re-request review once it's ready for review.

wingkwong avatar Oct 02 '24 07:10 wingkwong

I had one more idea, which I've added. The PickerItems now also have a keyUp listener, which just calls the scrollIntoView function. This seems to be a bullet proof way of ensuring that if you've been holding down any of the directional keys, then whatever was the last key to fire (and thus has focus) is scrolled to the desired position.

I have to confess my weaker experience with writing imperative DOM events, so please if anyone with that could ponder whether this broad-brush fix could have any unexpected side-effects?

I also changed the home/end values of the keyDown event to 1 and 12, since months are 1-indexed, not zero indexed. These buttons don't work for the year picker, since 1 and 12 aren't valid years within the available range. Couldn't see an obvious way to pass in the max/min years that would be dynamically set (since it could vary with locale etc.).

Hopefully this wraps up the functionality of this component & hook?

buchananwill avatar Oct 02 '24 20:10 buchananwill

The Home/End keys seem to already function in the original implementation. Where did you find the information about the month's index?

I also changed the home/end values of the keyDown event to 1 and 12, since months are 1-indexed, not zero indexed. These buttons don't work for the year picker, since 1 and 12 aren't valid years within the available range.

ryo-manba avatar Oct 03 '24 07:10 ryo-manba

I added two additional hooks to cover the different behaviours needed for the different navigation keys:

  1. Home/End use a deferred focus that only triggers when the scroll ends. Otherwise, the focus event overrides the scrolling, and the element immediately snaps into view.
  2. Home/End also use a repeated key blocker, to prevent the value from receiving more than one update from these keys, since a. repetition of this event is meaningless b. it was playing havoc with the other scrolling/focus events/behaviours.

If you have existing hooks with the required API already in the codebase, feel free to swap them in. Equally, feel free to migrate them to a different (generic) hook folder if you want to centralize them for other scenarios to use.

I can't find any more bugs in this component.

On a side note, I prefer the simpler version where home/end cause the instant scrolling. I get that this is a minor inconsistency in the style of the UI, but the two extra control flow checks to defer the focus update and block repeat key strokes seems overly convoluted. I wouldn't be surprised to find it has negative performance implications or introduces other bugs on different systems from my own...

Let me know what you think! :-)

buchananwill avatar Oct 03 '24 16:10 buchananwill

Yep there was one more bug where the listener wasn't clearing correctly. Fixed that now. cries in open source

buchananwill avatar Oct 03 '24 16:10 buchananwill

On a side note, I prefer the simpler version where home/end cause the instant scrolling. I get that this is a minor inconsistency in the style of the UI, but the two extra control flow checks to defer the focus update and block repeat key strokes seems overly convoluted. I wouldn't be surprised to find it has negative performance implications or introduces other bugs on different systems from my own...

This seems to be a separate issue from the current one. If you'd like to address it, please open a new issue for further discussion.

ryo-manba avatar Oct 19 '24 01:10 ryo-manba

Any updates on this PR?

lukasssicevs avatar Nov 06 '24 09:11 lukasssicevs