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(listbox): change listBoxItem key to optional #3883

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

ryo-manba
Copy link
Member

@ryo-manba ryo-manba commented Oct 13, 2024

Closes #3880

📝 Description

In SelectBox and other collections, when items include id or key, these values are treated as the component's key.
This allows numeric values to remain as numbers. However, using key directly in the component (e.g, <SelectItem key={1} />) results in the value being converted to a string.

This PR makes key optional since id from the item object can be used directly.

Reference: ListBox – React Aria

⛳️ Current behavior (updates)

key is required and always converted to a string.

🚀 New behavior

key is now optional.

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

No.

📝 Additional Information

Summary by CodeRabbit

  • New Features

    • Enhanced the Select component with improved pre-selection functionality based on numeric keys and IDs.
  • Bug Fixes

    • Updated tests to ensure correct initialization and selection behavior in the Select component.
  • Tests

    • Added new test cases for the Select component to improve functionality and coverage.
    • Adjusted existing tests to accommodate changes in identifier types for item selection.

Copy link

changeset-bot bot commented Oct 13, 2024

🦋 Changeset detected

Latest commit: d89915d

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

This PR includes changesets to release 4 packages
Name Type
@nextui-org/listbox Patch
@nextui-org/autocomplete Patch
@nextui-org/select 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 Oct 13, 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 Oct 13, 2024 2:33pm
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 13, 2024 2:33pm

Copy link
Contributor

coderabbitai bot commented Oct 13, 2024

Walkthrough

The pull request modifies the ListboxItemBaseProps type by omitting the key property from the AriaOptionProps type, which may influence how keys are used in list components. Additionally, new test cases have been added to the Select component to ensure proper handling of numeric keys in the selectedKeys prop, enhancing test coverage and functionality.

Changes

File Path Change Summary
packages/components/listbox/src/base/listbox-item-base.tsx Updated ListboxItemBaseProps to omit key from AriaOptionProps.
packages/components/select/__tests__/select.test.tsx Added tests for Select component to verify handling of numeric keys in defaultSelectedKeys.

Assessment against linked issues

Objective Addressed Explanation
Support numeric keys in Select component (#3880)

Possibly related PRs

Suggested reviewers

  • wingkwong
  • jrgarciadev
  • rettimo

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

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8f62613 and f4407c6.

📒 Files selected for processing (2)
  • packages/components/listbox/src/base/listbox-item-base.tsx (1 hunks)
  • packages/components/select/tests/select.test.tsx (4 hunks)
🧰 Additional context used
🔇 Additional comments (2)
packages/components/listbox/src/base/listbox-item-base.tsx (1)

94-94: Approve: Improved key handling for ListboxItemBaseProps

This change aligns well with the PR objectives by making the key property optional in ListboxItemBaseProps. By omitting key from AriaOptionProps, the component can now use the id from the item object directly, addressing the issue with numeric keys being converted to strings.

Benefits of this change:

  1. Resolves the problem with numeric keys in the Select component (issue [BUG] - Select with numeric keys are not supported #3880).
  2. Maintains backwards compatibility while providing more flexibility.
  3. Aligns better with React's recommendations for key handling in lists.

To ensure this change doesn't introduce unintended side effects, please run the following verification:

This script will help identify any existing usage of the key prop in ListboxItem components, which might need attention due to this type change.

✅ Verification successful

Verified: No issues found with omitting the key prop in ListboxItemBaseProps

The change to omit the key property in AriaOptionProps has been verified and does not introduce any issues. All existing usages of the key prop are confined to non-production files such as stories and documentation, ensuring that the component's core functionality remains unaffected.

  • No key prop usages found in production component files.
  • Existing key usages in stories and docs are appropriate and non-impactful.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any direct usage of the 'key' prop in ListboxItem components

# Test: Search for ListboxItem components with a 'key' prop
rg --type typescript --type typescriptreact 'ListboxItem.*key=' -g '!*test*'

Length of output: 113


Script:

#!/bin/bash
# Description: Check for any direct usage of the 'key' prop in ListboxItem components by searching .ts and .tsx files

# Search for ListboxItem components with a 'key' prop in .ts and .tsx files, excluding test files
rg 'ListboxItem\s+[^>]*\bkey\s*=' --glob '!*.test.tsx' --glob '!*.test.ts'

Length of output: 6469

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

353-376: Test cases effectively cover numeric key handling

The added test cases correctly verify the handling of numeric keys and IDs in the Select component. They ensure that items are pre-selected as expected and that onSelectionChange callbacks receive the correct values. The assertions are appropriate, and the tests enhance the robustness of the component.

Also applies to: 378-401, 405-422, 445-462

@ryo-manba ryo-manba changed the title fix(listbox): listBoxItem key to optional fix(listbox): change listBoxItem key to optional Oct 13, 2024
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)
.changeset/curly-zoos-thank.md (1)

1-5: LGTM! Changeset looks good.

The changeset correctly identifies this as a patch update for the "@nextui-org/listbox" package, which is appropriate for a backwards-compatible bug fix. The change description is concise and references the relevant issue.

Consider expanding the change description slightly for more clarity:

-change ListboxItem key to optional (#3880)
+change ListboxItem key to optional to support numeric keys (#3880)

This addition provides more context about the purpose of the change without significantly increasing the length of the description.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f4407c6 and d89915d.

📒 Files selected for processing (1)
  • .changeset/curly-zoos-thank.md (1 hunks)
🧰 Additional context used

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.

[BUG] - Select with numeric keys are not supported
2 participants