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

perf: modal and drawer api support chain calls #5351

Merged
merged 2 commits into from
Jan 11, 2025

Conversation

mynetfan
Copy link
Collaborator

@mynetfan mynetfan commented Jan 11, 2025

Description

Modal 和 Drawer 的部分API支持链式调用。

以前:

modalApi.setData({a:1});
modalApi.open();

现在:

modalApi.setData({a:1}).open();

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

  • Documentation

    • Updated documentation for Vben Drawer and Modal components.
    • Renamed modalApi section to drawerApi.
    • Improved method signatures for setState and setData.
  • Improvements

    • Enhanced method chaining for Drawer and Modal APIs.
    • Simplified function implementations in demo components.
    • Improved type definitions and API clarity.
  • Code Refactoring

    • Modified API methods to return this for fluent interface.
    • Consolidated method calls in various components.

@mynetfan mynetfan requested review from anncwb, vince292007 and a team as code owners January 11, 2025 02:36
Copy link

changeset-bot bot commented Jan 11, 2025

⚠️ No Changeset found

Latest commit: 3817d5a

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 Jan 11, 2025

Walkthrough

This pull request introduces method chaining capabilities to the DrawerApi and ModalApi classes across multiple files. The changes primarily focus on updating method signatures in the API classes to return this, allowing developers to chain method calls like setState().open(). Documentation for both drawer and modal components has been updated to reflect these changes, and various example components have been refactored to utilize the new chaining syntax. The modifications enhance code readability and provide a more fluent interface for managing drawer and modal states.

Changes

File Change Summary
docs/src/components/common-ui/vben-drawer.md Renamed modalApi to drawerApi, updated method signatures for setState and setData
docs/src/components/common-ui/vben-modal.md Updated method signatures for setState and setData, refined documentation
packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts Added return this to setData and setState methods
packages/@core/ui-kit/popup-ui/src/modal/modal-api.ts Added return this to setData and setState methods
docs/src/demos/vben-*/*.vue Refactored method calls to use chained syntax
playground/src/views/examples/drawer/index.vue Added DrawerState import, refactored drawer opening methods
playground/src/views/examples/modal/index.vue Refactored modal opening methods to use chained syntax

Sequence Diagram

sequenceDiagram
    participant Dev as Developer
    participant API as DrawerApi/ModalApi
    participant UI as User Interface

    Dev->>API: setState({ title: 'New Title' })
    Dev->>API: open()
    
    Note over Dev,API: New Approach
    Dev->>API: setState({ title: 'New Title' }).open()
    
    API->>UI: Update and Display
Loading

Possibly related PRs

Suggested labels

feature

Suggested reviewers

  • anncwb
  • vince292007

Poem

🐰 Chaining methods with grace and might,
Our APIs now dance so light!
From setState to open, one smooth line,
Code flows like water, simply divine!
Rabbit's magic makes coding bright! 🌟


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.

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
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: 1

🧹 Nitpick comments (5)
docs/src/demos/vben-drawer/dynamic/index.vue (1)

16-16: LGTM! Consider documenting setState chaining.

The implementation correctly extends chaining to setState, showing good API consistency. Consider updating the documentation to explicitly mention that both setData and setState support chaining.

docs/src/demos/vben-modal/dynamic/index.vue (1)

16-16: LGTM! Consider documenting the API symmetry.

The implementation maintains perfect symmetry between modal and drawer APIs, which is excellent for developer experience.

Consider documenting the intentional API symmetry between modal and drawer components in the architecture documentation. This pattern makes the API more intuitive and easier to learn.

packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts (1)

144-144: LGTM! Method chaining support added correctly.

The implementation properly returns this from both setData and setState methods, enabling fluent method chaining while maintaining existing functionality.

Consider adding JSDoc comments to document the chainable nature of these methods, for example:

+ /**
+  * Sets the shared data payload
+  * @returns this - Returns the instance for method chaining
+  */
  setData<T>(payload: T) {

Also applies to: 157-157

packages/@core/ui-kit/popup-ui/src/modal/modal-api.ts (1)

154-154: LGTM! Consistent implementation with DrawerApi.

The changes mirror the DrawerApi implementation, maintaining consistency across the codebase.

Consider adding JSDoc comments similar to DrawerApi to maintain documentation consistency:

+ /**
+  * Sets the shared data payload
+  * @returns this - Returns the instance for method chaining
+  */
  setData<T>(payload: T) {

Also applies to: 167-167

playground/src/views/examples/modal/index.vue (1)

66-71: LGTM! Effective use of method chaining.

The implementation properly demonstrates the new chaining capability while maintaining code readability.

Consider consistent formatting for chained calls. For single-line chains like handleUpdateTitle, the current format is good. For multi-line chains, consider this style for better readability:

sharedModalApi
  .setData({
    content: '外部传递的数据 content',
    payload: '外部传递的数据 payload',
  })
  .open();

Also applies to: 75-75, 79-84

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8bffd8 and c1cfb9e.

📒 Files selected for processing (10)
  • docs/src/components/common-ui/vben-drawer.md (1 hunks)
  • docs/src/components/common-ui/vben-modal.md (1 hunks)
  • docs/src/demos/vben-drawer/dynamic/index.vue (1 hunks)
  • docs/src/demos/vben-drawer/shared-data/index.vue (1 hunks)
  • docs/src/demos/vben-modal/dynamic/index.vue (1 hunks)
  • docs/src/demos/vben-modal/shared-data/index.vue (1 hunks)
  • packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts (2 hunks)
  • packages/@core/ui-kit/popup-ui/src/modal/modal-api.ts (2 hunks)
  • playground/src/views/examples/drawer/index.vue (3 hunks)
  • playground/src/views/examples/modal/index.vue (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Test (windows-latest)
  • GitHub Check: Test (ubuntu-latest)
  • GitHub Check: Check (windows-latest)
  • GitHub Check: Lint (windows-latest)
  • GitHub Check: Check (ubuntu-latest)
  • GitHub Check: post-update (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Lint (ubuntu-latest)
🔇 Additional comments (6)
docs/src/demos/vben-drawer/shared-data/index.vue (1)

12-17: LGTM! Verify TypeScript return types.

The chaining implementation looks good and matches the PR objectives. The code is more concise than using separate calls.

Let's verify the TypeScript types are properly updated to support chaining:

✅ Verification successful

TypeScript types verified and correctly support chaining

The setData method is properly typed with return this enabling method chaining, and uses generic type parameter <T> for type-safe data passing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for DrawerApi class definition and method signatures
ast-grep --pattern 'class DrawerApi {
  $$$
  setData($_) {
    $$$
  }
  $$$
}'

Length of output: 82


Script:

#!/bin/bash
# Try to find drawer-related type definitions and implementations
rg -t typescript -t vue "interface.*Drawer|class.*Drawer|type.*Drawer" -l

# Search for setData method definitions
rg -t typescript -t vue "setData.*\(" -A 3

# Find drawer-related files
fd -e ts -e tsx -e vue drawer

Length of output: 830


Script:

#!/bin/bash
# Check drawer-api.ts content
cat packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts

# Backup search for setData implementation
rg "setData" packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts -A 5

Length of output: 3353

docs/src/demos/vben-modal/shared-data/index.vue (1)

12-17: LGTM! Verify TypeScript return types.

The modal implementation maintains consistency with the drawer pattern, showing good API symmetry.

Let's verify the TypeScript types are properly updated:

✅ Verification successful

TypeScript types verified and correct

The setData method is properly typed with generic parameter <T> and returns this for method chaining, matching the drawer implementation pattern. Implementation is verified in packages/@core/ui-kit/popup-ui/src/modal/modal-api.ts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for ModalApi class definition and method signatures
ast-grep --pattern 'class ModalApi {
  $$$
  setData($_) {
    $$$
  }
  $$$
}'

Length of output: 81


Script:

#!/bin/bash
# First, find relevant modal type definition files
rg -l "type.*Modal|interface.*Modal|class.*Modal" --type=typescript

# Then search for setData method signature
rg "setData.*\(" --type=typescript -A 2

Length of output: 181


Script:

#!/bin/bash
# Search in .ts and .tsx files for Modal-related definitions
rg -g "*.{ts,tsx}" "type.*Modal|interface.*Modal|class.*Modal"

# Search for setData method implementations
rg -g "*.{ts,tsx}" "setData.*\(" -A 2

Length of output: 2116

playground/src/views/examples/drawer/index.vue (3)

2-2: LGTM! Good type safety with explicit DrawerState import.

The addition of the DrawerState type import improves type safety and code clarity.


49-54: Well-structured conditional state handling.

Good practice of creating a state object and conditionally modifying it before applying. The comment explaining the zIndex logic is particularly helpful.


75-80: LGTM! Consistent method chaining implementation.

The implementation follows the same pattern as the modal examples, maintaining consistency across the codebase.

Also applies to: 84-89

docs/src/components/common-ui/vben-drawer.md (1)

140-140: LGTM! Method signatures updated to support chaining

The updated return types for setState and setData methods now correctly reflect the chaining capability, aligning with the PR's objective to support fluent API calls like setData({...}).open().

Also applies to: 143-143

docs/src/components/common-ui/vben-modal.md Outdated Show resolved Hide resolved
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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c1cfb9e and 3817d5a.

📒 Files selected for processing (1)
  • docs/src/components/common-ui/vben-modal.md (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Test (windows-latest)
  • GitHub Check: Lint (windows-latest)
  • GitHub Check: Test (ubuntu-latest)
  • GitHub Check: Check (windows-latest)
  • GitHub Check: Lint (ubuntu-latest)
  • GitHub Check: Check (ubuntu-latest)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: post-update (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)

docs/src/components/common-ui/vben-modal.md Show resolved Hide resolved
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.

1 participant