Adding Data Attributes to Wrapper through Component

19 views
Skip to first unread message

Christine Panus

unread,
Mar 20, 2019, 7:28:25 PM3/20/19
to SimpleForm
I am working on adding a data attribute into the item_wrapper_tag for a given input, based on values entered in the input.  For example, I have the input referencing a custom component as follows:

= f.input :cat_and_bear,
  hide_if: { select_list: 'dog' },
  required: true

I have a custom component created, it does run, I can access input_options and get the value of `hide_if`.  This code is based off of this issue, which was the closest thing I was able to find to what I was trying to do.

module SimpleFormComponents
  module HideFieldComponent
    def hide_if(wrapper_options = nil)
      html_options_for(:wrapper, wrapper_options).tap do |options|
        options[:data_hide_contingent_field] = input_options[:hide_if].keys.first
        options[:data_hide_if_values] = input_options[:hide_if].values.first
      end
    end
    end
end

I would like to add the two data attributes to the item_wrapper_tag.  Currently, the output looks like this: 

<div class="input string required cat_and_bear">
  "{:data_hide_contingent_field=>:select_list, :data_hide_unless_values=>"dog"}"
  <label class="string required" for="cat_and_bear">Cat and Bear<span class="label-required">(required)</span></label>
  <input class="string required" required="required" aria-required="true" type="text" name="animal_form[cat_and_bear]" id="cat_and_bear">
</div>

I have been looking for how to access the wrapper to add to the html_options, but I have not been able to figure it out.  Can someone help or point me to resources where this has been discussed?

Thanks,
Christine

Christine Panus

unread,
Mar 24, 2019, 5:00:29 PM3/24/19
to SimpleForm
I gave up and added the data attributes to the input instead of the wrapper.

Thanks,
Christine
Reply all
Reply to author
Forward
0 new messages