Skip to content

Commit

Permalink
Ensure components file recompiles when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Nov 1, 2024
1 parent 07605ff commit a39d0f8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/mix/tasks/lvn.swiftui.gen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ defmodule Mix.Tasks.Lvn.Swiftui.Gen do
context: context,
assigns: %{
app_namespace: inspect(context.base_module),
test?: false,
gettext: true,
version: version,
live_form?: live_form_opt? && live_form_app?
Expand Down
43 changes: 26 additions & 17 deletions priv/templates/lvn.swiftui.gen/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule <%= inspect context.web_module %>.CoreComponents.<%= inspect context.m

use LiveViewNative.Component<%= if @live_form? do %>

import LiveViewNative.LiveForm.Component
import LiveViewNative.LiveForm.Component<%= if @test? do %>
@external_resource "priv/templates/lvn.swiftui.gen/core_components.ex"<% end %>

@doc """
Renders an input with label and error messages.
Expand Down Expand Up @@ -85,20 +86,28 @@ defmodule <%= inspect context.web_module %>.CoreComponents.<%= inspect context.m
slot :inner_block

def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|> assign_new(:value, fn -> field.value end)
|> assign(
:rest,
Map.put(assigns.rest, :style, [
Map.get(assigns.rest, :style, ""),
(if assigns.readonly or Map.get(assigns.rest, :disabled, false), do: "disabled(true)", else: ""),
(if assigns.autocomplete == "off", do: "textInputAutocapitalization(.never) autocorrectionDisabled()", else: "")
] |> Enum.join(" "))
)
|> input()
assigns =
assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|> assign_new(:value, fn -> field.value end)

styles =
[{:readonly, assigns.readonly} , {:autocomplete, assigns.autocomplete}]
|> Enum.reduce([], fn
{:readyonly, true}, styles -> ["disabled(true)" | styles]
{:autocomplete, "off"}, styles -> ["textInputAutocapitalization(.never)", "autocorrectionDisabled()" | styles]
_, styles -> styles
end)

style =
Map.get(assigns.rest, :style, "")
|> String.split(";")
|> Kernel.++(Enum.reverse(styles))
|> Enum.join(";")

input(put_in(assigns, [:rest, :style], style))
end

def input(%{type: "hidden"} = assigns) do
Expand Down Expand Up @@ -373,8 +382,8 @@ defmodule <%= inspect context.web_module %>.CoreComponents.<%= inspect context.m
## Examples
<.simple_form for={@form} phx-change="validate" phx-submit="save">
<.input field={@form[:email]} label="Email"/>
<.input field={@form[:username]} label="Username" />
<.input type="TextField" field={@form[:email]} label="Email"/>
<.input type="TextField" field={@form[:username]} label="Username" />
<:actions>
<.button type="submit">Save</.button>
</:actions>
Expand Down
26 changes: 25 additions & 1 deletion test/live_view_native/swiftui/core_components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule LiveViewNative.SwiftUI.CoreComponentsTest do

import LiveViewNativeTest.CoreComponents.SwiftUI

import LiveViewNative.LiveForm.Component
import LiveViewNative.Component, only: [sigil_LVN: 2]
import LiveViewNative.Template.Parser, only: [parse_document!: 1]

Expand Down Expand Up @@ -64,7 +65,30 @@ defmodule LiveViewNative.SwiftUI.CoreComponentsTest do
end

describe "simple_form/1" do

test "can render form with an input" do
params = %{"email" => "test@example.com"}
form = to_form(params, as: "user")

assigns = %{form: form}

template = ~LVN"""
<.simple_form for={@form}>
<.input field={@form[:email]} label="Email"/>
</.simple_form>
"""

assert t2h(template) ==
~X"""
<LiveForm>
<Form>
<VStack alignment="leading">
<TextField id="user_email" name="user[email]" style="" text="test@example.com">Email</TextField>
</VStack>
<Section/>
</Form>
</LiveForm>
"""
end
end

describe "button/1" do
Expand Down
14 changes: 0 additions & 14 deletions test/support/components/core_components.ex

This file was deleted.

15 changes: 15 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
File.read!("priv/templates/lvn.swiftui.gen/core_components.ex")
|> EEx.eval_string([
context: %{
web_module: LiveViewNativeTest,
module_suffix: SwiftUI
},
assigns: %{
live_form?: true,
gettext: true,
version: Application.spec(:live_view_native_swiftui)[:vsn],
test?: true
}
])
|> Code.eval_string()

{:ok, _} = LiveViewNativeTest.Endpoint.start_link()

Mix.shell(Mix.Shell.Process)
Expand Down

0 comments on commit a39d0f8

Please sign in to comment.