Skip to content

gpadvs-dropdown-remove-button.js: Added snippet to show remove button on Dropdown fields. #1028

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

Merged
merged 5 commits into from
Mar 21, 2025

Conversation

veryspry
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2861069601/78654?viewId=6987275

Summary

Adds the "remove button" plugin for Dropdown fields.

@saifsultanc saifsultanc self-requested a review March 8, 2025 10:20
Copy link
Contributor

@saifsultanc saifsultanc left a comment

Choose a reason for hiding this comment

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

gpadvs-dropdown-remove-button.js works good.

gpadvs-clear-button.js also works good. But it also works for Multi Select, which kind of looks weird. Do you think it's worth to scope it to only dropdown?
See screenshot:

Screenshot 2025-03-08 at 3 51 10 PM

…scope to _only_ `dropdown` or `multiselect` fields.
Copy link

coderabbitai bot commented Mar 18, 2025

Walkthrough

Two new JavaScript files have been added to the GP Advanced Select module. Each file registers a new filter via gform.addFilter('gpadvs_settings', ...) to modify the settings for Gravity Forms Advanced Select fields. One file enhances dropdown fields by adding a clear button using the Tom Select library, while the other targets multi-select fields by adding a remove button. Both implementations adjust the settings conditionally using the plugin mechanism provided by Tom Select.

Changes

File(s) Change Summary
gp-advanced-select/gpadvs-clear-button.js
gp-advanced-select/gpadvs-dropdown-remove-button.js
Introduces two new files that register a filter for gpadvs_settings. The clear button file adds a clear_button plugin for dropdown fields, while the remove button file adds a remove_button plugin for multi-select fields using Tom Select.

Sequence Diagram(s)

sequenceDiagram
    participant User as End User
    participant GF as Gravity Forms
    participant ClearFilter as Clear Button Filter (gpadvs-clear-button.js)
    participant TS as Tom Select

    User->>GF: Load Advanced Select Field (Dropdown)
    GF->>ClearFilter: Invoke gform.addFilter('gpadvs_settings')
    ClearFilter-->>GF: Return settings with clear_button plugin enabled
    GF->>TS: Initialize Advanced Select with clear button
    User->>TS: Interact with clear button
Loading
sequenceDiagram
    participant User as End User
    participant GF as Gravity Forms
    participant RemoveFilter as Remove Button Filter (gpadvs-dropdown-remove-button.js)
    participant TS as Tom Select

    User->>GF: Load Advanced Select Field (Multi-Select)
    GF->>RemoveFilter: Invoke gform.addFilter('gpadvs_settings')
    RemoveFilter-->>GF: Return settings with remove_button plugin enabled
    GF->>TS: Initialize Advanced Select with remove button
    User->>TS: Interact with remove button
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8752ed8 and 148b7c5.

📒 Files selected for processing (1)
  • gp-advanced-select/gpadvs-dropdown-remove-button.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • gp-advanced-select/gpadvs-dropdown-remove-button.js
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Danger JS

🪧 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 generate docstrings to generate docstrings for this 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.

@veryspry
Copy link
Contributor Author

@saifsultanc good call! I added a conditional along with a comment detailing how this can be applied to specific field type or all GPADVS fields:

8752ed8

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 (4)
gp-advanced-select/gpadvs-clear-button.js (3)

5-5: Fix the typo in the documentation.

There's a typo in "Clear buton" - it should be "Clear button".

- * Adds a Clear buton to GP Advanced Select fields.
+ * Adds a Clear button to GP Advanced Select fields.

5-7: Improve documentation clarity.

The documentation could be more specific about which field types this applies to by default, similar to how it's described in the dropdown-remove-button file.

 * Adds a Clear button to GP Advanced Select fields.
+ * By default, this is only applied to Dropdown fields.
 * 
 * The Clear Button is a built in plugin of Tom Select.

24-26: Consider i18n for the button title.

The title "Clear options" is hardcoded, which might not be ideal for internationalization. Consider using a similar approach as in the remove-button implementation where it checks for localized strings first.

            settings.plugins.clear_button = {
-                title: 'Clear options',
+                title: window.GPADVS.strings?.clear_options
+                    ? window.GPADVS.strings.clear_options
+                    : 'Clear options',
            };
gp-advanced-select/gpadvs-dropdown-remove-button.js (1)

19-23: Add safety check for window.GPADVS.

While you're using optional chaining for strings?.remove_this_item, it's good practice to also check if window.GPADVS exists to prevent potential errors.

    settings.plugins.remove_button = {
-        title: window.GPADVS.strings?.remove_this_item
-            ? window.GPADVS.strings.remove_this_item
+        title: window.GPADVS?.strings?.remove_this_item
+            ? window.GPADVS.strings.remove_this_item
            : 'Remove this item',
    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5a8881b and 8752ed8.

📒 Files selected for processing (2)
  • gp-advanced-select/gpadvs-clear-button.js (1 hunks)
  • gp-advanced-select/gpadvs-dropdown-remove-button.js (1 hunks)
🔇 Additional comments (2)
gp-advanced-select/gpadvs-clear-button.js (1)

15-31: Implementation looks good!

The implementation correctly adds the clear button plugin to dropdown fields only, with helpful comments about how to adjust the target field types if needed.

gp-advanced-select/gpadvs-dropdown-remove-button.js (1)

16-27: Consistent i18n implementation is good.

The implementation using a configurable title via window.GPADVS.strings is good for internationalization, similar to how one would implement localization in WordPress.

Comment on lines 5 to 7
* Adds a Remove button to selection options in Dropdown fields. By default,
* the remove button is only added to Multi-Select fields.
*
Copy link

@coderabbitai coderabbitai bot Mar 18, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Documentation and implementation mismatch.

The documentation states this adds a Remove button to Dropdown fields and is only added to Multi-Select fields by default. However, the implementation applies to all field types without any conditional check (unlike the clear-button implementation).

Either update the documentation to match the implementation or add a conditional check similar to the clear-button implementation:

function(settings, gpadvsInstance, selectNamespace) {
+    /**
+     * Scope to only multi-select fields.
+     * This conditional can be modified to target different field types.
+     */
+    if (gpadvsInstance.fieldType === 'multiselect') {
        settings.plugins.remove_button = {
            title: window.GPADVS.strings?.remove_this_item
                ? window.GPADVS.strings.remove_this_item
                : 'Remove this item',
        }
+    }

    return settings;
}
📝 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
* Adds a Remove button to selection options in Dropdown fields. By default,
* the remove button is only added to Multi-Select fields.
*
function(settings, gpadvsInstance, selectNamespace) {
/**
* Scope to only multi-select fields.
* This conditional can be modified to target different field types.
*/
if (gpadvsInstance.fieldType === 'multiselect') {
settings.plugins.remove_button = {
title: window.GPADVS.strings?.remove_this_item
? window.GPADVS.strings.remove_this_item
: 'Remove this item',
}
}
return settings;
}

Copy link
Contributor Author

@veryspry veryspry Mar 20, 2025

Choose a reason for hiding this comment

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

Nice one, code rabbit :finger-guns. Updated ✅

Copy link

Choose a reason for hiding this comment

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

@veryspry Thank you for implementing the suggested change! This will ensure the implementation aligns with the documentation and targets only multi-select fields as intended.

(\ /)
(・ᴗ・)
/ >🥕

@veryspry veryspry requested a review from saifsultanc March 20, 2025 15:09
Copy link
Contributor

@saifsultanc saifsultanc left a comment

Choose a reason for hiding this comment

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

Seems code rabbit is sending carrots your way, let's get this merged then! 👍

@veryspry veryspry merged commit faaa3e3 into master Mar 21, 2025
4 of 5 checks passed
@veryspry veryspry deleted the matt/gpadvs-x branch March 21, 2025 14:35
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.

2 participants