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

types: 为useVbenVxeGrid添加泛型声明,使grid实例上能正确获取到行数据的类型 #5653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RanMaoting
Copy link

@RanMaoting RanMaoting commented Mar 5, 2025

Description

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

    • Enhanced grid configuration options with improved type safety. Users can now customize grid behavior more precisely through explicit type parameters for diverse data structures and form interactions.
  • Refactor

    • Streamlined grid API interfaces for consistent and reliable grid interactions, leading to a more robust and flexible user experience.

Copy link

changeset-bot bot commented Mar 5, 2025

⚠️ No Changeset found

Latest commit: 9942f73

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 Mar 5, 2025

Walkthrough

The changes introduce generic type parameters to enhance type safety and flexibility in the grid API, interfaces, and hook functions. Updates include modifying class signatures, property types, method signatures, and interface definitions to accept customizable types. Additionally, a new adapter wrapper is provided to re-export the grid hook with improved type inference, and playground examples are updated to explicitly specify type parameters.

Changes

Files Change Summary
packages/effects/plugins/src/vxe-table/api.ts, packages/effects/plugins/src/vxe-table/types.ts, packages/effects/plugins/src/vxe-table/use-vxe-grid.ts Added generic type parameters (e.g., <T>, <T, D>) to class declarations, interfaces, and methods for improved type safety in grid and form configurations.
playground/src/adapter/vxe-table.ts Introduced a new type import for ComponentType and wrapped the original grid hook (useGrid) to export a type-safe version of useVbenVxeGrid.
playground/src/views/examples/vxe-table/basic.vue Updated the function call for useVbenVxeGrid to include an explicit type parameter <RowType> and added commented hints for additional configuration and inspection.

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • vince292007
  • anncwb

Poem

I'm a little rabbit in the code,
Hopping through types on a winding road.
With generics I now skip and play,
Making grid safety bright as day.
Code improvements make my ears stand tall—
A joyful leap for one and all!
🐇✨

✨ 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 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: 0

🧹 Nitpick comments (1)
playground/src/views/examples/vxe-table/basic.vue (1)

47-55: Helpful commented code example for form schema

The commented form configuration serves as a useful reference for developers who might need to implement form functionality in the future. Consider adding a brief documentation comment explaining when this form configuration would be useful.

-  // 放开注释查看表单组件的类型
+  // 放开注释启用表单组件并查看其类型。适用于需要在表格上方添加搜索或筛选表单的场景。
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3478964 and 9942f73.

📒 Files selected for processing (5)
  • packages/effects/plugins/src/vxe-table/api.ts (2 hunks)
  • packages/effects/plugins/src/vxe-table/types.ts (3 hunks)
  • packages/effects/plugins/src/vxe-table/use-vxe-grid.ts (2 hunks)
  • playground/src/adapter/vxe-table.ts (2 hunks)
  • playground/src/views/examples/vxe-table/basic.vue (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: post-update (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)
🔇 Additional comments (19)
playground/src/views/examples/vxe-table/basic.vue (2)

46-58: Great addition of generic type parameter for improved type safety!

The use of the generic type parameter <RowType> with useVbenVxeGrid now provides proper type inference for grid row data. This enhancement ensures that operations on grid rows will have proper type checking, making the code safer and enabling better IDE auto-completion.


60-61: Useful debugging comment for type inspection

This comment provides valuable guidance for developers who need to inspect the table instance's type structure. This is particularly helpful for understanding the available properties and methods on the grid instance when working with TypeScript.

playground/src/adapter/vxe-table.ts (3)

3-3: Good addition of ComponentType import

Adding the ComponentType import is necessary for the generic type implementation that follows. This creates a clean separation between component types and data types in the grid implementation.


9-12: Clean import renaming for implementation clarity

Renaming the imported function to useGrid while exporting a wrapper as useVbenVxeGrid creates a clear distinction between the core implementation and the adapter. This is a good practice for maintaining a clean API surface.


237-239: Well-implemented generic wrapper function

This type-safe wrapper implementation is excellent:

  1. It correctly constrains T to Record<string, any> which is appropriate for row data
  2. It uses Parameters<typeof useGrid<T, ComponentType>> to ensure parameter compatibility
  3. It correctly forwards all parameters to the underlying function

This pattern allows consumers to specify row types while maintaining a consistent form component type.

packages/effects/plugins/src/vxe-table/use-vxe-grid.ts (4)

1-2: Good import of BaseFormComponentType

Adding the import for BaseFormComponentType is necessary for the generic implementation. This type will be used as a constraint and default for the form component type parameter.


12-15: Well-structured generic type parameters

The implementation of generic type parameters is well done:

  1. T extends Record<string, any> = any provides proper constraint with backward compatibility
  2. D extends BaseFormComponentType = BaseFormComponentType offers flexibility for form component types
  3. The parameter options: VxeGridProps<T, D> correctly uses both type parameters

This structure allows for flexible typing while maintaining backward compatibility.


18-18: Properly typed extendedApi

The extendedApi is correctly typed as ExtendedVxeGridApi<T, D>, ensuring that the generic parameters are properly propagated through the API chain. This maintains type safety throughout the codebase.


24-24: Correct typing for props parameter

The props parameter in the defineComponent function is properly typed as VxeGridProps<T>, ensuring that the component receives correctly typed props. This completes the type safety chain from function call to component rendering.

packages/effects/plugins/src/vxe-table/api.ts (3)

29-29: Good implementation of generic type parameter for VxeGridApi

Adding the generic type parameter <T extends Record<string, any> = any> to the VxeGridApi class is a key enhancement that enables proper type inference throughout the grid API. The constraint and default value ensure backward compatibility.


33-36: Proper typing of class properties

The updates to class property types are correct:

  1. grid as VxeGridInstance<T>
  2. state as null | VxeGridProps<T>
  3. store as Store<VxeGridProps<T>>

These changes ensure that the row data type information flows through all aspects of the grid instance.


101-104: Correctly updated setState method parameter type

The setState method's parameter type has been properly updated to use the generic type parameter:

((prev: VxeGridProps<T>) => Partial<VxeGridProps<T>>) | Partial<VxeGridProps<T>>

This ensures type consistency when updating grid state, preventing potential type errors.

packages/effects/plugins/src/vxe-table/types.ts (7)

12-12: Updated import to include BaseFormComponentType.

The import now correctly includes BaseFormComponentType, which is necessary for the generic type constraints being added to the interfaces.


34-37: Good enhancement - generic type parameters improve type safety.

Adding generic parameters to VxeGridProps is a good improvement that allows consumers to specify the row data type (T) and form component type (D). The constraints are appropriate - row data extends Record<string, any> and form components extend BaseFormComponentType.


57-57: Correctly updated gridOptions to use generic type.

The gridOptions property now correctly uses the generic type parameter T, which will allow for proper type inference when accessing grid data.


61-61: Correctly updated gridEvents to use generic type.

The gridEvents property now correctly uses the generic type parameter T, ensuring event handlers receive properly typed data.


65-65: Correctly updated formOptions to use generic type.

The formOptions property now properly uses the generic type parameter D, which improves type safety for form components.


29-32:

✅ Verification successful

Verify all uses of VxeTableGridOptions are updated consistently.

The VxeTableGridOptions interface is now using a generic parameter <T = any>. Since you're updating the VxeGridProps interface to use this generic, ensure that all other places using VxeTableGridOptions are consistently updated throughout the codebase.


🏁 Script executed:

#!/bin/bash
# Search for all uses of VxeTableGridOptions to ensure consistent generic usage
echo "Searching for VxeTableGridOptions usage..."
rg -t ts "VxeTableGridOptions" -A 1 -B 1

Length of output: 1754


Generic Parameter Usage Verified

After verifying the codebase, all references to VxeTableGridOptions now consistently use the generic parameter as defined in packages/effects/plugins/src/vxe-table/types.ts. In particular:

  • The interface in packages/effects/plugins/src/vxe-table/types.ts is updated with <T = any>.
  • The re-export in packages/effects/plugins/src/vxe-table/index.ts correctly reflects this change.
  • Usages in playground/src/views/system/dept/data.ts and playground/src/views/system/menu/data.ts explicitly pass the expected generic, ensuring consistency.

No further changes are required.


72-78:

❓ Verification inconclusive

Verify that NoInfer utility type is defined.

The ExtendedVxeGridApi now uses generic parameters to provide better type inference. However, the code uses a NoInfer<T> utility type that doesn't appear to be imported in this file. This could lead to TypeScript errors if the type isn't defined elsewhere.

Also, in the useStore function, you've used VxeGridProps<any, any> for the state parameter but more specific types VxeGridProps<D, F> for the return type inference. Consider using consistent typing:

-  useStore: <T = NoInfer<VxeGridProps<D, F>>>(
-    selector?: (state: NoInfer<VxeGridProps<any, any>>) => T,
+  useStore: <T = NoInfer<VxeGridProps<D, F>>>(
+    selector?: (state: NoInfer<VxeGridProps<D, F>>) => T,

🏁 Script executed:

#!/bin/bash
# Search for NoInfer type definition in the codebase
echo "Searching for NoInfer type definition..."
rg -t ts "type\s+NoInfer" --no-filename
rg -t ts "interface\s+NoInfer" --no-filename

Length of output: 179


Action Required: Verify inclusion of NoInfer and align typing in useStore

  • The search for the NoInfer utility type yielded no results. Please double-check whether NoInfer is defined in a global declaration file (such as in a global.d.ts), imported from an external library, or if it needs to be added.
  • In the useStore function, the parameter for selector currently uses NoInfer<VxeGridProps<any, any>> while the default generic is based on NoInfer<VxeGridProps<D, F>>. For consistency and better type safety, update the selector function’s parameter type accordingly.
-  useStore: <T = NoInfer<VxeGridProps<D, F>>>(
-    selector?: (state: NoInfer<VxeGridProps<any, any>>) => T,
+  useStore: <T = NoInfer<VxeGridProps<D, F>>>(
+    selector?: (state: NoInfer<VxeGridProps<D, F>>) => T,

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