|
| 1 | +defmodule PeekAppSDK.UI.Odyssey.DatePicker do |
| 2 | + @moduledoc """ |
| 3 | + A date picker input component for selecting dates. |
| 4 | +
|
| 5 | + Provides both standalone usage and convenient integration with |
| 6 | + `Phoenix.HTML.Form` via the `:field` attribute. |
| 7 | + """ |
| 8 | + |
| 9 | + use Phoenix.Component |
| 10 | + |
| 11 | + import PeekAppSDK.UI.Odyssey.Icon |
| 12 | + |
| 13 | + @doc """ |
| 14 | + Renders a date input with a calendar icon. |
| 15 | +
|
| 16 | + ## Examples |
| 17 | +
|
| 18 | + <.odyssey_date_picker field={@form[:expiration_date]} label="Expiration Date" /> |
| 19 | + <.odyssey_date_picker field={@form[:start_date]} label="Start Date" required /> |
| 20 | + """ |
| 21 | + attr :id, :any, default: nil |
| 22 | + attr :name, :any |
| 23 | + attr :label, :string, default: nil |
| 24 | + attr :value, :any |
| 25 | + attr :errors, :list, default: [] |
| 26 | + attr :class, :string, default: nil, doc: "additional classes for the input" |
| 27 | + |
| 28 | + attr :field, Phoenix.HTML.FormField, |
| 29 | + doc: "a form field struct retrieved from the form, for example: @form[:expiration_date]" |
| 30 | + |
| 31 | + attr :rest, :global, include: ~w(autocomplete disabled form max min readonly required step) |
| 32 | + |
| 33 | + def odyssey_date_picker(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do |
| 34 | + errors = if Phoenix.Component.used_input?(field), do: field.errors, else: [] |
| 35 | + simple_errors = Enum.map(errors, fn {msg, _opts} -> msg end) |
| 36 | + |
| 37 | + assigns |
| 38 | + |> assign(field: nil, id: assigns.id || field.id) |
| 39 | + |> assign_new(:errors, fn -> simple_errors end) |
| 40 | + |> assign_new(:name, fn -> field.name end) |
| 41 | + |> assign_new(:value, fn -> field.value end) |
| 42 | + |> odyssey_date_picker() |
| 43 | + end |
| 44 | + |
| 45 | + def odyssey_date_picker(assigns) do |
| 46 | + ~H""" |
| 47 | + <fieldset class="fieldset mb-2"> |
| 48 | + <span :if={@label} class="label !mb-0 block">{@label}</span> |
| 49 | + <div class="relative inline-block w-[200px]"> |
| 50 | + <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none z-10"> |
| 51 | + <.odyssey_icon name="hero-calendar-days" class="size-5 text-neutrals-300" /> |
| 52 | + </div> |
| 53 | + <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none z-10"> |
| 54 | + <.odyssey_icon name="hero-chevron-down" class="size-5 text-interaction-300" /> |
| 55 | + </div> |
| 56 | + <input |
| 57 | + type="date" |
| 58 | + name={@name} |
| 59 | + id={@id} |
| 60 | + value={Phoenix.HTML.Form.normalize_value("date", @value)} |
| 61 | + class={[ |
| 62 | + "input pl-11 pr-10 w-[200px] cursor-pointer !outline-none", |
| 63 | + "before:absolute before:left-11 before:top-1/2 before:-translate-y-1/2", |
| 64 | + "before:text-neutrals-300 before:pointer-events-none", |
| 65 | + "[&::-webkit-calendar-picker-indicator]:opacity-0", |
| 66 | + "[&::-webkit-calendar-picker-indicator]:absolute", |
| 67 | + "[&::-webkit-calendar-picker-indicator]:inset-0", |
| 68 | + "[&::-webkit-calendar-picker-indicator]:w-full", |
| 69 | + "[&::-webkit-calendar-picker-indicator]:h-full", |
| 70 | + "[&::-webkit-calendar-picker-indicator]:cursor-pointer", |
| 71 | + "[&::-webkit-datetime-edit]:text-neutrals-300", |
| 72 | + "[&::-webkit-datetime-edit]:relative", |
| 73 | + "[&::-webkit-datetime-edit]:left-[32px]", |
| 74 | + "[&:not(:focus):invalid::-webkit-datetime-edit]:opacity-0", |
| 75 | + "[&:not(:focus):invalid]:before:block", |
| 76 | + "[&:focus]:before:hidden", |
| 77 | + "[&:valid]:before:hidden", |
| 78 | + @errors != [] && "input-error", |
| 79 | + @class |
| 80 | + ]} |
| 81 | + {@rest} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + <.error :for={msg <- @errors}>{msg}</.error> |
| 85 | + </fieldset> |
| 86 | + """ |
| 87 | + end |
| 88 | + |
| 89 | + defp error(assigns) do |
| 90 | + ~H""" |
| 91 | + <p class="mt-1.5 flex gap-2 items-center text-sm text-error"> |
| 92 | + <.odyssey_icon name="hero-exclamation-circle" class="size-5" /> |
| 93 | + {render_slot(@inner_block)} |
| 94 | + </p> |
| 95 | + """ |
| 96 | + end |
| 97 | +end |
0 commit comments