mirror of
https://github.com/woutdp/live_svelte
synced 2026-05-24 09:28:21 +00:00
Add composition example
This commit is contained in:
parent
52ca9f8d81
commit
f9d688960d
4 changed files with 49 additions and 0 deletions
17
example_project/assets/svelte/CompositionParent.svelte
Normal file
17
example_project/assets/svelte/CompositionParent.svelte
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<script lang="ts">
|
||||
import TextInput from "./TextInput.svelte";
|
||||
|
||||
export let live;
|
||||
|
||||
let textInput = "Content";
|
||||
|
||||
function handleForm() {
|
||||
live.pushEvent("validate-item", { name: textInput });
|
||||
}
|
||||
</script>
|
||||
|
||||
<form on:submit|preventDefault={handleForm}>
|
||||
<TextInput value={textInput} name="element" id="123456" />
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
|
||||
17
example_project/assets/svelte/TextInput.svelte
Normal file
17
example_project/assets/svelte/TextInput.svelte
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<script lang="ts">
|
||||
export let value: string;
|
||||
export let name: string;
|
||||
export let id: string;
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<input
|
||||
type="text"
|
||||
{name}
|
||||
{id}
|
||||
bind:value
|
||||
class="mt-2 block w-full rounded-lg text-zinc-900 focus:ring-2
|
||||
sm:text-sm sm:leading-6 dark:bg-zinc-700 dark:text-zinc-100
|
||||
inlinedark:placeholder-zinc-300"
|
||||
/>
|
||||
</div>
|
||||
14
example_project/lib/example_web/live/live_composition.ex
Normal file
14
example_project/lib/example_web/live/live_composition.ex
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
defmodule ExampleWeb.LiveComposition do
|
||||
use ExampleWeb, :live_view
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<.svelte name="CompositionParent" socket={@socket} />
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("validate-item", %{"name" => name}, socket) do
|
||||
IO.puts(name)
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
|
@ -33,6 +33,7 @@ defmodule ExampleWeb.Router do
|
|||
live "/svelvet", LiveSvelvet
|
||||
live "/live-json", LiveJson
|
||||
live "/slots-experiment", LiveSlotsExperiment
|
||||
live "/composition", LiveComposition
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
|
|
|||
Loading…
Reference in a new issue