defmodule GitSyncWeb.CoreComponents do @moduledoc """ Provides core UI components. At first glance, this module may seem daunting, but its goal is to provide core building blocks for your application, such as tables, forms, and inputs. The components consist mostly of markup and are well-documented with doc strings and declarative assigns. You may customize and style them in any way you want, based on your application growth and needs. Styling is hand-written CSS in `assets/css/app.css`; components carry semantic class names rather than utility classes. * [Phoenix.Component](https://phoenix-live-view.hexdocs.pm/Phoenix.Component.html) - the component system used by Phoenix. Some components, such as `<.link>` and `<.form>`, are defined there. """ use Phoenix.Component @doc """ Renders flash notices. ## Examples <.flash kind={:info} flash={@flash} /> <.flash id="welcome-back" kind={:info}>Welcome Back! """ attr :id, :string, doc: "the optional id of flash container" attr :flash, :map, default: %{}, doc: "the map of flash messages to display" attr :title, :string, default: nil attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup" attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container" slot :inner_block, doc: "the optional inner block that renders the flash message" def flash(assigns) do assigns = assign_new(assigns, :id, fn -> "flash-#{assigns.kind}" end) ~H""" """ end @doc """ Renders a button with navigation support. ## Examples <.button>Send! <.button variant="secondary">Send! <.button navigate={~p"/"}>Home """ attr :rest, :global, include: ~w(href navigate patch method download name value disabled type) attr :class, :any attr :variant, :string, values: ~w(primary secondary), default: "primary" slot :inner_block, required: true def button(%{rest: rest} = assigns) do assigns = assign_new(assigns, :class, fn -> assigns.variant end) if rest[:href] || rest[:navigate] || rest[:patch] do ~H""" <.link class={["button", @class]} {@rest}> {render_slot(@inner_block)} """ else ~H""" """ end end @doc """ Renders an input with label and error messages. A `Phoenix.HTML.FormField` may be passed as argument, which is used to retrieve the input name, id, and values. Otherwise all attributes may be passed explicitly. ## Types This function accepts all HTML input types, considering that: * You may also set `type="select"` to render a ` """ end def input(%{type: "checkbox"} = assigns) do assigns = assign_new(assigns, :checked, fn -> Phoenix.HTML.Form.normalize_value("checkbox", assigns[:value]) end) ~H"""
<.error :for={msg <- @errors}>{msg}
""" end def input(%{type: "select"} = assigns) do ~H"""
<.error :for={msg <- @errors}>{msg}
""" end def input(%{type: "datalist"} = assigns) do assigns = assign(assigns, :list_id, "#{assigns.id}-options") ~H"""
<.error :for={msg <- @errors}>{msg}
""" end def input(%{type: "textarea"} = assigns) do ~H"""
<.error :for={msg <- @errors}>{msg}
""" end # All other inputs text, datetime-local, url, password, etc. are handled here... def input(assigns) do ~H"""
<.error :for={msg <- @errors}>{msg}
""" end # Helper used by inputs to generate form errors defp error(assigns) do ~H"""

{render_slot(@inner_block)}

""" end @doc """ Renders a header with title. """ slot :inner_block, required: true slot :subtitle slot :actions def header(assigns) do ~H"""

{render_slot(@inner_block)}

{render_slot(@subtitle)}

{render_slot(@actions)}
""" end @doc """ Renders a table with generic styling. ## Examples <.table id="users" rows={@users}> <:col :let={user} label="id">{user.id} <:col :let={user} label="username">{user.username} """ attr :id, :string, required: true attr :rows, :list, required: true attr :row_id, :any, default: nil, doc: "the function for generating the row id" attr :row_item, :any, default: &Function.identity/1, doc: "the function for mapping each row before calling the :col and :action slots" slot :col, required: true do attr :label, :string end slot :action, doc: "the slot for showing user actions in the last table column" def table(assigns) do ~H"""
{col[:label]} Actions
{render_slot(col, @row_item.(row))}
<%= for action <- @action do %> {render_slot(action, @row_item.(row))} <% end %>
""" end @doc """ Renders a data list. ## Examples <.list> <:item title="Title">{@post.title} <:item title="Views">{@post.views} """ slot :item, required: true do attr :title, :string, required: true end def list(assigns) do ~H"""
<%= for item <- @item do %>
{item.title}
{render_slot(item)}
<% end %>
""" end @doc """ Translates an error message using gettext. """ def translate_error({msg, opts}) do # You can make use of gettext to translate error messages by # uncommenting and adjusting the following code: # if count = opts[:count] do # Gettext.dngettext(GitSyncWeb.Gettext, "errors", msg, msg, count, opts) # else # Gettext.dgettext(GitSyncWeb.Gettext, "errors", msg, opts) # end Enum.reduce(opts, msg, fn {key, value}, acc -> String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end) end) end @doc """ Translates the errors for a field from a keyword list of errors. """ def translate_errors(errors, field) when is_list(errors) do for {^field, {msg, opts}} <- errors, do: translate_error({msg, opts}) end end