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

feat: add submitOnEnter configuration to form #4670

Merged
merged 1 commit into from
Oct 17, 2024
Merged

feat: add submitOnEnter configuration to form #4670

merged 1 commit into from
Oct 17, 2024

Conversation

anncwb
Copy link
Collaborator

@anncwb anncwb commented Oct 17, 2024

Description

close #4637

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

    • Enhanced documentation for the Vben Form component, including new sections and examples for better usability.
    • Added submitOnEnter property to forms, allowing submission via the Enter key.
    • Introduced methods handleReset and handleSubmit for improved interactivity in form actions.
  • Improvements

    • Expanded form configuration options with a variety of input components for better layout management.
    • Refined explanations and examples in documentation for form validation and dynamic dependencies.
  • Bug Fixes

    • Adjustments made to ensure clarity in TypeScript definitions and API documentation.

@anncwb anncwb added the enhancement New feature or request label Oct 17, 2024
@anncwb anncwb requested review from vince292007 and a team as code owners October 17, 2024 14:43
Copy link

changeset-bot bot commented Oct 17, 2024

⚠️ No Changeset found

Latest commit: f387a4c

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 Oct 17, 2024

Walkthrough

The pull request introduces significant enhancements to the Vben Form component documentation and its associated functionalities. Key updates include the addition of TypeScript definitions, expanded sections on usage and validation, and the introduction of new properties like submitOnEnter. The form-actions.vue component now exposes methods for external access, while the vben-use-form.vue component has been updated to handle form submissions via the Enter key. Overall, these changes aim to improve clarity, usability, and interactivity within the form components.

Changes

File Change Summary
docs/src/components/common-ui/vben-form.md Updated documentation with new sections, examples, and TypeScript definitions for form components.
packages/@core/ui-kit/form-ui/src/components/form-actions.vue Added defineExpose to expose handleReset and handleSubmit methods.
packages/@core/ui-kit/form-ui/src/form-api.ts Introduced submitOnEnter property to VbenFormProps interface.
packages/@core/ui-kit/form-ui/src/types.ts Added submitOnEnter property to VbenFormProps interface.
packages/@core/ui-kit/form-ui/src/vben-use-form.vue Added formActionsRef and handleKeyDownEnter for handling Enter key submissions.
playground/src/views/examples/form/basic.vue Enhanced form with new components and added onSubmit and handleSetFormValue functions.
playground/src/views/examples/vxe-table/form.vue Added submitOnEnter property to formOptions.

Assessment against linked issues

Objective Addressed Explanation
Enable form submission on Enter key for useVbenVxeGrid (4637)

Possibly related PRs

Suggested labels

feature

Suggested reviewers

  • vince292007

Poem

In the fields of forms we play,
With Enter keys to save the day.
New docs and types, oh what a sight,
Vben Forms, now shining bright!
With actions exposed, we leap and bound,
In usability, joy is found! 🐇✨


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 (8)
playground/src/views/examples/vxe-table/form.vue (1)

68-69: LGTM! Consider adding English comments for consistency.

The addition of the submitOnEnter property to formOptions successfully implements the requested feature from issue #4637. Setting it to false by default is a good choice for maintaining backward compatibility.

Consider adding an English translation to the comment for consistency with the rest of the codebase:

-  // 按下回车时是否提交表单
+  // 按下回车时是否提交表单 (Whether to submit the form when Enter is pressed)
   submitOnEnter: false,
packages/@core/ui-kit/form-ui/src/vben-use-form.vue (3)

26-27: LGTM: FormActions reference declaration

The formActionsRef declaration is correctly implemented using useTemplateRef. This enables access to FormActions methods, which is crucial for the new Enter key submission feature.

Consider adding a comment explaining the purpose of this reference, e.g.:

// Reference to FormActions component for accessing its methods
const formActionsRef = useTemplateRef<typeof FormActions>('formActionsRef');

42-47: LGTM: handleKeyDownEnter function implementation

The handleKeyDownEnter function correctly implements the form submission on Enter key press, addressing the PR objective and the linked issue #4637. It appropriately checks for the submitOnEnter state and the availability of formActionsRef before triggering the submission.

Consider adding a comment explaining the purpose of this function, e.g.:

// Handle form submission when Enter key is pressed
function handleKeyDownEnter() {
  // ... existing code ...
}

Action Required: Incomplete Implementation of Enter Key Submission Feature

The current implementation has the following issues that need to be addressed:

  • Missing submitOnEnter Prop Definition:

    • The submitOnEnter prop is used in vben-use-form.vue but is not defined in the Props interface.
  • Undefined handleSubmit Method:

    • The FormActions component references a handleSubmit method, which is not implemented.

These issues prevent the Enter key form submission feature from functioning correctly and may lead to runtime errors.

