I want a component that adds 2 things. Firstly, a feedback for the user to see how many characters has left(later updated via JS). Secondly, an attribute(field limit) to the input for the JS to use.
I've followed this, and pretty much just coping/pasting from one of their built-in components. Feedback is displayed correctly but :maxlength
is not present at the input.
module SimpleForm
module Components
module CharCounter
def char_counter(wrapper_options = nil)
if options[:char_counter].present?
input_html_options[:maxlength] = limit
"<span>#{limit - object.read_attribute(attribute_name).length }</span>".html_safe
end
end
def has_char_counter?
char_counter.present?
end
end
end
end
wrapper.rb
...
b.optional :char_counter, wrap_with: { tag: 'p', class: 'help-block text-right' }
...
html output
<div class="form-group string required activity_name">
<label class="string required control-label" for="activity_name">
Name <abbr title="required">*</abbr>
</label>
<input class="string required form-control" type="text" value="" name="activity[name]" id="activity_name">
<p class="help-block text-right">
<span>73</span>
</p>
</div>
Thank you!
So after a few days of dig through SimpleForm code I realize that the elements order is quite important
wrapper.rb
b.optional :char_counter, wrap_with: { tag: 'p', class: 'help-block text-right' }
must be before
b.use :input