1+ <script lang =" ts" module >
2+ import type {FormActionResult } from ' $lib/interfaces' ;
3+ import type {Snippet } from ' svelte' ;
4+ import type {ActionResult } from ' @sveltejs/kit' ;
5+ import type {HTMLFormAttributes } from " svelte/elements" ;
6+
7+ export interface FormProps {
8+ action? : string ;
9+ loading? : boolean ;
10+ method? : HTMLFormAttributes [' method' ];
11+ class? : string ;
12+ action_function? : () => void ;
13+ children: Snippet ;
14+ ivl? : boolean ,
15+ reset? : boolean ,
16+ after? : (result : FormActionResult ) => void ;
17+ enctype? : HTMLFormAttributes [' enctype' ];
18+ }
19+
20+ export interface FormActionArgs {
21+ result: ActionResult ;
22+ update: (options : { invalidateAll: boolean ; reset? : boolean }) => Promise <void >;
23+ }
24+ </script >
25+
126<script lang =" ts" >
2- import {enhance } from " $app/forms" ;
3- import type {Snippet } from ' svelte'
4-
5- interface Props {
6- action? : string ;
7- loading? : boolean ;
8- method? : string ;
9- class: string ;
10- action_function? : (e : Event ) => void ;
11- children: Snippet ;
12- inval? : boolean ,
13- after? : () => void ;
14- }
15-
16- let {
17- action = ' ' ,
18- method = ' post' ,
19- class : className = ' ' ,
20- // eslint-disable-next-line @typescript-eslint/no-unused-vars
21- loading = $bindable (),
22- inval : invalidateAll = true ,
23- children,
24- after = () => {
25- }
26- }: Props = $props ();
27- let action_function = () => {
28- loading = true ;
29- return ({update }: { update: ({invalidateAll}: { invalidateAll: boolean }) => Promise <void > }) => {
30- update ({invalidateAll }).finally (async () => {
31- loading = false ;
32- after ();
33- });
34- };
35- }
27+ import {enhance } from ' $app/forms' ;
3628
29+ let {
30+ action = ' ' ,
31+ method = ' post' ,
32+ class : className = ' ' ,
33+ loading = $bindable (false ),
34+ ivl : invalidateAll = true ,
35+ children,
36+ after,
37+ reset = true ,
38+ enctype,
39+ action_function = actionFunction
40+ }: FormProps = $props ();
41+
42+
43+ async function actionFunction() {
44+ loading = true ;
45+
46+ return async ({result , update }: FormActionArgs ) => {
47+ await update ({invalidateAll , reset }).finally (async () => {
48+ loading = false ;
49+ if (after && typeof after === ' function' ) {
50+ after (result as unknown as FormActionResult );
51+ }
52+ });
53+ };
54+ }
3755 </script >
3856
39- <form {action } {method } use:enhance ={action_function } class ={className } class:loading >
40- {@render children ()}
41- </form >
57+ <form
58+ {action }
59+ use:enhance ={action_function }
60+ {method }
61+ class ={className }
62+ class:loading
63+ {enctype }>
64+ {@render children ()}
65+ </form >
0 commit comments