Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Table #47

Merged
merged 12 commits into from
Apr 16, 2020
33 changes: 33 additions & 0 deletions example/lib/example_web/templates/page/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,38 @@ airports = [{"Louis Armstrong", "MSY"}, {"John F. Kennedy", "JFK"}]

</section>

<section>
<%= row do %>
<%= col do %>
<h3>Table</h3>
<% end %>

<%= col do %>
<%= table do %>
<%= table_head do %>
<%= table_row do %>
<%= table_header do: "ID" %>
<%= table_header do: "First Name" %>
<%= table_header do: "Last Name" %>
<% end %>
<% end %>
<%= table_body do %>
<%= for n <- 0..9 do %>
<%= table_row do %>
<%= table_data do: n %>
<%= table_data do: "John" %>
<%= table_data do: "Doe#{n}" %>
<% end %>
<% end %>
<%= table_row do %>
<%= table_data do: "10" %>
<%= table_data do: "Jane" %>
<%= table_data do: "Doe" %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
</section>
<% end %>
<% end %>
26 changes: 25 additions & 1 deletion lib/harmonium.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ defmodule Harmonium do
defp error_helper(error) do
case Application.get_env(:harmonium, :error_helper, nil) do
nil ->
{message, opts} = error
{message, _opts} = error
message

error_helper ->
Expand Down Expand Up @@ -669,6 +669,30 @@ defmodule Harmonium do
end
end

defdelegate table_class(modifiers \\ []), to: Harmonium.Table

defdelegate table(modifiers \\ []), to: Harmonium.Table

defdelegate table_head_class(modifiers \\ []), to: Harmonium.Table

defdelegate table_head(opts \\ []), to: Harmonium.Table

defdelegate table_body_class(modifiers \\ []), to: Harmonium.Table

defdelegate table_body(opts \\ []), to: Harmonium.Table

defdelegate table_row_class(modifiers \\ []), to: Harmonium.Table

defdelegate table_row(opts \\ []), to: Harmonium.Table

defdelegate table_header_class(modifiers \\ []), to: Harmonium.Table

defdelegate table_header(opts \\ []), to: Harmonium.Table

defdelegate table_data_class(modifiers \\ []), to: Harmonium.Table

defdelegate table_data(opts \\ []), to: Harmonium.Table

@mock_form_default_form %Phoenix.HTML.Form{data: %{}, errors: [], name: "mock", id: "mock"}
@mock_form_default_inputs %{
empty: nil,
Expand Down
147 changes: 147 additions & 0 deletions lib/harmonium/table.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
defmodule Harmonium.Table do
@moduledoc """
Adds component to render pagination with correct harmonium classes.
"""

import Phoenix.HTML.Tag

@doc """
Constructs a callout class.

iex> table_class()
"rev-Table"

iex> table_class(color: "blue")
"rev-Table rev-Table--colorblue"

iex> table_class(bold: true)
"rev-Table rev-Table--bold"
"""
def table_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table", modifiers)

@doc """
Renders a table element (<table>).

iex> safe_to_string(table(do: []))
~S(<table class="rev-Table"></table>)
"""
def table(opts \\ []), do: table_element(:table, :table_class, opts)

@doc """
Constructs a table head class.

iex> table_head_class()
"rev-Table-head"

iex> table_head_class(stacked: true)
"rev-Table-head rev-Table-head--stacked"
"""
def table_head_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table-head", modifiers)

@doc """
Renders a table head element (<thead>).

iex> safe_to_string(table_head(do: []))
~S(<thead class="rev-Table-head"></thead>)
"""
def table_head(opts \\ []), do: table_element(:thead, :table_head_class, opts)

@doc """
Constructs a table body class.

iex> table_body_class()
"rev-Table-body"

iex> table_body_class(stacked: true)
"rev-Table-body rev-Table-body--stacked"
"""
def table_body_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table-body", modifiers)

@doc """
Renders a table body element (<tbody>).

iex> safe_to_string(table_body(do: []))
~S(<tbody class="rev-Table-body"></tbody>)
"""
def table_body(opts \\ []), do: table_element(:tbody, :table_body_class, opts)

@doc """
Constructs a table row class.

iex> table_row_class()
"rev-Table-row"

iex> table_row_class(stacked: true)
"rev-Table-row rev-Table-row--stacked"
"""
def table_row_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table-row", modifiers)

@doc """
Renders a table row element (<tr>).

iex> safe_to_string(table_row(do: []))
~S(<tr class="rev-Table-row"></tr>)
"""
def table_row(opts \\ []), do: table_element(:tr, :table_row_class, opts)

@doc """
Constructs a table header class.

iex> table_header_class()
"rev-Table-header"

iex> table_header_class(stacked: true)
"rev-Table-header rev-Table-header--stacked"
"""
def table_header_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table-header", modifiers)

@doc """
Renders a table header element (<th>).

iex> safe_to_string(table_header(do: []))
~S(<th class="rev-Table-header"></th>)
"""
def table_header(opts \\ []), do: table_element(:th, :table_header_class, opts)

@doc """
Constructs a table data class.

iex> table_data_class()
"rev-Table-data"

iex> table_data_class(stacked: true)
"rev-Table-data rev-Table-data--stacked"
"""
def table_data_class(modifiers \\ []), do: Harmonium.rev_class("rev-Table-data", modifiers)

@doc """
Renders a table data element (<td>).

iex> safe_to_string(table_data)
~S(<td class="rev-Table-data"></td>)

iex> safe_to_string(table_data(attrs: [colspan: 2]))
~S(<td class="rev-Table-data" colspan="2"></td>)

iex> safe_to_string(table_data(do: []))
~S(<td class="rev-Table-data"></td>)

iex> safe_to_string(table_data(bold: true, attrs: [colspan: 2], do: []))
~S(<td class="rev-Table-data rev-Table-data--bold" colspan="2"></td>)

iex> safe_to_string(table_data(bold: true, italic: true, attrs: [colspan: 2], do: []))
~S(<td class="rev-Table-data rev-Table-data--bold rev-Table-data--italic" colspan="2"></td>)
"""
def table_data(opts \\ []), do: table_element(:td, :table_data_class, opts)

defp table_element(tag, class_function, opts) do
{block, opts} = Keyword.pop(opts, :do, [])
{attrs, modifiers} = Keyword.pop(opts, :attrs, [])
modifier_classes = apply(__MODULE__, class_function, [modifiers])
opts = Keyword.merge(attrs, [class: modifier_classes])

content_tag tag, opts do
block
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have said this before, but I think all these helper functions should follow the current standard Harmonium pattern where you can optionally pass CSS class modifiers as the 2nd arg:

  @doc """
  Constructs a callout class.
  """
  def callout_class(modifiers \\ []), do: rev_class(@callout_class, modifiers)

  @doc """
  Renders a callout.
  """
  def callout(do: block), do: callout([], do: block)

  def callout(modifiers, do: block), do: callout(:div, modifiers, do: block)

  def callout(tag, modifiers, do: block) do
    content_tag tag, class: callout_class(modifiers) do
      block
    end
  end

Our Harmonium SCSS Table does include some very useful SUIT modifiers: https://github.com/revelrylabs/harmonium/blob/master/scss/components/_Table.scss

2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [], [], "hexpm"},
"decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "2.2.10", "e7366dc82f48f8dd78fcbf3ab50985ceeb11cb3dc93435147c6e13f2cda0992e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
7 changes: 7 additions & 0 deletions test/harmonium/table_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Harmonium.TableTest do
use ExUnit.Case
import Phoenix.HTML, only: [safe_to_string: 1]
import Harmonium.Table

doctest Harmonium.Table
end