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

refactor: use onPress event instead (component-wise) #4354

Merged
merged 7 commits into from
Dec 17, 2024

Conversation

wingkwong
Copy link
Member

@wingkwong wingkwong commented Dec 13, 2024

Closes #

📝 Description

⛳️ Current behavior (updates)

🚀 New behavior

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

📝 Additional Information

Summary by CodeRabbit

Release Notes

  • New Features

    • Updated event handling for various components to use onPress instead of onClick, enhancing user interaction consistency across the application.
  • Bug Fixes

    • Addressed deprecated onClick functionality in the Alert component to ensure compatibility with the latest library updates.
  • Tests

    • Adjusted test cases to reflect changes from onClick to onPress for various components, ensuring accurate testing of user interactions.

Copy link

changeset-bot bot commented Dec 13, 2024

🦋 Changeset detected

Latest commit: 4120d2e

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

This PR includes changesets to release 2 packages
Name Type
@nextui-org/alert 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 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 Dec 13, 2024 2:41pm
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 2:41pm

Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

Caution

Review failed

The head commit changed during the review from 1751ae4 to 4120d2e.

Walkthrough

The changes in this pull request involve updating the event handling for various components across the codebase, specifically transitioning from the deprecated onClick event handler to onPress. This update affects components such as Alert, Button, Link, Menu, and others, ensuring compatibility with the latest event handling conventions. The modifications also include adjustments to corresponding test cases and story configurations to reflect the new event model.

Changes

File Path Change Summary
.changeset/two-ghosts-search.md Updated @nextui-org/alert package to replace deprecated onClick functionality.
packages/components/alert/src/alert.tsx Changed Button component's onClick to onPress.
packages/components/button/tests/button-group.test.tsx Updated test case to change onClick to onPress for disabled ButtonGroup.
packages/components/button/tests/button.test.tsx Renamed test case from onClick to onPress and updated assertions accordingly.
packages/components/button/stories/button.stories.tsx Updated StateTemplate function's event handler from onClick to onPress.
packages/components/card/tests/card.test.tsx Renamed test case from onClick to onPress for Card component.
packages/components/dropdown/stories/dropdown.stories.tsx Changed DropdownItem event handler from onClick to onPress.
packages/components/link/tests/link.test.tsx Renamed test case from onClick to onPress for Link component.
packages/components/link/stories/link.stories.tsx Updated PressableTemplate function's event handler from onClick to onPress.
packages/components/listbox/stories/listbox.stories.tsx Changed ListboxItem event handler from onClick to onPress.
packages/components/menu/tests/menu.test.tsx Removed onClick from MenuItem test case, focusing on onPress and onAction.
packages/components/menu/stories/menu.stories.tsx Changed MenuItem event handler from onClick to onPress.

Possibly related PRs

Suggested labels

📋 Scope : Docs

Suggested reviewers

  • jrgarciadev: Suggested for review due to familiarity with the components affected by the changes.

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. (Experiment)
  • @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: 2

🧹 Outside diff range and nitpick comments (6)
.changeset/two-ghosts-search.md (1)

1-5: Fix typo and enhance changeset description

The changeset has a typo and could benefit from a more detailed description:

  1. "decrepate" should be "deprecated"
  2. Consider adding more details about:
    • The reason for the change
    • Whether this affects existing implementations
    • Migration steps if needed

Apply this diff:

---
"@nextui-org/alert": patch
---

-replace decrepate onClick in Alert
+replace deprecated onClick with onPress in Alert component
+
+This change:
+- Updates event handling to use onPress instead of onClick
+- Maintains backward compatibility
+- Aligns with the component library's event handling patterns
packages/components/card/__tests__/card.test.tsx (1)

44-52: Consider consolidating duplicate test cases

There are two test cases testing the same functionality:

  1. "should be clicked when is pressable" (lines 31-40)
  2. "should trigger onPress function" (lines 44-52)

Consider consolidating these tests to improve maintainability.

