-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
@use "katalyst/tables"; | ||
|
||
:where(th.type-enum, td.type-enum) { | ||
width: var(--width-small); | ||
} | ||
|
||
:where(td.type-enum span) { | ||
--background-color: var(--site-primary-light); | ||
--color: var(--site-on-primary); | ||
background: var(--background-color); | ||
color: var(--color); | ||
border-radius: 0.25rem; | ||
font-size: var(--paragraph--small); | ||
padding: 0.25rem 0.5rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Koi | ||
module Tables | ||
module Cells | ||
# Displays an enum value using data inferred from the model. | ||
class EnumComponent < Katalyst::Tables::CellComponent | ||
def rendered_value | ||
if (value = self.value).present? | ||
label = t(i18n_enum_label_key(value), default: value) | ||
tag.span(label, data: { enum: column, value: }) | ||
end | ||
end | ||
|
||
private | ||
|
||
def default_html_attributes | ||
{ class: "type-enum" } | ||
end | ||
|
||
def i18n_enum_label_key(value) | ||
"active_record.attributes.#{collection.model_name.i18n_key}/#{column}.#{value}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Koi::Tables::Cells::EnumComponent do | ||
let(:table) { Koi::Tables::TableComponent.new(collection:) } | ||
let(:collection) { create_list(:banner, 1, status: "published") } | ||
let(:rendered) { render_inline(table) { |row, _post| row.enum(:status) } } | ||
let(:label) { rendered.at_css("thead th") } | ||
let(:data) { rendered.at_css("tbody td") } | ||
|
||
it "renders column header" do | ||
expect(label).to match_html(<<~HTML) | ||
<th class="type-enum">Status</th> | ||
HTML | ||
end | ||
|
||
it "renders column data" do | ||
expect(data).to match_html(<<~HTML) | ||
<td class="type-enum"><span data-enum="status" data-value="published">Published</span></td> | ||
HTML | ||
end | ||
|
||
context "with html_options" do | ||
let(:rendered) { render_inline(table) { |row| row.enum(:status, **Test::HTML_ATTRIBUTES) } } | ||
|
||
it "renders header with html_options" do | ||
expect(label).to match_html(<<~HTML) | ||
<th id="ID" class="type-enum CLASS" style="style" data-foo="bar" aria-label="LABEL">Status</th> | ||
HTML | ||
end | ||
|
||
it "renders data with html_options" do | ||
expect(data).to match_html(<<~HTML) | ||
<td id="ID" class="type-enum CLASS" style="style" data-foo="bar" aria-label="LABEL"><span data-enum="status" data-value="published">Published</span></td> | ||
HTML | ||
end | ||
end | ||
|
||
context "when given a label" do | ||
let(:rendered) { render_inline(table) { |row| row.enum(:status, label: "LABEL") } } | ||
|
||
it "renders header with label" do | ||
expect(label).to match_html(<<~HTML) | ||
<th class="type-enum">LABEL</th> | ||
HTML | ||
end | ||
|
||
it "renders data without label" do | ||
expect(data).to match_html(<<~HTML) | ||
<td class="type-enum"><span data-enum="status" data-value="published">Published</span></td> | ||
HTML | ||
end | ||
end | ||
|
||
context "when given an empty label" do | ||
let(:rendered) { render_inline(table) { |row| row.enum(:status, label: "") } } | ||
|
||
it "renders header with an empty label" do | ||
expect(label).to match_html(<<~HTML) | ||
<th class="type-enum"></th> | ||
HTML | ||
end | ||
end | ||
|
||
context "with nil data value" do | ||
let(:rendered) { render_inline(table) { |row| row.enum(:status) } } | ||
let(:collection) { create_list(:banner, 1, status: nil) } | ||
|
||
it "renders an empty cell" do | ||
expect(data).to match_html(<<~HTML) | ||
<td class="type-enum"></td> | ||
HTML | ||
end | ||
end | ||
|
||
context "when given a block" do | ||
let(:rendered) { render_inline(table) { |row| row.enum(:status) { |cell| cell.tag.span(cell) } } } | ||
|
||
it "renders the default header" do | ||
expect(label).to match_html(<<~HTML) | ||
<th class="type-enum">Status</th> | ||
HTML | ||
end | ||
|
||
it "renders the custom data" do | ||
expect(data).to match_html(<<~HTML) | ||
<td class="type-enum"><span><span data-enum="status" data-value="published">Published</span></span></td> | ||
HTML | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class Banner < ApplicationRecord | ||
enum :status, { draft: 0, published: 1, archived: 2 }, default: :draft | ||
|
||
has_one_attached :image do |image| | ||
image.variant :thumb, resize_to_fill: [100, 100] | ||
end | ||
|
||
scope :admin_search, ->(query) do | ||
where("name LIKE :query", query: "%#{query}%") | ||
end | ||
|
||
default_scope -> { order(ordinal: :asc) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
en: | ||
active_record: | ||
attributes: | ||
banner/status: | ||
draft: Draft | ||
published: Published | ||
archived: Archived |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters