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.



63
64
65
66
67
68
69
# File 'app/components/solidus_admin/ui/table/component.rb', line 63

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



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

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

#current_scope_nameObject



174
175
176
# File 'app/components/solidus_admin/ui/table/component.rb', line 174

def current_scope_name
  @search.current_scope.name
end

#initial_modeObject



178
179
180
181
182
183
184
185
# File 'app/components/solidus_admin/ui/table/component.rb', line 178

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/components/solidus_admin/ui/table/component.rb', line 103

def render_batch_action_button(batch_action)
  params = {
    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.label,
    scheme: :secondary,
  }

  if batch_action.require_confirmation
    params["data-action"] = "click->#{stimulus_id}#confirmAction"
    params["data-#{stimulus_id}-message-param"] = t(
      ".action_confirmation",
      action: batch_action.label.downcase
    )
    params["data-#{stimulus_id}-resource-singular-param"] = @data.singular_name.downcase
    params["data-#{stimulus_id}-resource-plural-param"] = @data.plural_name.downcase
  end

  render component("ui/button").new(**params)
end

#render_data_cell(column, data) ⇒ Object



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

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



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

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



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

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

#search_form_idObject



99
100
101
# File 'app/components/solidus_admin/ui/table/component.rb', line 99

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

#selectable_columnObject



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

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)


187
188
189
# File 'app/components/solidus_admin/ui/table/component.rb', line 187

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