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(select): remove input from hidden-select #4427

Merged
merged 4 commits into from
Dec 22, 2024

Conversation

Peterl561
Copy link
Contributor

@Peterl561 Peterl561 commented Dec 22, 2024

Closes #3913
Alternative to #4281

📝 Description

  • hidden-select has a hidden input that's causing the browser required error popup to appear instead of NextUI's
  • select does not commit validation on value change when validationBehavior=native, which causes error messages to not clear
    • fix by calling state.commitValidation when value changes
  • updated old select tests that did not test what they were supposed to test

⛳️ Current behavior (updates)

when isRequired and validationBehavior=native, select is showing browser error popup

a1.mp4

when validationBehavior=native, error does not clear on change

a2.mp4

🚀 New behavior

when isRequired and validationBehavior=native, select shows the correct error message

b1.mp4

when validationBehavior=native, error clears on change

b2.mp4

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

No

📝 Additional Information

Summary by CodeRabbit

  • New Features

    • Enhanced validation behavior for the select component, improving error handling.
    • Added new properties for better control over listbox rendering and performance.
  • Bug Fixes

    • Resolved issues causing browser UI errors with native validation behavior.
    • Improved error message handling when using native validation.
  • Documentation

    • Comprehensive updates to the Select component documentation, including installation, usage examples, and advanced features.
  • Tests

    • Updated and added tests to ensure robust validation logic and selection handling.

Copy link

changeset-bot bot commented Dec 22, 2024

🦋 Changeset detected

Latest commit: 5b5796c

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

vercel bot commented Dec 22, 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 Dec 22, 2024 3:02am
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 22, 2024 3:02am

Copy link
Contributor

coderabbitai bot commented Dec 22, 2024

Walkthrough

This pull request focuses on improving the validation behavior of the Select component in the NextUI library. The changes address issues with native validation, error handling, and component state management. The modifications span across multiple files, including test suites, documentation, and core component logic, with the primary goal of ensuring consistent validation behavior across different form components.

Changes

File Change Summary
packages/components/select/src/use-select.ts Added commitValidation() in selection change callback; expanded UseSelectProps with virtualization and rendering options
packages/hooks/use-aria-multiselect/src/use-multiselect-state.ts Added validationState.commitValidation() in toggle method
packages/components/select/src/hidden-select.tsx Simplified input rendering and handling of select props
packages/components/select/__tests__/select.test.tsx Updated test cases for validation behavior and error handling
apps/docs/content/docs/components/select.mdx Comprehensive documentation update for Select component

Assessment against linked issues

Objective Addressed Explanation
Consistent validation behavior between Input and Select
Support for validationBehavior in Select
HTML built-in validation for Select

Possibly related PRs

Suggested labels

👀 Status: In Review, 📋 Scope : Docs

Suggested reviewers

  • wingkwong
  • jrgarciadev

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

🧹 Nitpick comments (6)
packages/components/select/__tests__/select.test.tsx (5)

1183-1230: Add test coverage for unselected edge case
This test scenario looks good and ensures that selecting a different option resolves the validation error. As an optional improvement, consider adding a test for the unselected state (e.g., no items selected) to verify that the form is valid when it's not required or to confirm that an appropriate validation message appears if it is required.


1231-1300: Enhance server validation coverage
The coverage for setting and clearing error states on user interaction is correct. You may want to expand this further by adding a test for reverting to the previously selected item to ensure that the error consistently clears or persists as expected in all transitions.


Line range hint 1303-1356: Consider testing default value scenario
When using isRequired with native validation, the coverage is thorough. However, you could add a test covering the scenario where a default value is already set on the required select, confirming that the form is immediately valid without user interaction.


1360-1413: Validate function test covers key workflow
This test properly verifies native validation with a custom validate function. To strengthen reliability, consider adding an additional assertion for switching back to an invalid selection (e.g., "penguin") after choosing a valid value—ensuring that the error reappears.


1414-1483: Comprehensive server validation scenario
The test successfully demonstrates changing server error states under native validation. As a minor enhancement, adding a test to ensure that repeated submissions aggregate or replace errors properly could further validate robust error handling.

packages/components/select/src/hidden-select.tsx (1)

122-122: Avoiding large hidden The threshold of 300 items for deciding between a hidden select and a hidden input is a practical performance optimization. Consider making this threshold a configurable prop for flexibility in different use cases. 📜 Review details Configuration used: .coderabbit.yaml Review profile: CHILL Plan: Pro 📥 Commits Reviewing files that changed from the base of the PR and between 5494fa2 and 5b5796c. 📒 Files selected for processing (6) .changeset/lemon-cheetahs-grow.md (1 hunks) apps/docs/content/docs/components/select.mdx (0 hunks) packages/components/select/__tests__/select.test.tsx (3 hunks) packages/components/select/src/hidden-select.tsx (2 hunks) packages/components/select/src/use-select.ts (1 hunks) packages/hooks/use-aria-multiselect/src/use-multiselect-state.ts (0 hunks) 💤 Files with no reviewable changes (2) packages/hooks/use-aria-multiselect/src/use-multiselect-state.ts apps/docs/content/docs/components/select.mdx 🔇 Additional comments (4) .changeset/lemon-cheetahs-grow.md (1) 1-7: Informative and succinct changeset These changeset notes clearly document the introduced patches. Everything appears fine, and the descriptions are appropriately concise. No additional action needed. packages/components/select/src/hidden-select.tsx (2) 92-92: Hidden input style Setting "display: none" ensures the input is truly hidden. Be sure to confirm that screen readers do not inadvertently pick it up, but since the style is hidden alongside additional ARIA attributes, it should remain accessible only to form submission logic. 95-99: Validate ARIA attributes The ARIA attributes (aria-invalid, aria-required) and required prop are conditionally applied based on the validationBehavior. This is a reliable way to handle two different validation modes. No issues found here—just ensure any upstream or downstream code also differentiates between "aria" and "native" modes as needed. packages/components/select/src/use-select.ts (1) 304-304: Timely validation submission Calling “state.commitValidation();” upon selection change ensures that errors are recalculated and displayed promptly. This is a valuable addition, especially for real-time feedback.

@wingkwong wingkwong added this to the v2.6.9 milestone Dec 22, 2024
Copy link
Member

@jrgarciadev jrgarciadev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!, thanks!

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] - Add validationBehavior support for Select
3 participants