|
| 1 | +class HotwireCombobox::Listbox::Item |
| 2 | + class << self |
| 3 | + def collection_for(view, options, render_in:, include_blank:, **custom_methods) |
| 4 | + new(view, options, render_in: render_in, include_blank: include_blank, **custom_methods).items |
| 5 | + end |
| 6 | + end |
| 7 | + |
| 8 | + def initialize(view, options, render_in:, include_blank:, **custom_methods) |
| 9 | + @view = view |
| 10 | + @options = options |
| 11 | + @render_in = render_in |
| 12 | + @include_blank = include_blank |
| 13 | + @custom_methods = custom_methods |
| 14 | + end |
| 15 | + |
| 16 | + def items |
| 17 | + items = groups_or_options |
| 18 | + items.unshift(blank_option) if include_blank.present? |
| 19 | + items |
| 20 | + end |
| 21 | + |
| 22 | + private |
| 23 | + attr_reader :view, :options, :render_in, :include_blank, :custom_methods |
| 24 | + |
| 25 | + def groups_or_options |
| 26 | + if grouped? |
| 27 | + create_listbox_group options |
| 28 | + else |
| 29 | + create_listbox_options options |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def grouped? |
| 34 | + key, value = options.to_a.first |
| 35 | + value.is_a? Array |
| 36 | + end |
| 37 | + |
| 38 | + def create_listbox_group(options) |
| 39 | + options.map do |group_name, group_options| |
| 40 | + HotwireCombobox::Listbox::Group.new group_name, |
| 41 | + options: create_listbox_options(group_options) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + def create_listbox_options(options) |
| 46 | + options.map do |option| |
| 47 | + HotwireCombobox::Listbox::Option.new **option_attrs(option) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def option_attrs(option) |
| 52 | + case option |
| 53 | + when Hash |
| 54 | + option.tap do |attrs| |
| 55 | + attrs[:content] = render_content(object: attrs[:display], attrs: attrs) if render_in.present? |
| 56 | + end |
| 57 | + when String |
| 58 | + {}.tap do |attrs| |
| 59 | + attrs[:display] = option |
| 60 | + attrs[:value] = option |
| 61 | + attrs[:content] = render_content(object: attrs[:display], attrs: attrs) if render_in.present? |
| 62 | + end |
| 63 | + when Array |
| 64 | + {}.tap do |attrs| |
| 65 | + attrs[:display] = option.first |
| 66 | + attrs[:value] = option.last |
| 67 | + attrs[:content] = render_content(object: attrs[:display], attrs: attrs) if render_in.present? |
| 68 | + end |
| 69 | + else |
| 70 | + {}.tap do |attrs| |
| 71 | + attrs[:id] = view.hw_call_method_or_proc(option, custom_methods[:id]) if custom_methods[:id] |
| 72 | + attrs[:display] = view.hw_call_method_or_proc(option, custom_methods[:display]) if custom_methods[:display] |
| 73 | + attrs[:value] = view.hw_call_method_or_proc(option, custom_methods[:value] || :id) |
| 74 | + |
| 75 | + if render_in.present? |
| 76 | + attrs[:content] = render_content(object: option, attrs: attrs) |
| 77 | + elsif custom_methods[:content] |
| 78 | + attrs[:content] = view.hw_call_method_or_proc(option, custom_methods[:content]) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + def render_content(render_opts: render_in, object:, attrs:) |
| 85 | + view.render **render_opts.reverse_merge( |
| 86 | + object: object, |
| 87 | + locals: { combobox_display: attrs[:display], combobox_value: attrs[:value] }) |
| 88 | + end |
| 89 | + |
| 90 | + def blank_option |
| 91 | + display, content = extract_blank_display_and_content |
| 92 | + HotwireCombobox::Listbox::Option.new display: display, content: content, value: "", blank: true |
| 93 | + end |
| 94 | + |
| 95 | + def extract_blank_display_and_content |
| 96 | + if include_blank.is_a? Hash |
| 97 | + text = include_blank.delete(:text) |
| 98 | + |
| 99 | + [ text, render_content(render_opts: include_blank, object: text, attrs: { display: text, value: "" }) ] |
| 100 | + else |
| 101 | + [ include_blank, include_blank ] |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments