thedelchop

thedelchop

Surface-UI: Passing slots through to a child component

Hello,

I have been working with Surface for a little while and have been pretty thrilled with the experience and its ease of use, however, I have recently run across a use case whose implementation is unclear to me.

I have grabbed the example Table component from the Surface-UI docs to work with in my side project and its worked great.

However, I’d like to render a bunch of tables which all share the same formatting but can continue accept a <:cols> slot, so that I can individually specify the content for each, using the same <Col> component as the original table.

So here is my first attempt which does not work, but I’m hoping that someone could help me understand how this could work:

defmodule OOTPUtilityWeb.Components.Player.Attributes.Table do
  use Surface.Component

  alias OOTPUtilityWeb.Components.Shared.Table

  slot cols, args: [item: ^data], required: true
  prop attributes, :keyword, required: true
  prop id, :string, required: true

  def render(assigns) do
    ~F"""
      <Table id={@id} data={{name, ratings} <- @attributes} header_class={&header_class/2} column_class={&column_class/2}>
        <:cols>
          <#cols />
        </:cols>
      </Table>
    """
  end

  def header_class(_standing, 0) do
    do_header_class(["text-left"])
  end

  def header_class(_col, _index),
    do: do_header_class([])

  def do_header_class(extra_classes \\ []) do
    extra_classes ++ [
      "p-1",
      "md:p-2",
      "uppercase",
      "font-small",
      "md:font-medium",
      "text-left"
    ]

  end

  def column_class(_standing, 0) do
    do_column_class(["text-left"])
  end

  def column_class(_standing, _index) do
    do_column_class([])
  end

  defp do_column_class(extra_classes) do
    Enum.join(extra_classes ++ [ "p-px", "md:p-1", "whitespace-nowrap" ], " ")
  end
end

and then try to use it in a way like this:

defmodule OOTPUtilityWeb.Components.Player.Attributes.Pitches do
  use Surface.Component

  alias OOTPUtilityWeb.Components.Player.Attributes.Table
  alias OOTPUtilityWeb.Components.Shared.Table.Column

  prop pitches, :keyword, required: true

  def render(assigns) do
    ~F"""
      <Table id={"player-pitch-ratings"} attributes={@pitches}>
        <Column label={"Pitches"}>
          {attribute_name(name)}
        </Column>

        <Column label="Ability">
          {as_number(Keyword.get(ratings, :ability))}
        </Column>

        <Column label="Talent">
          {as_number(Keyword.get(ratings, :talent))}
        </Column>
      </Table>
    """
  end

  def attribute_name(attribute) do
    OOTPUtilityWeb.Helpers.capitalize_all(attribute)
  end

  def as_number(rating) do
    assigns = %{rating: rating}

    ~F"""
      <span class={"text-rating-#{@rating * 2}"}>{@rating}</span>
    """
  end
end

But I see the following compilation error:

== Compilation error in file lib/ootp_utility_web/components/player/attributes/table.ex ==
** (FunctionClauseError) no function clause matching in Code.ensure_compiled/1

    The following arguments were given to Code.ensure_compiled/1:

        # 1
        {:cols, [line: 1], nil}

    Attempted function clauses (showing 1 out of 1):

        def ensure_compiled(module) when is_atom(module)

    (elixir 1.13.2) lib/code.ex:1519: Code.ensure_compiled/1
    (surface 0.7.0) lib/surface/compiler/helpers.ex:236: Surface.Compiler.Helpers.check_module_loaded/2
    (surface 0.7.0) lib/surface/compiler/helpers.ex:260: Surface.Compiler.Helpers.validate_component_module/2
    (surface 0.7.0) lib/surface/compiler.ex:677: Surface.Compiler.convert_node_to_ast/3
    (surface 0.7.0) lib/surface/compiler.ex:198: anonymous fn/3 in Surface.Compiler.to_ast/2
    (elixir 1.13.2) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (surface 0.7.0) lib/surface/compiler.ex:197: Surface.Compiler.to_ast/2
    (surface 0.7.0) lib/surface/compiler.ex:433: Surface.Compiler.convert_node_to_ast/3

since this is a compiler error, I’m wondering if this is why Surface.quote_surface/2 exists, but TBH I’m still struggling to understand how it might help me if it did.

If anyone could help me to at least understand why Surface seems to hate this so much or how I might leverage Surface.quote_surface/2 to fix this I’d be grateful.

Thank you,

Joe

First Post!

cmo

cmo

You’re not passing it to the cols slot. Use the default <#slot /> in your inner table instead.

Or put the columns inside a <:cols> in your pitches table, but that seems unnecessary in this example.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement