Skip to content

When hiding all fields in a fieldset, the fieldset remains, but is empty. it should be hidden too. #23

@plazmus

Description

@plazmus

If all the fields in a fieldset are hidden, then the fieldset itself should be hidden rather than displaying an empty fieldset.

This is a working patch taken from https://www.drupal.org/project/conditional_fields/issues/1903226

---
 conditional_fields.js | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/conditional_fields.js b/conditional_fields.js
index 148f793..d16eca7 100644
--- a/conditional_fields.js
+++ b/conditional_fields.js
@@ -141,4 +141,24 @@ Backdrop.behaviors.conditionalFields = {
   }
 };
 
+// Hide fieldsets unless one of their fields is visible.
+  $(document).bind('state:visible', function(e) {
+    if (e.trigger) {
+      var $wrapper = $(e.target).closest('.fieldset-wrapper');
+      if ($wrapper.length) {
+        var hidden = true;
+        $wrapper.children(':not(.fieldset-description)').each(function (index, element) {
+          if ($(element).css('display') != 'none') {
+            hidden = false;
+          }
+        });
+        if (hidden) {
+          $wrapper.closest('fieldset').hide();
+        } else {
+          $wrapper.closest('fieldset').show();
+        }
+      }
+    }
+  });
+
 })(jQuery);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions