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: form 表单不支持field.xxx.xx格式的defaultValue配置 #4965

Merged
merged 4 commits into from
Dec 1, 2024

Conversation

leizhiyou
Copy link
Contributor

@leizhiyou leizhiyou commented Nov 27, 2024

Description

Form 表单useVbenForm 无法设置表单字段 field.xx.xx 的defaultValue的默认值配置

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

Release Notes

  • New Features

    • Introduced a new input field in the form for capturing nested property paths.
    • Added the set function from lodash to enhance utility functions for manipulating object properties.
  • Bug Fixes

    • Improved handling of initial values in forms, allowing for more flexible assignments to nested properties.
  • Chores

    • Updated package configurations to include new lodash dependencies for enhanced type support.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

The pull request introduces several modifications to enhance the @vben-core/shared package and related components. Key changes include adding the lodash.set dependency and its type definitions, which facilitate improved handling of nested properties in forms. The set function is now exported from the utilities module, and its usage is integrated into the form context for generating initial values. Additionally, a new input field is added to the form schema, allowing for the input of nested property paths.

Changes

File Path Change Summary
packages/@core/base/shared/package.json - Added dependency: "lodash.set"
- Added dev dependency: "@types/lodash.set"
- Updated publishConfig for type definitions.
packages/@core/base/shared/src/utils/index.ts - Added export: export { default as set } from 'lodash.set';
packages/@core/ui-kit/form-ui/src/use-form-context.ts - Updated import to include set from @vben-core/shared/utils.
- Modified generateInitialValues to use set for assigning initial values.
playground/src/views/examples/form/basic.vue - Added new input field to CustomLayoutForm schema: fieldName: 'field4.path', defaultValue: 'field4.path', label: 'field4.path'.
pnpm-workspace.yaml - Added type definition: @types/lodash.set with version ^4.3.9
- Added package: lodash.set with version ^4.3.2.

Possibly related PRs

Suggested reviewers

  • vince292007
  • anncwb

🐰 In the meadow, changes bloom,
New paths for forms, dispelling gloom.
With lodash's help, values set right,
Nested treasures now shine bright!
A field added here, a function there,
In our code garden, we show we care! 🌼


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

🧹 Outside diff range and nitpick comments (3)
packages/@core/base/shared/src/utils/index.ts (1)

18-18: Consider adding JSDoc documentation for the new export.

To maintain consistency and improve maintainability, consider adding documentation for the new export.

+/** Safely sets a value at path of object. If a portion of path doesn't exist, it's created. */
 export { default as set } from 'lodash.set';
packages/@core/ui-kit/form-ui/src/use-form-context.ts (1)

44-46: Consider documenting nested field support

While the implementation is solid, consider:

  1. Adding JSDoc comments explaining the support for nested paths
  2. Updating form documentation to highlight this feature
  3. Reviewing other form features (like validation rules, transformers) to ensure consistent support for nested paths
playground/src/views/examples/form/basic.vue (1)

294-299: Consider enhancing the layout and example.

The new field successfully demonstrates the fix for supporting field.xxx.xx format. However, consider these improvements:

  1. Add formItemClass to maintain consistent layout with other fields
  2. Consider a more comprehensive example with actual nested object values
     {
       component: 'Input',
       defaultValue: 'field4.path',
       fieldName: 'field4.path',
       label: 'field4.path',
+      formItemClass: 'col-start-1',
     },

Additionally, consider adding another example with deeper nesting and actual object values:

+    {
+      component: 'Input',
+      defaultValue: { nested: { value: 'test' } },
+      fieldName: 'field5.nested.value',
+      label: 'Nested Object Example',
+      formItemClass: 'col-start-2',
+    },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 54a9ff0 and 6dc9f22.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • packages/@core/base/shared/package.json (1 hunks)
  • packages/@core/base/shared/src/utils/index.ts (1 hunks)
  • packages/@core/ui-kit/form-ui/src/use-form-context.ts (2 hunks)
  • playground/src/views/examples/form/basic.vue (1 hunks)
  • pnpm-workspace.yaml (2 hunks)
🔇 Additional comments (5)
packages/@core/base/shared/src/utils/index.ts (1)

18-18: LGTM! Verify type definitions are properly configured.

The addition of lodash.set is appropriate for handling nested property paths in form configurations. This aligns well with the PR objective to support field.xxx.xx format.

Let's verify the type definitions are properly configured:

✅ Verification successful

Type definitions are properly configured - good to go!

The verification shows that:

  • lodash.set is correctly added as a dependency in the shared package
  • @types/lodash.set is properly configured in devDependencies
  • The package is only imported in the utils/index.ts file as intended
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if lodash.set types are properly configured in package.json
# Expected: Should find @types/lodash.set in devDependencies

# Search for package.json files that might contain lodash.set configuration
fd package.json | while read -r file; do
  echo "Checking $file:"
  cat "$file" | jq -r '.dependencies["lodash.set"] // empty, .devDependencies["@types/lodash.set"] // empty'