- it("should be clicked when is pressable", async () => {
-   const onPress = jest.fn();
-   const {getByRole} = render(<Card disableRipple isPressable onPress={onPress} />);
-
-   const button = getByRole("button");
-
-   await user.click(button);
-
-   expect(onPress).toHaveBeenCalled();
- });
-
- it("should trigger onPress function", async () => {
+ it("should trigger onPress when clicked and pressable", async () => {
    const onPress = jest.fn();
    const {getByRole} = render(<Card disableRipple isPressable onPress={onPress} />);

    const button = getByRole("button");

    await user.click(button);

    expect(onPress).toHaveBeenCalled();
  });
packages/components/button/__tests__/button.test.tsx (1)

Line range hint 1-85: Consider adding touch event tests

Since we're moving to onPress events, which are more touch-friendly, consider adding test cases for touch events to ensure proper behavior on touch devices.

Example test case to add:

it("should trigger onPress function on touch", async () => {
  const onPress = jest.fn();
  const {getByRole} = render(<Button disableRipple onPress={onPress} />);

  const button = getByRole("button");
  
  // Simulate touch events
  fireEvent.touchStart(button);
  fireEvent.touchEnd(button);

  expect(onPress).toHaveBeenCalled();
});
packages/components/menu/stories/menu.stories.tsx (1)

48-48: LGTM: Event handler updated correctly with a suggestion

The change from onClick to onPress is implemented correctly. However, consider improving the demo implementation.

Consider using a more descriptive demonstration instead of the alert function:

-    <MenuItem key="new" onPress={() => alert("[onPress] New file")}>
+    <MenuItem 
+      key="new" 
+      onPress={() => console.log("[MenuItem:onPress] New file clicked")}
+    >

This would provide better visibility in the browser's console and follow common development practices for debugging and demonstration purposes.

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

Line range hint 320-343: LGTM! Consider adding more test cases.

The test case effectively verifies both onPress and onAction handlers. Consider adding additional test cases to verify:

  • Event handling when the item is disabled
  • Event propagation order between onPress and onAction
packages/components/dropdown/stories/dropdown.stories.tsx (1)

151-151: Consider updating the alert message for consistency.

The alert message could be updated to reflect the event type being used.

-      <DropdownItem key="new" onPress={() => alert("New file")}>
+      <DropdownItem key="new" onPress={() => alert("[onPress] New file")}>
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fd86056 and afc5b8f.

📒 Files selected for processing (12)
  • .changeset/two-ghosts-search.md (1 hunks)
  • packages/components/alert/src/alert.tsx (1 hunks)
  • packages/components/button/__tests__/button-group.test.tsx (1 hunks)
  • packages/components/button/__tests__/button.test.tsx (1 hunks)
  • packages/components/button/stories/button.stories.tsx (1 hunks)
  • packages/components/card/__tests__/card.test.tsx (1 hunks)
  • packages/components/dropdown/stories/dropdown.stories.tsx (1 hunks)
  • packages/components/link/__tests__/link.test.tsx (1 hunks)
  • packages/components/link/stories/link.stories.tsx (1 hunks)
  • packages/components/listbox/stories/listbox.stories.tsx (1 hunks)
  • packages/components/menu/__tests__/menu.test.tsx (1 hunks)
  • packages/components/menu/stories/menu.stories.tsx (1 hunks)
🔇 Additional comments (5)
packages/components/button/__tests__/button-group.test.tsx (1)

29-31: LGTM! Event handler update is correct

The change from onClick to onPress is consistent with the refactoring objective while maintaining the same test coverage and verification logic.

packages/components/alert/src/alert.tsx (1)

76-76: Verify impact of onClick to onPress migration

The change from onClick to onPress on the close button is consistent with the refactoring objective. However, we should verify if this change affects existing consumers of the Alert component.

✅ Verification successful

Change from onClick to onPress is safe

The change from onClick to onPress on the close button is safe as there are no direct consumers using the onClick prop. The only usage of Alert component found in the codebase is in stories, where it uses onClose prop for handling close events, which is already aligned with the new implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any direct usage of Alert component's onClick handler
rg -l "onClick.*Alert" 

# Search for any spread props passed to Alert that might contain onClick
rg "const.*props.*=.*{.*onClick.*}.*\n.*<Alert.*{\.\.\.props}" -U

Length of output: 250


Script:

#!/bin/bash
# Search for Alert component usage patterns
rg -A 2 "<Alert.*onClick"

# Search for potential spread props usage with Alert
rg -A 2 "<Alert.*\.\.\."

# Search for test files that might be affected
rg -t test "Alert.*onClick"

# Look for Alert component stories or examples
fd -e stories.tsx -e stories.ts -x rg -l "Alert.*onClick" {}

Length of output: 1598

packages/components/button/stories/button.stories.tsx (1)

85-85: LGTM: Event handler updated correctly

The change from onClick to onPress is implemented correctly and maintains the component's state management functionality.

packages/components/link/stories/link.stories.tsx (1)

61-61: LGTM: Event handler updated correctly

The change from onClick to onPress is implemented correctly in the PressableTemplate component, maintaining proper state management and event logging.

packages/components/listbox/stories/listbox.stories.tsx (1)

177-177: LGTM! Event handler change is consistent.

The change from onClick to onPress is properly implemented and maintains consistency with the alert message format.

packages/components/link/__tests__/link.test.tsx Outdated Show resolved Hide resolved
packages/components/button/__tests__/button.test.tsx Outdated Show resolved Hide resolved
@jrgarciadev jrgarciadev merged commit fa38828 into canary Dec 17, 2024
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.

2 participants