🔗 Analysis chain

Line range hint 1-91: Summary: Successful implementation of Enter key form submission

The changes in this file successfully implement the Enter key form submission feature, addressing the PR objective and the linked issue #4637. Key improvements include:

  1. Addition of useTemplateRef for creating a reference to the FormActions component.
  2. Implementation of the handleKeyDownEnter function to manage form submission on Enter key press.
  3. Template modifications to link the Form and FormActions components with the new functionality.

These changes enhance the form's usability by allowing users to submit forms using the Enter key, as requested. The implementation is clean, efficient, and well-integrated with the existing code structure.

To ensure the changes are properly integrated and don't introduce any regressions, please run the following verification script:

This script will help verify the proper implementation and integration of the new feature across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of Enter key form submission feature

# Test 1: Check if the submitOnEnter prop is properly defined in the component props
echo "Test 1: Checking submitOnEnter prop definition"
ast-grep --lang typescript --pattern 'interface Props extends VbenFormProps { $$$1 }' packages/@core/ui-kit/form-ui/src/vben-use-form.vue

# Test 2: Verify the usage of submitOnEnter in other components
echo "Test 2: Checking usage of submitOnEnter in other components"
rg --type vue 'submitOnEnter' packages/@core/ui-kit/form-ui/src

# Test 3: Look for any existing keydown.enter handlers that might conflict
echo "Test 3: Checking for potential conflicting keydown.enter handlers"
rg --type vue '@keydown\.enter' packages/@core/ui-kit/form-ui/src

# Test 4: Verify FormActions component interface
echo "Test 4: Checking FormActions component interface"
ast-grep --lang typescript --pattern 'interface $_ { $$$1 handleSubmit?: () => void; $$$2 }' packages/@core/ui-kit/form-ui/src/components/form-actions.vue

Length of output: 964


Script:

#!/bin/bash
# Description: Enhanced verification of Enter key form submission feature

# Test 1: Check if the submitOnEnter prop is properly defined in the component props
echo "Test 1: Checking submitOnEnter prop definition"
ast-grep --lang typescript --pattern 'interface Props extends VbenFormProps { $$$1 }' packages/@core/ui-kit/form-ui/src/vben-use-form.vue

# Test 2: Verify the usage of submitOnEnter in other components by searching within .vue files
echo "Test 2: Checking usage of submitOnEnter in other components"
rg 'submitOnEnter' packages/@core/ui-kit/form-ui/src --glob '*.vue'

# Test 3: Look for any existing keydown.enter handlers that might conflict by searching within .vue files
echo "Test 3: Checking for potential conflicting keydown.enter handlers"
rg '@keydown\.enter' packages/@core/ui-kit/form-ui/src --glob '*.vue'

# Test 4: Verify FormActions component interface by checking for handleSubmit method
echo "Test 4: Checking FormActions component interface"
ast-grep --lang typescript --pattern 'interface $_ { $$$1 handleSubmit?: () => void; $$$2 }' packages/@core/ui-kit/form-ui/src/components/form-actions.vue

Length of output: 1129

packages/@core/ui-kit/form-ui/src/components/form-actions.vue (1)

Line range hint 40-48: Consider enhancing error handling in handleSubmit

While the handleSubmit method correctly validates the form before submission, it might be beneficial to add explicit error handling. This could improve the user experience and make debugging easier.

Consider updating the handleSubmit method as follows:

 async function handleSubmit(e: Event) {
   e?.preventDefault();
   e?.stopPropagation();
-  const { valid } = await form.validate();
-  if (!valid) {
-    return;
+  try {
+    const { valid } = await form.validate();
+    if (!valid) {
+      console.warn('Form validation failed');
+      return;
+    }
+    await unref(rootProps).handleSubmit?.(toRaw(form.values));
+  } catch (error) {
+    console.error('Error during form submission:', error);
+    // Optionally, you can add user notification logic here
   }
-  await unref(rootProps).handleSubmit?.(toRaw(form.values));
 }

This change adds try-catch block to handle potential errors during validation or submission, improving the robustness of the component.

packages/@core/ui-kit/form-ui/src/types.ts (1)

332-337: LGTM! Consider enhancing the documentation.

The addition of the submitOnEnter property is well-implemented and aligns with the PR objectives. The type, optionality, and default value are appropriate.

Consider enhancing the documentation to provide more context:

 /**
  * 是否在回车时提交表单
+ * This allows users to submit the form by pressing the Enter key.
  * @default false
  */
 submitOnEnter?: boolean;
packages/@core/ui-kit/form-ui/src/form-api.ts (1)

38-38: LGTM! Consider additional steps for full implementation.

The addition of submitOnEnter: false to the default form properties is a good start for implementing the requested feature. This change aligns well with the PR objectives and the linked issue #4637.

To complete the implementation, consider the following steps:

  1. Update the documentation to reflect this new configuration option, explaining its purpose and usage.
  2. Implement the logic to handle the submitOnEnter behavior in the form submission code (likely in the FormApi class or a related component).
  3. Add unit tests to cover this new configuration option, ensuring it behaves correctly when set to both true and false.

Would you like assistance in implementing any of these additional steps?

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

314-314: LGTM! Consider adding an example for submitOnEnter.

The addition of the submitOnEnter property is well-documented and aligns with the PR objectives. Great job on providing a clear description and setting a sensible default value.

To further improve the documentation, consider adding a brief example demonstrating how to use this new property in the "Basic Usage" or "Form Operations" section.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f89f4f3 and f387a4c.

📒 Files selected for processing (7)
  • docs/src/components/common-ui/vben-form.md (1 hunks)
  • packages/@core/ui-kit/form-ui/src/components/form-actions.vue (1 hunks)
  • packages/@core/ui-kit/form-ui/src/form-api.ts (1 hunks)
  • packages/@core/ui-kit/form-ui/src/types.ts (1 hunks)
  • packages/@core/ui-kit/form-ui/src/vben-use-form.vue (4 hunks)
  • playground/src/views/examples/form/basic.vue (1 hunks)
  • playground/src/views/examples/vxe-table/form.vue (1 hunks)
🧰 Additional context used
🔇 Additional comments (8)
playground/src/views/examples/vxe-table/form.vue (2)

Line range hint 1-110: Summary: Well-implemented feature addition

The addition of the submitOnEnter property to the formOptions object successfully addresses the feature request from issue #4637. The implementation is minimal, well-placed, and maintains backward compatibility by defaulting to false.

This change enhances the flexibility of the form component, allowing users to control whether forms can be submitted by pressing the Enter key. It aligns well with the PR objectives and should improve the user experience for those who need this functionality.

Great job on implementing this feature! Once the suggested verifications and documentation updates are complete, this PR should be ready for merging.


68-69: Verify hook implementation and update documentation

While the addition of the submitOnEnter property is correct, it's important to ensure that the useVbenVxeGrid hook and its related components properly handle this new property.

Please run the following script to check the implementation of useVbenVxeGrid:

Consider updating the documentation to include usage examples for the new submitOnEnter property. This will help users understand how to utilize this new feature effectively.

packages/@core/ui-kit/form-ui/src/vben-use-form.vue (2)

9-10: LGTM: Import statement for useTemplateRef

The addition of useTemplateRef import is appropriate for creating a reference to the FormActions component, which is necessary for the new form submission functionality.


52-52: LGTM: Template changes for Enter key submission

The template changes effectively implement the Enter key submission feature:

  1. The @keydown.enter.prevent="handleKeyDownEnter" event listener on the Form component correctly triggers the handleKeyDownEnter function.
  2. The ref="formActionsRef" attribute on the FormActions component properly links it to the formActionsRef, enabling access to its methods.

These changes align with the PR objective and address the linked issue #4637.

Also applies to: 71-71

packages/@core/ui-kit/form-ui/src/components/form-actions.vue (2)

78-82: Excellent enhancement to component flexibility!

The exposure of handleReset and handleSubmit methods is a valuable addition. This change:

  1. Allows parent components to programmatically trigger form reset and submit actions.
  2. Enhances the reusability and flexibility of the form-actions component.
  3. Aligns well with the PR objectives of improving form functionality.

The implementation is clean and doesn't introduce any apparent issues.


Line range hint 1-124: Summary of review for form-actions.vue

  1. The exposure of handleReset and handleSubmit methods is a positive enhancement, improving the component's flexibility and aligning with the PR objectives.
  2. The implementation of these methods is robust, including form validation and flexible reset behavior.
  3. A suggestion was made to improve error handling in the handleSubmit method for better user experience and easier debugging.

Overall, the changes contribute positively to the form functionality and component reusability. The file is well-structured and maintains good coding practices.

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

Line range hint 1-314: Excellent documentation improvements!

The extensive updates to the Vben Form documentation are commendable. The additions of new sections, detailed examples, and TypeScript type definitions significantly enhance the usability and clarity of the component. The explanations of complex features like form linkage and validation are particularly well-done.

These improvements will greatly assist developers in understanding and implementing the Vben Form component effectively.

playground/src/views/examples/form/basic.vue (1)

Line range hint 19-109: The form configurations are comprehensive and well-structured

The forms are effectively utilizing a wide range of components and demonstrate various layout options. This provides a thorough example of the form capabilities and enhances the usability of the application.

playground/src/views/examples/form/basic.vue Show resolved Hide resolved
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FEATURE: useVbenVxeGrid 配合表单查询 Input 回车自动提交查询
1 participant