Class: SolidusAdmin::UI::Table::Component

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/solidus_admin/ui/table/component.rb

Defined Under Namespace

Classes: Data, Search

Instance Method Summary collapse

Constructor Details

#initialize(id:, data:, search: nil, sortable: nil) ⇒ Component

Returns a new instance of Component.



59
60
61
62
63
64
65
# File 'app/components/solidus_admin/ui/table/component.rb', line 59

def initialize(id:, data:, search: nil, sortable: nil)
  @id = id
  @data = Data.new(**data)
  @data.columns.unshift selectable_column if @data.batch_actions.present? && @data.rows.present?
  @search = Search.new(**search) if search
  @sortable = Sortable.new(**sortable) if sortable
end

Instance Method Details

#batch_actions_form_idObject



91
92
93
# File 'app/components/solidus_admin/ui/table/component.rb', line 91

def batch_actions_form_id
  @batch_actions_form_id ||= "#{stimulus_id}--batch-actions-#{@id}"
end

#current_scope_nameObject



158
159
160
# File 'app/components/solidus_admin/ui/table/component.rb', line 158

def current_scope_name
  @search.current_scope.name
end

#initial_modeObject



162
163
164
165
166
167
168
169
# File 'app/components/solidus_admin/ui/table/component.rb', line 162

def initial_mode
  @initial_mode ||=
    if @search && (@search.value[@search.searchbar_key] || @search.scopes.none?)
      "search"
    else
      "scopes"
    end
end

#render_batch_action_button(batch_action) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/solidus_admin/ui/table/component.rb', line 99

def render_batch_action_button(batch_action)
  render component("ui/button").new(
    name: request_forgery_protection_token,
    value: form_authenticity_token(form_options: {
      action: batch_action.action,
      method: batch_action.method,
    }),
    formaction: batch_action.action,
    formmethod: batch_action.method,
    form: batch_actions_form_id,
    type: :submit,
    icon: batch_action.icon,
    text: batch_action.display_name,
    scheme: :secondary,
  )
end

#render_data_cell(column, data) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/components/solidus_admin/ui/table/component.rb', line 145

def render_data_cell(column, data)
  cell = column.data
  cell = cell.call(data) if cell.respond_to?(:call)
  cell = data.public_send(cell) if cell.is_a?(Symbol)
  cell = cell.render_in(self) if cell.respond_to?(:render_in)
  cell = tag.div(cell, class: "flex items-center gap-1.5 justify-start overflow-x-hidden") if column.wrap

  tag.td(cell, class: "
    py-2 px-4 h-10 vertical-align-middle leading-none
    [tr:last-child_&:first-child]:rounded-bl-lg [tr:last-child_&:last-child]:rounded-br-lg
  ")
end

#render_header_cell(cell, **attrs) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/components/solidus_admin/ui/table/component.rb', line 129

def render_header_cell(cell, **attrs)
  cell = cell.call if cell.respond_to?(:call)
  cell = @data[:class].human_attribute_name(cell) if cell.is_a?(Symbol)
  cell = cell.render_in(self) if cell.respond_to?(:render_in)

  (:th, cell, class: %{
    border-b
    border-gray-100
    px-4
    h-9
    font-semibold
    vertical-align-middle
    leading-none
  }, **attrs)
end

#render_ransack_filter_dropdown(filter, index) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/components/solidus_admin/ui/table/component.rb', line 116

def render_ransack_filter_dropdown(filter, index)
  render component("ui/table/ransack_filter").new(
    presentation: filter.presentation,
    search_param: @search.name,
    combinator: filter.combinator,
    attribute: filter.attribute,
    predicate: filter.predicate,
    options: filter.options,
    form: search_form_id,
    index: index,
  )
end

#search_form_idObject



95
96
97
# File 'app/components/solidus_admin/ui/table/component.rb', line 95

def search_form_id
  @search_form_id ||= "#{stimulus_id}--search-form-#{@id}"
end

#selectable_columnObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/components/solidus_admin/ui/table/component.rb', line 67

def selectable_column
  @selectable_column ||= Column.new(
    header: -> {
      component("ui/forms/checkbox").new(
        form: batch_actions_form_id,
        "data-action": "#{stimulus_id}#selectAllRows",
        "data-#{stimulus_id}-target": "headerCheckbox",
        "aria-label": t('.select_all'),
      )
    },
    data: ->(data) {
      component("ui/forms/checkbox").new(
        name: "id[]",
        form: batch_actions_form_id,
        value: data.id,
        "data-action": "#{stimulus_id}#selectRow",
        "data-#{stimulus_id}-target": "checkbox",
        "aria-label": t('.select_row'),
      )
    },
    col: { class: 'w-[52px]' },
  )
end

#should_enable_sortable?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'app/components/solidus_admin/ui/table/component.rb', line 171

def should_enable_sortable?
  @sortable && @search&.on_default_scope?
end