done

Length of output: 10881


Script:

#!/bin/bash
# Let's check the root package.json and the shared package's package.json more thoroughly
echo "=== Root package.json ==="
cat package.json | jq '.dependencies,.devDependencies' 

echo -e "\n=== Shared package's package.json ==="
cat packages/@core/base/shared/package.json | jq '.dependencies,.devDependencies'

# Also check if lodash.set is already being used somewhere
echo -e "\n=== Files using lodash.set ==="
rg "lodash\.set" --type ts

Length of output: 1962

packages/@core/ui-kit/form-ui/src/use-form-context.ts (2)

6-6: LGTM: Import of set utility is appropriate

The addition of set from shared utils (backed by lodash.set) is a good choice for handling nested object properties in form fields.


44-46: Implementation correctly supports nested field paths

The use of set utility properly handles both simple and nested property paths in form field names, directly addressing the PR objective.

Let's verify the behavior with complex nested paths:

✅ Verification successful

Implementation correctly supports nested field paths

The verification confirms that nested field paths are properly supported:

  • Found example usage in playground with field4.path demonstrating nested path support
  • The set utility is correctly used in use-form-context.ts for both initialValues and zodObject
  • The form API implementation (form-api.ts) includes proper handling of nested fields through:
    • Value setting with deep merge support
    • Field validation
    • Form submission with nested structures
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing usage patterns of nested form fields to ensure compatibility

# Look for form schema definitions with nested paths
rg -A 5 "fieldName: ['\"].*\..*['\"]" 

# Look for form default value assignments with nested paths
rg -A 5 "defaultValue.*{.*\..*}"

Length of output: 581


Script:

#!/bin/bash
# Let's try a different approach to find nested field patterns and their usage

# Look for field name patterns with dots using ast-grep
ast-grep --pattern 'fieldName: $FIELD' --pattern-type js

# Search for set utility usage with nested paths
rg -A 3 "set\(.+,.+\..+," 

# Look for form-related test files that might contain nested field scenarios
fd "form.*test|test.*form" --type f

# Search for form schema definitions in example files
fd "form" --type f --exec rg -l "schema.*=.*\[" {} \;

Length of output: 987


Script:

#!/bin/bash
# Let's examine the test file and form API implementation for nested field handling

# Check form API tests for nested field scenarios
cat packages/@core/ui-kit/form-ui/__tests__/form-api.test.ts

# Check form API implementation
cat packages/@core/ui-kit/form-ui/src/form-api.ts

# Look for actual usage of nested fields in the playground
cat playground/src/views/examples/form/basic.vue

Length of output: 21223

packages/@core/base/shared/package.json (2)

89-89: LGTM! Good choice using lodash.set for nested property handling.

The addition of lodash.set and its type definitions is a suitable solution for handling nested object paths in form configurations. This aligns well with the PR's objective to support field.xxx.xx format in form defaultValue.

Also applies to: 96-96


Line range hint 27-31: Verify the utils export configuration.

Since lodash.set is being added and exported through utils, let's verify that the export configuration in utils/index.ts matches the package.json configuration.

✅ Verification successful

Export configuration for utils is correctly set up

The verification confirms that:

  • The lodash.set is properly exported in utils/index.ts via export { default as set } from 'lodash.set'
  • The package.json configuration for utils exports correctly maps the source and distribution paths:
    • Types: "./src/utils/index.ts"
    • Development: "./src/utils/index.ts"
    • Default: "./dist/utils/index.mjs"
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the lodash.set export in utils/index.ts

# Check if the set function is properly exported
rg -l "export.*set.*from.*lodash\.set" "src/utils/"

# Verify there are no duplicate exports of 'set'
rg -c "export.*set" "src/utils/"

Length of output: 186


Script:

#!/bin/bash
# First, let's find the correct path to utils directory
fd -t d "utils" packages/@core/base/shared

# Then check the content of index.ts in utils directory
fd -t f "index.ts" packages/@core/base/shared/src/utils -x cat {}

# Also verify the exports configuration
cat packages/@core/base/shared/package.json

Length of output: 3417

pnpm-workspace.yaml Outdated Show resolved Hide resolved
anncwb
anncwb previously approved these changes Nov 27, 2024
@anncwb anncwb added the bug Something isn't working label Nov 27, 2024
@vince292007
Copy link
Collaborator

ci没通过

@leizhiyou
Copy link
Contributor Author

ci没通过

哦,我看到那里没通过了,我处理一下再重新提交

Copy link

changeset-bot bot commented Nov 28, 2024

⚠️ No Changeset found

Latest commit: 23e493d

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

@anncwb anncwb self-requested a review December 1, 2024 13:48
@anncwb anncwb merged commit 12f216c into vbenjs:main Dec 1, 2024
13 checks passed
@ChenWeihua123
Copy link

这个功能 在 5.5.0 好像并没有发布上去 use-form-context.ts 没看到修改后的代码

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.

4 participants