Skip to content

Add form files #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/lib/forms/Form.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import type { JSONSchema7 } from "json-schema";
import { setContext } from "svelte";
import { FormService } from "./form.service.svelte";

let {
children,
schema,
value = $bindable()
}: { children: any; schema?: JSONSchema7; value?: any } = $props();

const formService = new FormService();
if (value) {
formService.data = value;
}
$effect(() => {
value = formService.data;
});
setContext("formService", formService);
</script>

{@render children()}
21 changes: 21 additions & 0 deletions src/lib/forms/FormField.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { getContext } from "svelte";
import type { FormService } from "./form.service.svelte.js";

const { path } = $props<{ path: string }>();

const formService: FormService = getContext("formService");

function handleInput(event: Event) {
const target = event.target as HTMLInputElement;
formService.updateDataForPath(path, target.value);
}
</script>

<input
type="text"
placeholder="Name"
class="input input-bordered m-2"
oninput={handleInput}
value={formService.getValueForPath(path)}
/>
Empty file.
17 changes: 17 additions & 0 deletions src/lib/forms/form.service.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FormValue = any;

export class FormService<T = FormValue> {
data = $state<T>({} as FormValue);

public getValueForPath(path: string): FormValue {
return this.data[path];
}
public updateDataForPath(path: string, value: FormValue) {
this.data = { ...this.data, [path]: value };
}

public updateData(value: FormValue): void {
this.data = value;
}
}
1 change: 1 addition & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
/>
</a>
</div>
<div class="alert alert-warning mb-8">This library is based on Svelte 5 beta versions. Even though these versions are already "release candidates", things might not work well enough for your production usage. Use at own risk.</div>
<slot></slot>
</div>
</AppShell>
32 changes: 32 additions & 0 deletions src/routes/forms/form-container/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script lang="ts">
import ComponentCode from "$lib/docs/ComponentCode.svelte";
import ComponentHeader from "$lib/docs/ComponentHeader.svelte";
import ComponentPlayground from "$lib/docs/ComponentPlayground.svelte";
import Form from "$lib/forms/Form.svelte";
import FormField from "$lib/forms/FormField.svelte";
import type { JSONSchema7 } from "json-schema";

const schema: JSONSchema7 = {
type: "object",
properties: {
firstName: {
type: "string",
title: "firstName"
}
}
};

let formValue = $state({ firstName: "Max" });
</script>

<ComponentHeader
title="Form Container"
description="The Form Container wraps Form Input Components and handles the form value."
/>

<ComponentPlayground value={formValue}>
<Form {schema} bind:value={formValue}>
<FormField path="firstName" />
<FormField path="lastName" />
</Form>
</ComponentPlayground>
Loading