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(hooks): handle numeric selectedKeys in Select #2589

Merged
merged 3 commits into from
Apr 2, 2024

Conversation

wingkwong
Copy link
Member

@wingkwong wingkwong commented Mar 26, 2024

Closes #2587

📝 Description

In collection, the key of the map is always string no matter the inputted key is numeric or string. Therefore, handle such case when we get the item from the collection.

⛳️ Current behavior (updates)

image

🚀 New behavior

image

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

No

📝 Additional Information

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of numeric keys in the multi-select component to ensure consistent behavior.

Copy link

changeset-bot bot commented Mar 26, 2024

🦋 Changeset detected

Latest commit: 2c412b3

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/use-aria-multiselect 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
Contributor

coderabbitai bot commented Mar 26, 2024

Walkthrough

The patch addresses an issue in the "@nextui-org/use-aria-multiselect" library, specifically enhancing the handling of numeric keys in the Select component. It ensures that keys, whether numeric or string, are treated uniformly by converting them to strings for consistent processing within the collection management functions. This change directly targets and resolves a bug where numeric selectedKeys were not correctly recognized, thereby improving the component's versatility and reliability in controlled mode scenarios.

Changes

File Path Change Summary
.changeset/hot-rivers-move.md Introduces a patch for numeric selectedKeys handling in Select.
packages/hooks/use-aria-multiselect/src/use-multiselect-list-state.ts Modifies key handling by coercing to strings for consistent access in collection management.

Assessment against linked issues

Objective Addressed Explanation
Support React.Key (string | number) in <Select /> controlled mode [#2587]
Correct handling of numeric selectedKeys in <Select /> to prevent undefined items [#2587]

Related issues

  • [BUG] - Autocomplete -- controlled component doesn't work with async filtering #2563: This issue could potentially be linked to the PR as it discusses a related problem with controlled components and key handling, specifically within the AutoComplete component. The underlying cause might be similar, suggesting an examination of the AutoComplete component might be beneficial, considering the changes made to the Select component.

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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

vercel bot commented Mar 26, 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 Apr 1, 2024 2:34am

Copy link

vercel bot commented Mar 26, 2024

@wingkwong 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 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.yaml

Commits Files that changed from the base of the PR and between 2aa4764 and 6903812.
Files selected for processing (2)
  • .changeset/hot-rivers-move.md (1 hunks)
  • packages/hooks/use-aria-multiselect/src/use-multiselect-list-state.ts (2 hunks)
Additional comments: 3
.changeset/hot-rivers-move.md (1)
  • 1-5: The changeset description is clear and accurately communicates the patch's purpose. The term "selectedKeys" is correctly used in this context, indicating a false positive from the static analysis tool.
packages/hooks/use-aria-multiselect/src/use-multiselect-list-state.ts (2)
  • 32-32: Coercing keys to strings before checking their presence in the collection is a straightforward solution to support numeric keys. Ensure this change does not inadvertently affect other parts of the codebase where keys are expected in their original form.
Verification successful

The search results indicate that collection.getItem is used in multiple places across the codebase. Specifically, within the use-aria-multiselect package, keys are coerced to strings before being used with collection.getItem, as seen in two instances in use-multiselect-list-state.ts. This aligns with the changes reviewed and suggests a consistent approach to handling keys within this context.

However, in other parts of the codebase, such as hidden-select.tsx and tree-keyboard-delegate.ts within the apps/docs/utils directory, keys are used with collection.getItem without explicit coercion to strings. This variation in usage indicates that while the change in use-multiselect-list-state.ts is appropriate for its specific context, there is no universal approach to key handling across the codebase.

Given the localized nature of the change and its consistency within the use-aria-multiselect package, the modification is verified as appropriate for its intended purpose. However, it's important to note the varied approaches to key handling in different parts of the codebase, which may warrant further review or standardization efforts depending on the broader application requirements.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for other occurrences where collection.getItem might be used with keys not coerced to strings.
rg "collection.getItem\((?!.*\+" --type ts

Length of output: 295



Script:

#!/bin/bash
# Corrected search for other occurrences where collection.getItem might be used with keys not coerced to strings, using PCRE2 for regex support.
rg "collection.getItem\((?!.*\+" --type ts --pcre2

Length of output: 125



Script:

#!/bin/bash
# Simplified search for all occurrences of collection.getItem to manually check for key coercion.
rg "collection.getItem" --type ts

Length of output: 815

* 42-42: Similarly, coercing keys to strings when retrieving selected items aligns with the PR's objective. This change should ensure consistent handling of numeric and string keys across the component.
Verification successful

The search results indicate that the collection.getItem method is used in various parts of the codebase, including both the use-aria-multiselect hook and other components/utilities like tree-keyboard-delegate and hidden-select. The usage in use-aria-multiselect includes coercing keys to strings, which aligns with the PR's objective for consistent handling of numeric and string keys. Other occurrences in the codebase do not seem to be directly affected by this change, as they either already use string keys or handle keys in a manner consistent with their specific contexts.

Given this information, it appears that the change to coerce keys to strings when retrieving selected items in the use-aria-multiselect hook does not negatively impact the retrieval of items elsewhere in the codebase. Each usage of collection.getItem is context-specific and seems to be appropriately handled within its respective component or utility.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the change in key handling does not impact the retrieval of items elsewhere in the codebase.
rg "collection.getItem\((?!.*\+" --type ts

Length of output: 295



Script:

#!/bin/bash
# Verify that the change in key handling does not impact the retrieval of items elsewhere in the codebase.
rg "collection.getItem\((?!.*\+" --type ts --pcre2

Length of output: 125



Script:

#!/bin/bash
# Search for all occurrences of collection.getItem to manually review the context.
rg "collection.getItem" --type ts

Length of output: 815

@jrgarciadev jrgarciadev added this to the v2.3.0 milestone Mar 31, 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

Review Status

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 6903812 and 2c412b3.
Files selected for processing (1)
  • packages/hooks/use-aria-multiselect/src/use-multiselect-list-state.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/hooks/use-aria-multiselect/src/use-multiselect-list-state.ts
Additional Context Used

@jrgarciadev jrgarciadev merged commit a60c2d7 into nextui-org:main Apr 2, 2024
7 of 8 checks passed
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 controlled mode not support number key
2 participants