Skip to content

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/3094351964/90041

Summary

Adds a new snippet that automatically appends a new row to Gravity Forms List fields when the user types in the last row.
Improves user experience by streamlining data entry and removing the need to manually click the “Add” button.

https://www.loom.com/share/0120ed39296b4e4988c7fdd054af070e

Copy link

coderabbitai bot commented Oct 7, 2025

Walkthrough

Introduces a new JavaScript snippet that auto-appends a row to Gravity Forms List fields when the user begins typing in the last row. It binds input listeners to the last row, detects transition from empty to non-empty, clicks the add button, and rebinds after additions to track the new last row.

Changes

Cohort / File(s) Summary
Auto-add List row behavior
gravity-forms/gw-auto-add-list-row.js
New script that binds to List field containers, tracks last input value, auto-clicks the add-row button on first non-empty input in the last row, and rebinds listeners after manual additions to avoid duplicate handlers.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant P as Page Load
    participant S as AutoAdd Script
    participant C as List Field Container
    participant L as Last Row Input
    participant B as Add Button (.add_list_item)

    P->>S: Initialize
    S->>C: Query all .gfield_list_container
    loop per container
      S->>L: Bind input listener (store lastVal)
      S->>B: Bind click listener (rebind after add)
    end

    U->>L: Type character
    L-->>S: input event (prev empty → now non-empty)
    S->>B: trigger click()
    B->>C: Add new row to list
    Note over S,C: After short delay, rebind to new last row
    S->>C: Cleanup old listeners and rebind
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • veryspry

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title concisely and accurately reflects the main change by describing the addition of a snippet to automatically append a new row to a Gravity Forms List field, matching the pull request contents. It avoids vague phrasing and clearly conveys the primary purpose. As a result, it provides clear context at a glance.
Description Check ✅ Passed The description follows the repository template by providing a Context section with the relevant help desk ticket and a Summary section that clearly explains the new snippet’s behavior and user benefit. It aligns with the required headings and includes the essential details without extraneous placeholders. The inclusion of a Loom link enhances clarity but does not detract from the completeness of the required template sections.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch saif/add/90041-add-auto-add-list-row-snippet

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

github-actions bot commented Oct 7, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.
Messages
📖 A new snippet with a Loom video? Magical! 📹
📖 Merlin would give this scroll the highest of praises. Cheers for adding this new snippet to the library! 🪄

Generated by 🚫 dangerJS against ad57040

Copy link

@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

🧹 Nitpick comments (5)
gravity-forms/gw-auto-add-list-row.js (5)

1-17: Document the jQuery dependency.

The snippet relies on jQuery but doesn't mention it in the instructions. Add a note about the jQuery requirement to help users troubleshoot if issues arise.

Apply this diff to add the dependency note:

  * Instructions:
  *
+ * 0. Ensure jQuery is loaded (Gravity Forms includes it by default).
+ *
  * 1. Install this snippet with our free Code Chest plugin.
  *    https://gravitywiz.com/gravity-forms-code-chest/

19-21: Consider using const instead of var.

Modern JavaScript best practice favors const for values that won't be reassigned. This also provides better scoping and avoids unintended global pollution.

Apply this diff:

-var listSelector = '.gfield_list_container';
+const listSelector = '.gfield_list_container';

37-37: Consider more targeted handler cleanup.

The current approach removes handlers from all text inputs in the container, even though only the last input needs rebinding. While this works and prevents duplicates, a more targeted approach would be to track and remove only the handler from the previously bound input.

However, the current implementation is clear and safe, so this is just a minor optimization suggestion. You may keep it as-is if preferred.


65-65: Wrap initial binding in a DOM ready check.

The script calls bindAutoAdd() immediately, which may fail if the form hasn't rendered yet. While Gravity Forms typically loads scripts after the DOM, explicitly wrapping this in a ready check ensures reliability across different loading scenarios.

Apply this diff:

-bindAutoAdd();
+$( document ).ready( function() {
+	bindAutoAdd();
+} );

Or use the shorthand:

-bindAutoAdd();
+$( bindAutoAdd );

70-72: Verify the 50ms delay is sufficient across browsers.

The arbitrary 50ms timeout may be insufficient if DOM manipulation is slow, or excessive if it's fast. While this approach is simple and likely works in most cases, consider whether a more robust approach is needed.

Optional enhancement: Use requestAnimationFrame for more reliable timing:

 $( document ).on( 'click', '.add_list_item', function() {
-	setTimeout( bindAutoAdd, 50 );
+	requestAnimationFrame( function() {
+		setTimeout( bindAutoAdd, 0 );
+	} );
 } );

This ensures the rebind happens after the next browser paint, which is more reliable than an arbitrary delay. However, the current implementation is acceptable if it works consistently in your testing.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc0881c and ad57040.

📒 Files selected for processing (1)
  • gravity-forms/gw-auto-add-list-row.js (1 hunks)

$( listSelector ).each( function() {
var $container = $( this );
var $lastRow = $container.find( '.gfield_list_group' ).last();
var $input = $lastRow.find( 'input[type="text"]' );
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Support all input types in List fields.

The snippet only targets input[type="text"], but Gravity Forms List fields can contain textareas, selects, number inputs, and other field types. This significantly limits the snippet's applicability.

Apply this diff to support all input types:

-		var $input = $lastRow.find( 'input[type="text"]' );
+		var $input = $lastRow.find( 'input, textarea, select' ).first();

Note: Using .first() ensures we bind to the first input in the row, which is a reasonable trigger point for multi-column list fields.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var $input = $lastRow.find( 'input[type="text"]' );
// instead of targeting only text inputs, bind to the first form control in the row
var $input = $lastRow.find( 'input, textarea, select' ).first();
🤖 Prompt for AI Agents
In gravity-forms/gw-auto-add-list-row.js around line 30, the code currently only
finds input[type="text"] which misses textareas, selects, number inputs, etc.;
change the selector to use jQuery’s generic input selector (e.g. :input) and
call .first() on the matched set so the code binds to the first form control in
the new row, ensuring all field types are supported.

@saifsultanc saifsultanc merged commit 2267f62 into master Oct 8, 2025
4 checks passed
@saifsultanc saifsultanc deleted the saif/add/90041-add-auto-add-list-row-snippet branch October 8, 2025 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant