File tree 3 files changed +14
-6
lines changed
3 files changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { generateRandomID } from '../../../random'
4
4
5
5
export let id = generateRandomID (' form-' )
6
6
export let saveToLocalStorage = false
7
+ export let success = false
7
8
8
9
let form = {}
9
10
@@ -12,6 +13,12 @@ onMount(() => {
12
13
})
13
14
14
15
$: saveToLocalStorage && restoreFormValues (form)
16
+ $: success && resetForm (form)
17
+
18
+ const resetForm = (form ) => {
19
+ form .reset ()
20
+ sessionStorage .removeItem (id)
21
+ }
15
22
16
23
const getValuesFromForm = (form ) => Object .fromEntries (new FormData (form))
17
24
Original file line number Diff line number Diff line change @@ -305,6 +305,7 @@ declare module '@silintl/ui-components' {
305
305
interface FormProps extends svelte . JSX . HTMLAttributes < HTMLElementTagNameMap [ 'div' ] > {
306
306
id ?: string
307
307
saveToLocalStorage ?: boolean
308
+ success ?: boolean
308
309
}
309
310
export class Form extends SvelteComponentTyped < FormProps > { }
310
311
Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ const args = {
8
8
class: ' ' , // only works for global classes
9
9
id: ' ' ,
10
10
saveToLocalStorage: false ,
11
+ success: false ,
11
12
' on:submit ' : function () {
12
- sessionStorage .removeItem (this .id )
13
- alert (' submitted, reloading page' )
14
- location .reload ()
13
+ alert (' submitted successfully, form will now reset and remove any saved values' )
14
+ this .success = true
15
15
},
16
16
}
17
17
</script >
@@ -20,11 +20,11 @@ const args = {
20
20
21
21
<Template let:args >
22
22
<Form on:submit ={args [' on:submit' ]} {...args }>
23
- <TextField label =' first' name =' first' />
23
+ <TextField label =" first" name =" first" />
24
24
25
- <TextArea label =' second' name =' second' rows ={4 }/>
25
+ <TextArea label =" second" name =" second" rows ={4 } />
26
26
27
- <MoneyInput label =' third' name =' third' />
27
+ <MoneyInput label =" third" name =" third" />
28
28
29
29
<Button raised >Submit</Button >
30
30
</Form >
You can’t perform that action at this time.
0 commit comments