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 fieldMapToTime prop to FormRenderProps #4838

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

DesignHhuang
Copy link
Contributor

@DesignHhuang DesignHhuang commented Nov 8, 2024

Description

feat: add fieldMapToTime prop to FormRenderProps

fix: #4642 Form表单添加fieldMapToTime格式化时间范围字段

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)
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Summary by CodeRabbit

  • New Features

    • Enhanced form submission and reset functionalities with improved handling of time-related fields.
    • Introduced a new RangePicker component for selecting date ranges in forms.
    • Added a mapping capability for form fields to time formats.
  • Type Definitions

    • New type FieldMapToTime added to support mapping form fields to time formats.

Copy link

changeset-bot bot commented Nov 8, 2024

⚠️ No Changeset found

Latest commit: de4c74e

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 Nov 8, 2024

Walkthrough

The pull request introduces enhancements to form handling in the form-actions.vue component, focusing on submission and reset functionalities. It includes the addition of two new functions, handleRangeTimeValue and formatTime, to manage time-related fields effectively. A new type, FieldMapToTime, is defined in the types file, allowing for the mapping of form fields to time formats. Additionally, the form.vue file is updated to incorporate a new property and schema for handling date ranges.

Changes

File Path Change Summary
packages/@core/ui-kit/form-ui/src/components/form-actions.vue - Added functions: handleRangeTimeValue and formatTime.
- Modified handleSubmit and handleReset to utilize the new functions for time field management.
packages/@core/ui-kit/form-ui/src/types.ts - Added new type FieldMapToTime.
- Updated FormRenderProps interface to include fieldMapToTime.
playground/src/views/examples/vxe-table/form.vue - Added fieldMapToTime property to formOptions.
- Introduced RangePicker component in the form schema.

Assessment against linked issues

Objective Addressed Explanation
Implement fieldMapToTime for formatting time range fields (#4642)
Enhance form submission and reset functionalities (#4642)

Possibly related PRs

Suggested labels

feature

Suggested reviewers

  • vince292007
  • anncwb

Poem

In the fields of time we play,
With ranges bright, we find our way.
A form to fill, a reset clear,
With every change, we cheer and cheer!
So hop along, let values flow,
In this new code, let magic grow! 🐇✨


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

🧹 Outside diff range and nitpick comments (6)
playground/src/views/examples/vxe-table/form.vue (2)

66-70: Consider adding componentProps for better UX

The RangePicker component could benefit from additional configuration:

    {
      component: 'RangePicker',
      fieldName: 'dateRangePicker',
      label: 'DateRange',
+     componentProps: {
+       placeholder: ['Start Date', 'End Date'],
+       format: 'YYYY-MM',
+       valueType: 'timestamp',
+       allowClear: true
+     }
    },

This would:

  • Add helpful placeholders
  • Ensure consistent date formatting
  • Specify value type for form submission
  • Allow clearing the selection

Line range hint 24-70: Enhance example implementation with validation and error handling

Since this is an example implementation in the playground, consider enhancing it to demonstrate best practices:

  1. Add form validation rules for the date range
  2. Show error handling for invalid date selections
  3. Add comments explaining the fieldMapToTime configuration

Would you like me to provide an example of these enhancements?

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

64-74: Consider improving the destructuring pattern.

While the time field cleanup logic is correct, the destructuring pattern [_, [startTimeKey, endTimeKey]] could be more explicit.

Consider this improvement:

-    props.fieldMapToTime.forEach(([_, [startTimeKey, endTimeKey]]) => {
+    props.fieldMapToTime.forEach(([fieldName, [startTimeKey, endTimeKey]]) => {

This makes the code more self-documenting by using a meaningful variable name instead of the underscore.

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

208-212: Enhance type definition with documentation and type aliases.

While the type structure is correct, consider these improvements for better maintainability and developer experience:

+/** Time format string for date-fns or moment */
+type TimeFormat = string;
+
+/** Tuple of field names for start and end time */
+type TimeFieldNames = [string, string];
+
+/** Format specification for time fields - either a single format or separate formats for start/end */
+type TimeFormatSpec = [TimeFormat, TimeFormat] | TimeFormat;
+
+/**
+ * Maps form fields to time range fields with optional formatting
+ * @example
+ * const fieldMapToTime: FieldMapToTime = [
+ *   // Map 'dateRange' field to 'startTime' and 'endTime' with 'yyyy-MM-dd' format
+ *   ['dateRange', ['startTime', 'endTime'], 'yyyy-MM-dd'],
+ *   // Map with different formats for start and end times
+ *   ['timeRange', ['start', 'end'], ['HH:mm', 'HH:mm:ss']]
+ * ];
+ */
-export type FieldMapToTime = [
-  string,
-  [string, string],
-  ([string, string] | string)?,
-][];
+export type FieldMapToTime = [
+  string,
+  TimeFieldNames,
+  TimeFormatSpec?,
+][];

274-277: Enhance property documentation with examples and use cases.

The documentation could be more descriptive to help developers understand the property's purpose and usage.

-  /**
-   * 表单字段映射成时间格式
-   */
+  /**
+   * Maps form fields to time range fields with optional formatting.
+   * Use this to transform time range picker values into separate start/end time fields
+   * during form submission.
+   * @example
+   * fieldMapToTime: [
+   *   // Basic mapping with default format
+   *   ['dateRange', ['startDate', 'endDate']],
+   *   // With custom format
+   *   ['timeRange', ['startTime', 'endTime'], 'HH:mm:ss'],
+   *   // With different formats for start/end
+   *   ['customRange', ['start', 'end'], ['yyyy-MM-dd', 'dd/MM/yyyy']]
+   * ]
+   */

Implementation needs additional time formatting functionality

The current implementation only defines the FieldMapToTime type but lacks the complete implementation for handling time range fields. While there is a basic formatTime function in form-actions.vue, it doesn't fully support the time range mapping functionality described in the type definition. Specifically:

  • The formatTime function only handles single timestamps
  • No implementation found for handling the time range arrays as defined in FieldMapToTime type
  • Missing logic for processing the optional third element in the FieldMapToTime tuple

The implementation should be extended to include:

  • Support for processing time range arrays [string, string]
  • Logic for handling the optional format parameter
  • Integration with the form value transformation pipeline
🔗 Analysis chain

Line range hint 208-277: Implementation successfully addresses the requirements.

The type system implementation effectively supports the time range field formatting feature requested in issue #4642. The solution is:

  • Type-safe and flexible
  • Backward compatible
  • Well-integrated into the existing form props structure

Let's verify the integration with the form component:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the form component's usage of fieldMapToTime

# Check for form component implementations using fieldMapToTime
echo "Checking for form component implementations..."
rg -l "fieldMapToTime.*:\s*\[" --type ts

# Check for any existing time-related form field handling
echo "Checking for existing time field handling..."
rg -l "time.*format|format.*time" --type ts

Length of output: 290


Script:

#!/bin/bash
# Let's try a broader search to understand the implementation and usage

# Check for any references to the new FieldMapToTime type
echo "Checking for FieldMapToTime references..."
rg "FieldMapToTime" --type ts -A 2

# Check for time-related form field transformations
echo "Checking for time field transformations..."
rg "getFieldsValue|setFieldsValue.*time" --type ts -A 3

# Look for form component implementations
echo "Looking for main form component..."
fd -e ts -e tsx "Form\.tsx$|Form\.ts$"

Length of output: 773


Script:

#!/bin/bash
# Let's check the implementation details more thoroughly

# Look for the form component implementation in the form-ui directory
echo "Checking form-ui directory for implementation..."
rg -t ts -t tsx "class.*Form|function.*Form" packages/@core/ui-kit/form-ui/src/ -A 5

# Check for any time-related transformations or utilities
echo "Checking for time-related utilities..."
rg "transform.*time|format.*time|time.*format" packages/@core/ui-kit/form-ui/src/ -A 3

# Check for form value handling
echo "Checking form value handling..."
rg "handleFormValues|transformFormValues" packages/@core/ui-kit/form-ui/src/ -A 5

Length of output: 1474

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8f1e397 and de4c74e.

📒 Files selected for processing (3)
  • packages/@core/ui-kit/form-ui/src/components/form-actions.vue (1 hunks)
  • packages/@core/ui-kit/form-ui/src/types.ts (2 hunks)
  • playground/src/views/examples/vxe-table/form.vue (2 hunks)
🔇 Additional comments (2)
playground/src/views/examples/vxe-table/form.vue (1)

24-24: Verify if YYYY-MM format meets requirements

The time format is set to 'YYYY-MM' which only captures year and month. Please confirm if this is the intended format or if it should include day/time components (e.g., 'YYYY-MM-DD').

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

55-57: LGTM: Time range processing added to form submission.

The addition of handleRangeTimeValue properly processes time range fields before form submission, aligning with the PR objectives.

Let's verify the form submission handling:

✅ Verification successful

Time range processing is correctly isolated to form-actions component

The handleRangeTimeValue function is only defined and used in form-actions.vue, which is the centralized component for form submissions in the UI kit. This is the correct place for this functionality as:

  1. It's part of the core form submission flow in @core/ui-kit/form-ui
  2. The function is conditionally applied based on fieldMapToTime prop, making it an opt-in feature
  3. All form submissions in the codebase go through this component, ensuring consistent time range processing when needed
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for form submission handlers to ensure consistent time range processing
ast-grep --pattern 'handleSubmit($$$) {
  $$$
  handleRangeTimeValue($$$)
  $$$
}'

Length of output: 84


Script:

#!/bin/bash
# Let's first check where handleRangeTimeValue is defined
rg "handleRangeTimeValue" -A 3

# Also search for other form submission handlers
rg "handleSubmit" -A 3

# Check for any other time range processing in form submissions
ast-grep --pattern 'const values = $$$($$$form.values$$$)'

Length of output: 24305

@anncwb anncwb merged commit 6b9acf0 into vbenjs:main Nov 9, 2024
14 checks passed
@anncwb anncwb added the feature label Nov 9, 2024
@DesignHhuang DesignHhuang deleted the fieldMapToTime branch November 15, 2024 06:05
@github-actions github-actions bot locked and limited conversation to collaborators Dec 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FEATURE: Form表单添加fieldMapToTime格式化时间范围字段
2 participants