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(tree): [tree] fix parent node toggle when click child node margin #2501

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

gimmyhehe
Copy link
Member

@gimmyhehe gimmyhehe commented Nov 8, 2024

close #2491

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

close #2491

Summary by CodeRabbit

  • New Features

    • Enhanced tree component interactivity with new functions for icon display and indentation.
    • Added ref attribute for improved manipulation of tree node content.
  • Bug Fixes

    • Improved click handling to prevent parent node collapse when clicking on whitespace.
    • Refined checkbox click behavior to allow event propagation.
  • Chores

    • Updated version number of the @opentiny/vue-tree package from 3.19.1 to 3.19.2.

@gimmyhehe gimmyhehe added the bug Something isn't working label Nov 8, 2024
Copy link

coderabbitai bot commented Nov 8, 2024

Walkthrough

The pull request introduces several modifications to the tree component's functionality, primarily in the packages/renderless/src/tree-node/index.ts file. Key changes include the addition of two functions for managing icons and indentation, updates to click handling logic to prevent unintended collapses, and refinements in event handling for state changes. The package.json file reflects a version update, while the TreeNode component in the Vue file gains a new reference for the content section, impacting event propagation.

Changes

File Path Change Summary
packages/renderless/src/tree-node/index.ts - Added computedExpandIcon and computedIndent functions.
- Updated handleClick to prevent parent collapse on whitespace clicks.
- Refined handleCheckChange for state management.
- Enhanced handleContextMenu for better visibility management.
packages/vue/src/tree/package.json - Updated version from 3.19.1 to 3.19.2.
packages/vue/src/tree/src/tree-node.vue - Added ref="content" to the content div.
- Removed @click.stop from tiny-checkbox.

Assessment against linked issues

Objective Addressed Explanation
Cancel event trigger on whitespace clicks to avoid unintended collapses (2491)

Possibly related PRs

Suggested reviewers

  • zzcr

🐰 In the tree where the branches sway,
A click on the whitespace won't lead you astray.
Icons now shine, and nodes stand tall,
With each little change, we’re having a ball!
So hop along, dear friends, don’t delay,
The tree's now a joy in every way! 🌳✨


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

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

@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/renderless/src/tree-node/index.ts (3)

89-94: Add null check for event target.

The implementation effectively prevents unintended parent node collapse when clicking whitespace. However, consider adding a null check for the event target to improve robustness.

-    if (contentElm && !contentElm.contains(e?.target)) {
+    if (contentElm && e?.target && !contentElm.contains(e.target)) {

Line range hint 441-456: Consider improvements to icon configuration.

The implementation is solid but could benefit from these enhancements:

  1. Extract icon names as constants to improve maintainability
  2. Translate Chinese comments to English for better international collaboration
+const ICON_NAMES = {
+  DEFAULT: 'icon-chevron-right',
+  EXPANDED: 'icon-expand',
+  COLLAPSE: 'icon-put-away'
+}

 export const computedExpandIcon =
   ({ designConfig }) =>
   (treeRoot, state) => {
     if (state.tree.icon) {
       return state.tree.icon
     }
 
-    // tiny 新增的判断。 显示线时强制切换图标,仅smb定制了
+    // Force icon switch when showing lines (SMB customization only)
     if (treeRoot.showLine) {
-      const expandIcon = designConfig?.icons?.expanded || 'icon-expand'
-      const collapseIcon = designConfig?.icons?.collapse || 'icon-put-away'
+      const expandIcon = designConfig?.icons?.expanded || ICON_NAMES.EXPANDED
+      const collapseIcon = designConfig?.icons?.collapse || ICON_NAMES.COLLAPSE
       return state.expanded ? expandIcon : collapseIcon
     }
 
-    return 'icon-chevron-right'
+    return ICON_NAMES.DEFAULT
   }

Line range hint 457-461: Enhance indentation calculation robustness.

The implementation works but could benefit from these improvements:

  1. Add radix to parseInt
  2. Validate for negative values
  3. Use template literals for better readability
 export const computedIndent =
   () =>
   ({ node, showLine }, { tree }) => {
-    return (node.level > 1 ? 1 : 0) * (parseInt(tree.indent) + (showLine ? 8 : 0)) + parseInt(tree.baseIndent) + 'px'
+    const level = node.level > 1 ? 1 : 0;
+    const indent = Math.max(0, parseInt(tree.indent, 10));
+    const baseIndent = Math.max(0, parseInt(tree.baseIndent, 10));
+    const lineSpacing = showLine ? 8 : 0;
+    return `${level * (indent + lineSpacing) + baseIndent}px`;
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8eb46c5 and f78a509.

📒 Files selected for processing (3)
  • packages/renderless/src/tree-node/index.ts (1 hunks)
  • packages/vue/src/tree/package.json (1 hunks)
  • packages/vue/src/tree/src/tree-node.vue (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/vue/src/tree/package.json
🔇 Additional comments (2)
packages/vue/src/tree/src/tree-node.vue (2)

50-50: LGTM: Content ref addition helps fix margin click issues.

Adding the content ref enables precise click target detection, which aligns with the PR objective of preventing unintended parent node toggles when clicking child node margins.


Line range hint 94-102: Verify checkbox click behavior after removing @click.stop.

The removal of @click.stop from the checkbox allows event propagation. While this change is part of fixing the margin click issues, we should verify that:

  1. Checkbox clicks still work as expected
  2. Checkbox clicks don't trigger unintended parent node toggles

Let's check for any similar click.stop patterns in the codebase:

✅ Verification successful

Based on the search results, I can now make a determination about the checkbox click.stop removal:

Removal of @click.stop from checkbox is safe and intentional

The codebase search reveals:

  1. The checkbox click handling is properly managed through the component's internal logic in packages/vue/src/tree/src/tree-node.vue and related renderless code.

  2. The parent node click handling is separately controlled by @click.stop="handleClick" on the tree node content wrapper, which prevents event bubbling for the node click itself.

  3. The comment "tiny: 去掉 @click.stop的绑定" indicates this was an intentional change to fix click handling behavior.

  4. The checkbox component has its own click handling that manages propagation appropriately through its renderless architecture, making the extra @click.stop unnecessary and potentially problematic for proper event handling.

The removal aligns with Vue's event handling best practices by allowing natural event propagation while still maintaining proper component-level click handling through the checkbox's internal logic.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for similar click.stop patterns in tree components
# This helps understand the event handling patterns and potential impacts

# Search for click.stop patterns in tree-related files
echo "Searching for click.stop patterns in tree components..."
rg "@click\.stop" --type vue

# Search for checkbox usage in tree components
echo "Searching for checkbox usage in tree components..."
rg "tiny-checkbox" --type vue -A 5

Length of output: 353


Script:

#!/bin/bash
# Let's try without file type filtering since vue type is not recognized
# Search for click.stop patterns
echo "Searching for click.stop patterns..."
rg "@click\.stop"

# Search for checkbox usage and its event handling
echo "Searching for checkbox usage..."
rg "tiny-checkbox" -A 5

# Search for tree-node related click handlers
echo "Searching for tree node click handlers..."
rg "handleClick|toggleExpand|toggleCheck" -A 3

Length of output: 282229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants