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 unmount field validation logic to JSON form widget (#37220) #37259

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

rahulbarwal
Copy link
Contributor

@rahulbarwal rahulbarwal commented Nov 6, 2024

Description

Problem

When deleting all fields of array item, submit became disabled.

Root cause

The JSON form widget did not properly handle field validation during unmounting of array items, leading to inconsistencies in the form's error state.

Solution

This PR implements useUnmountFieldValidation to FieldRenderer.tsx, enhancing field validation for array items in the JSON form widget. This PR handles...

  • Ensuring proper cleanup of field validation when an array field is removed from the form, resolving visibility issues in list view mode.
  • Maintaining a consistent and accurate form state even after field removal.

Fixes #18752
or
Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags="@tag.JSONForm"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/11703351095
Commit: 521fd25
Cypress dashboard.
Tags: @tag.JSONForm
Spec:


Wed, 06 Nov 2024 12:45:42 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Introduced a new mechanism for handling field validation upon unmounting in the form widget.
  • Bug Fixes

    • Improved field validation logic to enhance form data management during component unmounting.

Implement `useUnmountFieldValidation` for better field validation during unmounting in JSON form widget
@rahulbarwal rahulbarwal self-assigned this Nov 6, 2024
Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

Walkthrough

The changes introduce a new import statement for the useUnmountFieldValidation hook in the FieldRenderer.tsx file. This hook is utilized within the FieldRenderer function to manage field validation upon unmounting. The overall structure of the FieldRenderer function remains unchanged, with no modifications to props or rendering logic. The useEffect hook continues to handle field visibility and form data updates.

Changes

File Path Change Summary
app/client/src/widgets/JSONFormWidget/fields/FieldRenderer.tsx Added import for useUnmountFieldValidation and invoked it within FieldRenderer.

Assessment against linked issues

Objective Addressed Explanation
Handle field validation on unmount to avoid submit issues (#18752)

Possibly related PRs

Suggested labels

Bug, Widgets Product, High, Production, Community Reported, Needs Triaging, JSON Form, Widget Validation, ok-to-test, Widgets & Accelerators Pod

Suggested reviewers

  • ashit-rath
  • jacquesikot

🎉 In the code where fields reside,
A hook was added, oh what a ride!
Validation now takes its stand,
As forms unmount, it lends a hand.
With props intact, the logic flows,
A smoother path for all who chose! 🌟


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.

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.

@github-actions github-actions bot added Bug Something isn't working Community Reported issues reported by community members High This issue blocks a user from building or impacts a lot of users JSON Form Issue / features related to the JSON form wiget Needs Triaging Needs attention from maintainers to triage Production Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets labels Nov 6, 2024
@rahulbarwal rahulbarwal added the ok-to-test Required label for CI label Nov 6, 2024
@github-actions github-actions bot added Enhancement New feature or request Bug Something isn't working and removed Bug Something isn't working labels Nov 6, 2024
@rahulbarwal
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

Copy link

github-actions bot commented Nov 6, 2024

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/11703417058.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 37259.
recreate: .

@github-actions github-actions bot added Bug Something isn't working and removed Bug Something isn't working labels Nov 6, 2024
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

🧹 Outside diff range and nitpick comments (2)
app/client/src/widgets/JSONFormWidget/fields/FieldRenderer.tsx (2)

Line range hint 11-14: Address TODO comment during this edit

Since we're editing the file now, we should address the eslint-disable comment by properly typing the options.

-  // TODO: Fix this the next time the file is edited
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  options?: Record<string, any>;
+  options?: Record<string, unknown>;

Line range hint 34-52: Consider documenting the interaction between unmount validation and visibility handling

The existing comment about visibility and unmounting should be updated to clarify how the new unmount validation interacts with the visibility-based field removal.

Add a note about the unmount validation handling to the existing comment:

  /**
   * When a component is hidden, the field is removed from the form and the component unmounts.
   * So formData update is triggered so that the particular field can be removed from the `formData`.
+  * The useUnmountFieldValidation hook ensures proper cleanup of validation state during this process.
   *
   * Alternative solution: enable `shouldUnregister` in useController which will
   * de-register in the RHF's internal formData state. There's a caveat which needs to be
   * solved before this is enabled i.e the ArrayField does not play well with this configuration.
   * Removing of one Array field item will reset all the items below it.
   */
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b23ba1d and 521fd25.

📒 Files selected for processing (1)
  • app/client/src/widgets/JSONFormWidget/fields/FieldRenderer.tsx (2 hunks)
🔇 Additional comments (2)
app/client/src/widgets/JSONFormWidget/fields/FieldRenderer.tsx (2)

8-8: LGTM: Clean import statement

The import follows the project's conventions and is appropriately placed with other hook imports.


34-34: Verify unmount cleanup behavior

The hook placement is correct, but let's verify its cleanup behavior with array fields.

✅ Verification successful

The previous pattern might have been too specific. Let's try to find the hook implementation first and then analyze its cleanup behavior.


Cleanup behavior is properly implemented and tested

The hook correctly handles unmount cleanup:

  • Implements cleanup function in useEffect
  • Triggers field validation on unmount
  • Properly updates when fieldName changes
  • Comprehensive test coverage including field name changes
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for cleanup handling in the hook implementation
ast-grep --pattern 'useUnmountFieldValidation = function($_) {
  $$$
  useEffect(() => {
    return () => {
      $$$
    }
  }, $$$)
}'

Length of output: 138


Script:

#!/bin/bash
# Let's try a different pattern to find the hook implementation
rg -A 10 "export const useUnmountFieldValidation" 

# Also search for any file containing this hook name
fd "useUnmountFieldValidation"

Length of output: 236


Script:

#!/bin/bash
# Let's examine the hook implementation
cat app/client/src/widgets/JSONFormWidget/fields/useUnmountFieldValidation.ts

# Also check the test file to understand the expected behavior
cat app/client/src/widgets/JSONFormWidget/fields/useUnmountFieldValidation.test.tsx

Length of output: 2820

Copy link

github-actions bot commented Nov 6, 2024

Deploy-Preview-URL: https://ce-37259.dp.appsmith.com

@@ -30,6 +31,7 @@ function FieldRenderer({

const FieldComponent = FIELD_MAP[fieldType];

useUnmountFieldValidation({ fieldName });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are adding it here; then doesn't it mean we might not need to explicitly do it for every field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so as well, but the conditionally rendered inputs which are not inside ObjectField or Array don't trigger this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this problem #28018 appears again if we remove it from all the other places.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

@rahulbarwal rahulbarwal merged commit ae7f8c7 into release Nov 7, 2024
50 checks passed
@rahulbarwal rahulbarwal deleted the rahulbarwal/issue18752 branch November 7, 2024 05:59
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Nov 20, 2024
…horg#37220) (appsmithorg#37259)

## Description
<ins>Problem</ins>

When deleting all fields of array item, submit became disabled.

<ins>Root cause</ins>

The JSON form widget did not properly handle field validation during
unmounting of array items, leading to inconsistencies in the form's
error state.

<ins>Solution</ins>

This PR implements `useUnmountFieldValidation` to `FieldRenderer.tsx`,
enhancing field validation for array items in the JSON form widget. This
PR handles...

- Ensuring proper cleanup of field validation when an array field is
removed from the form, resolving visibility issues in list view mode.
- Maintaining a consistent and accurate form state even after field
removal.


Fixes appsmithorg#18752
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.JSONForm"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/11703351095>
> Commit: 521fd25
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11703351095&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.JSONForm`
> Spec:
> <hr>Wed, 06 Nov 2024 12:45:42 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new mechanism for handling field validation upon
unmounting in the form widget.
  
- **Bug Fixes**
- Improved field validation logic to enhance form data management during
component unmounting.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Reported issues reported by community members Enhancement New feature or request High This issue blocks a user from building or impacts a lot of users JSON Form Issue / features related to the JSON form wiget Needs Triaging Needs attention from maintainers to triage ok-to-test Required label for CI Production Widgets & Accelerators Pod Issues related to widgets & Accelerators Widgets Product This label groups issues related to widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]-[72]:Submit gets unclickable after deleting all array item from JSONForm
2 participants