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(input): ensure clear button is not focusable when disabled #3774

Merged
merged 8 commits into from
Sep 26, 2024

Conversation

ryxxn
Copy link
Contributor

@ryxxn ryxxn commented Sep 17, 2024

📝 Description

This PR fixes an issue where the clear button inside the Input component could still receive focus when the input is disabled (isDisabled). The button is now properly excluded from the tab order by setting tabIndex to -1 when the input is disabled.

⛳️ Current behavior (updates)

record.mov

Currently, the clear button in the Input component can still receive focus via the Tab key when the input is disabled. This behavior can cause accessibility issues and an inconsistent user experience.

🚀 New behavior

With this fix, the clear button will no longer be focusable when the input is disabled. The tabIndex for the clear button is set to -1 when isDisabled is true, preventing it from being part of the tab order.

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

📝 Additional Information

This fix enhances accessibility and user experience by ensuring that the clear button behaves correctly when the input is disabled.

Summary by CodeRabbit

  • New Features
    • Enhanced accessibility for the clear button in the input component by preventing focus when the input is disabled.
    • Updated the clear button from a <span> to a <button> element for improved semantic meaning and usability.
  • Bug Fixes
    • Adjusted the tabIndex property for the clear button to ensure compliance with keyboard navigation standards.
  • Tests
    • Added new test cases to verify the correct behavior of the clear button's state when the input is disabled and ensure it is not focusable.

Copy link

changeset-bot bot commented Sep 17, 2024

🦋 Changeset detected

Latest commit: 10b4a1d

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/input Patch
@nextui-org/autocomplete 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 Sep 17, 2024

@ryxxn 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 Sep 17, 2024

Walkthrough

This update introduces a patch to the @nextui-org/input component, specifically modifying the behavior of the clear button when the input is disabled. The clear button will no longer receive focus in a disabled state, and its tabIndex is set to -1 under this condition. Additionally, the clear button's implementation is changed from a <span> to a <button> element to improve accessibility. New test cases have been added to verify these behaviors.

Changes

File Change Summary
.changeset/two-waves-own.md Introduced a patch for the clear button in the @nextui-org/input component to enhance accessibility.
packages/components/input/tests/input.test.tsx Added test cases for the Input component to check the behavior of the clear button when isDisabled is true and verify that it is not focusable.
packages/components/input/src/use-input.ts Modified button properties to reflect the disabled state based on the isDisabled property.
packages/components/input/src/input.tsx Changed the clear button from a <span> to a <button> element for improved semantic meaning and accessibility.

Possibly related PRs

Suggested reviewers

  • jrgarciadev
  • wingkwong

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.

@wingkwong wingkwong added this to the v2.5.0 milestone Sep 17, 2024
packages/components/input/src/use-input.ts Outdated Show resolved Hide resolved
packages/components/input/__tests__/input.test.tsx Outdated Show resolved Hide resolved
@ryxxn
Copy link
Contributor Author

ryxxn commented Sep 19, 2024

@ryo-manba
I have updated the code to replace the span element with a button element for the clear button.

This change removes the need to manually handle tabIndex and role, while improving accessibility.

@wingkwong wingkwong requested review from wingkwong and removed request for jrgarciadev September 25, 2024 02:08
Copy link
Member

@ryo-manba ryo-manba left a comment

Choose a reason for hiding this comment

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

Good! Thanks for the PR!

Copy link

vercel bot commented Sep 25, 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 Sep 25, 2024 4:33pm
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 4:33pm

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.

@ryxxn
Copy link
Contributor Author

ryxxn commented Sep 25, 2024

@wingkwong

Thank you for the feedback!
I've added a test case.

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 (3)
packages/components/input/__tests__/input.test.tsx (3)

48-54: LGTM: New test case for non-focusable clear button.

This test case effectively verifies that the clear button is not focusable by checking its tabIndex attribute. It aligns well with the PR objective and uses the recommended getByRole method for querying elements.

Consider adding an additional assertion to check if the button is not disabled in this case, as it would help distinguish between the behavior of a disabled button and a non-focusable button:

expect(clearButton).not.toBeDisabled();

160-160: LGTM: Updated clear button selection method.

The change to use getByRole("button") aligns with best practices for querying elements in tests.

While the non-null assertion (!) ensures type safety, consider handling the case where the button might not be present:

const clearButton = getByRole("button", { name: /clear/i });
expect(clearButton).toBeInTheDocument();

This approach would provide a more descriptive error if the button is missing and aligns with Testing Library's error-first philosophy.


275-275: LGTM: Improved submit button selector specificity.

The change to use querySelector('button[type="submit"]') improves the specificity of the submit button selection, which is a good practice for robust tests.

Consider using Testing Library's getByRole method for consistency with other tests and better accessibility representation:

submitButton = screen.getByRole('button', { type: 'submit' });

This approach would also eliminate the need for the non-null assertion and provide a more descriptive error if the button is missing.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 84fd6b4 and 10b4a1d.

📒 Files selected for processing (1)
  • packages/components/input/tests/input.test.tsx (4 hunks)
🔇 Additional comments not posted (1)
packages/components/input/__tests__/input.test.tsx (1)

40-46: LGTM: New test case for disabled clear button.

This test case effectively verifies that the clear button is disabled when the isDisabled prop is set to true. It aligns well with the PR objective and uses the recommended getByRole method for querying elements.

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.

I think you misunderstood my comment.

First the PR here is to fix the issue where currently the clear button is focusable when the input is disabled. With your PR changes, it looks fine.

In my previous comment, I was saying that the clear button can be focusable when the input is not disabled. With your PR changes, this existing expected behaviour was changed, which is not expected. I've also provided two links for you to compare. That's why I asked for changes and a new test case to cover the base case.

Therefore, I'm expecting two test cases.

  • should allow focus on clear button when input is not disabled
  • should not allow focus on clear button when input is disabled

@ryxxn
Copy link
Contributor Author

ryxxn commented Sep 25, 2024

@wingkwong

I previously received feedback suggesting that setting tabIndex="-1" for the clear button to make it non-focusable at all times would improve web accessibility.

https://github.com/nextui-org/nextui/pull/3774/files/b3bd46ab9434449c5db01f1f9daf0d1dd893ed9c#r1774284969

Could we discuss which approach aligns better with the project's accessibility guidelines? Should the clear button always be non-focusable for accessibility, or should it remain focusable when the input is not disabled?

I can adjust the implementation based on the preferred approach.

@wingkwong
Copy link
Member

I see. Lemme discuss with Ryo internally first and get back to you afterwards. Thanks.

@wingkwong
Copy link
Member

confirmed the clear button will not be focusable no matter it is disabled or not.

ref:

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.

3 participants