Skip to content

Commit

Permalink
Extract SLForm
Browse files Browse the repository at this point in the history
  • Loading branch information
ykrods committed Sep 9, 2024
1 parent 555a4a6 commit f3404f0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ body {
}

/** form */
form.attention-error {
form {
& sl-input[data-user-invalid]::part(base),
& sl-select[data-user-invalid]::part(combobox),
& form.attention-error sl-checkbox[data-user-invalid]::part(control) {
Expand Down
19 changes: 4 additions & 15 deletions src/pages/FormBasic.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@
import Layout from "$src/layout/Layout.svelte";
import { SLForm } from "$src/shoelace";
let alert;
let alertText = $state('');
let form;
$effect(() => {
// @see https://shoelace.style/getting-started/form-controls#required-fields
customElements.whenDefined("sl-input").then(() => {
form?.addEventListener("submit", onSubmit);
});
return () => {
form?.removeEventListener("submit", onSubmit);
}
});
function onSubmit(evt) {
evt.preventDefault();
Expand All @@ -43,7 +32,7 @@
<Layout>
<h1>Form - basic</h1>

<form class="attention-error" bind:this={form}>
<SLForm {onSubmit}>
<sl-input
name="name"
type="text"
Expand All @@ -69,7 +58,7 @@
</sl-checkbox>
<br><br>
<sl-button type="submit" variant="primary">Submit</sl-button>
</form>
</SLForm>

<sl-alert type="success" duration=3000 closable bind:this={alert}>
<sl-icon slot="icon" name="check2-circle"></sl-icon>
Expand Down
20 changes: 3 additions & 17 deletions src/pages/FormReactive.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
<script lang="ts">
import "@shoelace-style/shoelace/dist/components/input/input";
import "@shoelace-style/shoelace/dist/components/select/select";
import "@shoelace-style/shoelace/dist/components/menu-item/menu-item";
import "@shoelace-style/shoelace/dist/components/checkbox/checkbox";
import "@shoelace-style/shoelace/dist/components/button/button";
import Layout from "$src/layout/Layout.svelte";
import { SLForm } from "$src/shoelace";
let confirmation = $state('');
let form;
let upper = $derived(confirmation.toUpperCase());
$effect(() => {
// @see https://shoelace.style/getting-started/form-controls#required-fields
customElements.whenDefined("sl-input").then(() => {
form?.addEventListener("submit", onSubmit);
});
return () => {
form?.removeEventListener("submit", onSubmit);
}
});
function onSubmit(evt: SubmitEvent) {
evt.preventDefault();
alert('confirmed!');
Expand All @@ -33,7 +19,7 @@
</svelte:head>
<Layout>
<h1>Form - reactive</h1>
<form class="attention-error" bind:this={form}>
<SLForm {onSubmit}>
<sl-input
name="Confirmation"
type="text"
Expand All @@ -57,5 +43,5 @@
type="submit"
variant="primary"
>Submit</sl-button>
</form>
</SLForm>
</Layout>
1 change: 1 addition & 0 deletions src/shoelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as SLApp } from "$src/shoelace/SLApp.svelte";
export { default as SLButton } from "./shoelace/SLButton.svelte";
export { default as SLIconButton } from "./shoelace/SLIconButton.svelte";
export { default as SLDialog } from "./shoelace/SLDialog.svelte";
export { default as SLForm } from "./shoelace/SLForm.svelte";
2 changes: 1 addition & 1 deletion src/shoelace/SLDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
footer?: Snippet
} & Props = $props();
let dialog;
let dialog: SlDialog;
$effect(() => {
const afterHide = () => {
Expand Down
47 changes: 47 additions & 0 deletions src/shoelace/SLForm.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script lang="ts">
import "@shoelace-style/shoelace/dist/components/button/button";
import "@shoelace-style/shoelace/dist/components/checkbox/checkbox";
import "@shoelace-style/shoelace/dist/components/input/input";
import "@shoelace-style/shoelace/dist/components/option/option";
import "@shoelace-style/shoelace/dist/components/select/select";
import "@shoelace-style/shoelace/dist/components/textarea/textarea";
import type { Snippet } from "svelte";
let {
onSubmit,
children,
} : {
onSubmit: (evt: SubmitEvent) => any
children: Snippet
} = $props();
let form: HTMLFormElement | undefined;
$effect(() => {
// @see https://shoelace.style/getting-started/form-controls#required-fields
const elements = [
"sl-button",
"sl-checkbox",
"sl-input",
"sl-option",
"sl-select",
"sl-textarea",
];
Promise.all(
elements.map(elm => customElements.whenDefined(elm))
).then(() => {
form?.addEventListener("submit", onSubmit);
});
return () => {
form?.removeEventListener("submit", onSubmit);
}
});
</script>
<form bind:this={form}>
{@render children()}
</form>

0 comments on commit f3404f0

Please sign in to comment.