Sha256: beb5286f9c64f1e0ace50d4c8dbe64636f44a43574317a7c3fa491ae97e8e1f0

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

module IRuby
  module Input
    class Checkbox < Label
      needs :options, :default

      builder :checkbox do |*args, **params|
        key = :checkbox
        key, *args = args if args.first.is_a? Symbol

        params[:key] = unique_key(key)
        params[:options] = args

        params[:default] = case params[:default]
        when false, nil
          []
        when true
          [*params[:options]]
        else
          [*params[:default]]
        end

        add_field Checkbox.new(**params)
      end

      def widget_css
        <<-CSS
          .iruby-checkbox.form-control { display: inline-table; }
          .iruby-checkbox .checkbox-inline { margin: 0 15px 0 0; }
        CSS
      end

      def widget_js
        <<-JS
          $('.iruby-checkbox input').change(function(){
            var parent = $(this).closest('.iruby-checkbox');
            $(parent).data('iruby-value', []);

            $(parent).find(':checked').each(function(){
              $(parent).data('iruby-value').push($(this).val());
            });

            if ($(parent).data('iruby-value').length == 0) {
              $(parent).data('iruby-value', null);
            }
          });

          $('.iruby-checkbox input').trigger('change');
        JS
      end

      def widget_html
        params = {
          :'data-iruby-key' => @key,
          class: 'iruby-checkbox form-control'
        }
        widget_label do
          div **params do
            @options.each do |option|
              label class: 'checkbox-inline' do
                input(
                  name: @key,
                  value: option,
                  type: 'checkbox',
                  checked: @default.include?(option)
                )
                text option
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
iruby-0.8.0 lib/iruby/input/checkbox.rb
iruby-0.7.4 lib/iruby/input/checkbox.rb
iruby-0.7.3 lib/iruby/input/checkbox.rb
iruby-0.7.2 lib/iruby/input/checkbox.rb
iruby-0.7.1 lib/iruby/input/checkbox.rb
iruby-0.7.0 lib/iruby/input/checkbox.rb
iruby-0.6.1 lib/iruby/input/checkbox.rb
iruby-0.6.0 lib/iruby/input/checkbox.rb
iruby-0.5.0 lib/iruby/input/checkbox.rb