| 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 | # File 'app/helpers/spark/input_helper.rb', line 4
def input_tag(type, name, value, options=nil, &block)
  type.to_sym! if type.is_a?( String )
  if value.is_a? Hash
    options = value
    value = nil
  end
  options = Spark::Helpers.set_type_defaults( type, options )
  options = Spark::Helpers.set_data_keys( options, 'default' )
  options = Spark::Helpers.set_aria_keys( options )
  icon = Esvg.use( icon ) if icon = options.delete('icon')
  icon_after = Esvg.use( icon_after ) if icon_after = options.delete('icon_after')
  options['data']['unique-id'] = "input-#{SecureRandom.hex(8)}"
  options['autocomplete'] ||= 'off' if type != :textarea
  label_options = { 
    data: { 'input-type' => type },
    class: [ label_class( type ), options.delete( 'class' ) ]
  }
    %w(show-panel show-dialog show-menu).each do |opt|
    if val = options['data'].delete(opt)
      label_options[:data][opt] = val
    end
  end
    if type != :textarea && placeholder = options['placeholder']
    placeholder = content_tag(:span, class: 'input-placeholder-text') { placeholder }
    label_options = add_class label_options, 'placeholder-label'
  end
  if label_description = options.delete('description')
    options['aria-describedby'] = options['data']['unique-id'] + '-description'
    label_description = content_tag(:span, id: options['aria-describedby'], class: 'label-description') { label_description }
    label_options = add_class label_options, 'has-description'
  end
  if options['label'] && label_text = options.delete('label')
    options['aria-labelledby'] = options['data']['unique-id'] + '-label'
    label_text = content_tag(:span, class: 'input-label-text') {
      content_tag(:span, id: options['aria-labelledby']){ label_text }
    }
  end
    options['aria-label'] = options['placeholder'] if placeholder && !label_text
  input = base_input_tag( name, value, options, type, &block )
  content_tag(:div, label_options) do
    concat( label_text )
    concat( label_description )
    concat content_tag( :span, class: 'input-wrapper' ) {
      if type == :select
        concat( input )
              else
        concat content_tag( :span, class: 'input-icon' ) { icon } if icon
        concat content_tag( :span, class: 'input-container' ) {
          concat( input )
                  }
        if type == :search 
          concat button_tag( type: 'button', class: 'clear-search', "aria-label" => 'Clear search', disabled: !value, "data-clear-input" => "[data-unique-id=#{options['data']['unique-id']}]" ) { 'x' }
        end
        concat content_tag( :span, class: 'input-icon after' ) { icon_after } if icon_after
        if block_given? && type != :textarea
          html = capture(&block)
          if html.match(/<select.*?>/)
            selected = html.match(/<option.*?selected.*?>(.+?)<\/option>/m) || html.match(/<option.*?>(.+?)<\/option/m)
            html.sub!(/<select/, "<select data-sync-selected='[data-sync=#{options['data']['unique-id']}]' ")
            html << "<span class='select-text' data-sync='#{options['data']['unique-id']}'>#{selected[1]}</span>"
          end
          concat content_tag( :span, class: 'input-extra' ) { html.html_safe }
        end
      end
    }
  end
end |