Skip to content

fix: alert beforeClose callback arguments fixed #5845

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

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

mynetfan
Copy link
Collaborator

@mynetfan mynetfan commented Apr 1, 2025

Description

修复beforeClose在点击关闭按钮时和点击取消按钮时的逻辑不一致的问题;
修改beforeClose的参数,传入isConfirm和value

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Checklist

ℹ️ Check all checkboxes - this will indicate that you have done everything in accordance with the rules in CONTRIBUTING.

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs:dev command.
  • Run the tests with pnpm test.
  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

  • New Features
    • Alert and prompt dialogs now offer improved control over confirmation and cancellation actions. User interactions are refined so that dialog closures and input validation occur only when appropriate confirmation and valid input are provided, leading to a more intuitive and reliable experience.

@mynetfan mynetfan requested review from anncwb, vince292007, jinmao88 and a team as code owners April 1, 2025 12:32
Copy link

changeset-bot bot commented Apr 1, 2025

⚠️ No Changeset found

Latest commit: 7960923

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Apr 1, 2025

Walkthrough

The changes enhance the beforeClose callback by introducing a new type, BeforeCloseScope, which carries an isConfirm boolean and, in some cases, a value property. Function signatures in alert components, prompt functions, and related view methods are updated to accept this richer context. Component logic in the Vue templates is modified to utilize this context when handling cancel and open-change events.

Changes

Files Change Summary
docs/src/.../vben-alert.md
packages/@core/ui-kit/.../alert.ts
packages/@core/ui-kit/.../AlertBuilder.ts
Added new BeforeCloseScope type with an isConfirm property and updated the beforeClose callback signatures in AlertProps and prompt options to accept an object with additional context.
packages/@core/ui-kit/.../alert.vue
playground/src/.../index.vue
Modified component behavior: updated handleCancel and open-change methods to pass the isConfirm state (and input value for prompt) to the beforeClose callback and adjusted control flow based on its response.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant M as Modal Component
    participant B as beforeClose Callback
    U->>M: Trigger cancel/confirm action
    M->>B: Call beforeClose({ isConfirm: true/false, value?: input })
    B-->>M: Return boolean decision
    alt Close confirmed
        M->>M: Close modal dialog
    else
        M->>M: Remain open / show error
    end
Loading
sequenceDiagram
    participant U as User
    participant P as Prompt Component
    participant B as beforeClose Handler
    U->>P: Enters input & clicks confirm
    P->>B: Invoke beforeClose({ isConfirm: true, value: userInput })
    B-->>P: Return validation result
    alt Valid input and confirmation
        P->>P: Close prompt
    else
        P->>P: Display error, keep open
    end
Loading

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • anncwb
  • vince292007

Poem

I'm a rabbit with hops so light,
Celebrating code changes bright as daylight.
With a twitch of my nose, new types now play,
isConfirm guides our steps along the way.
In a meadow of logic, I cheer with delight!
🐇✨ Happy coding and carrot bites!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d9f1be and 7960923.

📒 Files selected for processing (5)
  • docs/src/components/common-ui/vben-alert.md (3 hunks)
  • packages/@core/ui-kit/popup-ui/src/alert/AlertBuilder.ts (3 hunks)
  • packages/@core/ui-kit/popup-ui/src/alert/alert.ts (1 hunks)
  • packages/@core/ui-kit/popup-ui/src/alert/alert.vue (3 hunks)
  • playground/src/views/examples/modal/index.vue (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/@core/ui-kit/popup-ui/src/alert/AlertBuilder.ts (1)
packages/@core/ui-kit/popup-ui/src/alert/alert.ts (1)
  • BeforeCloseScope (5-7)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Check (windows-latest)
  • GitHub Check: Lint (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)
  • GitHub Check: post-update (windows-latest)
🔇 Additional comments (15)
playground/src/views/examples/modal/index.vue (2)

132-133: Improved logic in beforeClose callback.

The update to have the beforeClose callback accept an object with the isConfirm property, combined with the early return when not confirmed, is a good improvement. This ensures that the asynchronous operations only execute when users confirm the action, not when they cancel it.


154-155: Enhanced input validation with confirmation context.

Good refinement of the validation logic. Now the error message only appears when the user tries to confirm with an invalid value ('芝士'), not when they cancel. This creates a better user experience by avoiding unnecessary validation messages when the user is canceling.

packages/@core/ui-kit/popup-ui/src/alert/alert.ts (2)

5-7: Good addition of a formal type for the callback scope.

Creating the BeforeCloseScope type is a good practice for improving type safety and documentation. This type clearly communicates the data structure passed to the beforeClose callback.


11-13: Type signature correctly updated for the beforeClose callback.

The updated signature properly accepts the BeforeCloseScope parameter, ensuring type safety and making it clear to developers what data is available in the callback.

packages/@core/ui-kit/popup-ui/src/alert/alert.vue (5)

96-98: Improved cancel handling with confirmation state.

The handleCancel function now correctly sets isConfirm to false instead of directly modifying open.value. This allows the component to properly track the cancellation state, which is then used in the beforeClose callback.


105-105: Correct usage of confirmation state in beforeClose callback.

The function now passes the current confirmation state to the beforeClose callback, providing the callback with the necessary context to determine whether to proceed with closing the alert.


146-146: Cancel button now correctly triggers the cancellation handler.

The X button (close icon) now properly triggers the handleCancel function, ensuring consistent behavior with other cancellation methods.


160-165: Cancel action consistent with cancellation handler.

The AlertDialogCancel component now properly uses the handleCancel function, ensuring consistent behavior across all cancellation methods.


169-174: Confirm action properly triggers confirmation state.

The AlertDialogAction now correctly triggers the handleConfirm function, which sets the confirmation state to true, ensuring proper behavior differentiation between confirm and cancel actions.

docs/src/components/common-ui/vben-alert.md (3)

41-44: Well-documented new type in the documentation.

The BeforeCloseScope type is properly added to the documentation with a helpful comment explaining that isConfirm indicates whether the close action was triggered by a confirmation button click.


48-50: Updated callback signature in documentation.

The documentation for the beforeClose callback's parameter is correctly updated to reflect the code changes, ensuring users have accurate information.


99-104: Comprehensive documentation for prompt function parameters.

The documentation for the beforeClose callback's parameter in the prompt function is now more detailed, correctly showing that it receives both the confirmation state and the input value. This is particularly important for the prompt component where input validation is common.

packages/@core/ui-kit/popup-ui/src/alert/AlertBuilder.ts (3)

5-5: Import of BeforeCloseScope type adds needed context

The addition of the BeforeCloseScope import reflects the enhanced type safety in the alert callbacks, providing better context for the closing actions.


134-137: Enhanced beforeClose callback signature improves consistency

This change directly addresses the PR objective by enhancing the beforeClose callback to include both the confirmation state (isConfirm) and the value being processed. This provides important context that was previously inconsistently available depending on how the alert was closed.


169-174: Implementation properly passes context to delegated beforeClose

The updated implementation correctly passes both the original BeforeCloseScope (which contains isConfirm) along with the current modelValue.value to the delegated beforeClose function. This ensures consistent behavior regardless of how the alert is closed (via close button or cancel button), fixing the inconsistency mentioned in the PR objective.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@mynetfan mynetfan merged commit ecf518b into vbenjs:main Apr 1, 2025
14 checks passed
@mynetfan mynetfan deleted the fix/alert branch April 1, 2025 14:55
caszhou pushed a commit to YueChengMinJie/vue-vben-admin that referenced this pull request Apr 2, 2025
qianYuanJ added a commit to qianYuanJ/vue-vben-admin that referenced this pull request Apr 2, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant