Skip to content

Commit 8aedd72

Browse files
committed
feat(Form): add success prop so Form can remove stored values internally
1 parent 44ba3a9 commit 8aedd72

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

components/custom/Form/Form.svelte

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { generateRandomID } from '../../../random'
44
55
export let id = generateRandomID('form-')
66
export let saveToLocalStorage = false
7+
export let success = false
78
89
let form = {}
910
@@ -12,6 +13,12 @@ onMount(() => {
1213
})
1314
1415
$: saveToLocalStorage && restoreFormValues(form)
16+
$: success && resetForm(form)
17+
18+
const resetForm = (form) => {
19+
form.reset()
20+
sessionStorage.removeItem(id)
21+
}
1522
1623
const getValuesFromForm = (form) => Object.fromEntries(new FormData(form))
1724

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ declare module '@silintl/ui-components' {
305305
interface FormProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
306306
id?: string
307307
saveToLocalStorage?: boolean
308+
success?: boolean
308309
}
309310
export class Form extends SvelteComponentTyped<FormProps> {}
310311

stories/Form.stories.svelte

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const args = {
88
class: '', //only works for global classes
99
id: '',
1010
saveToLocalStorage: false,
11+
success: false,
1112
'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
1515
},
1616
}
1717
</script>
@@ -20,11 +20,11 @@ const args = {
2020

2121
<Template let:args>
2222
<Form on:submit={args['on:submit']} {...args}>
23-
<TextField label='first' name='first'/>
23+
<TextField label="first" name="first" />
2424

25-
<TextArea label='second' name='second' rows={4}/>
25+
<TextArea label="second" name="second" rows={4} />
2626

27-
<MoneyInput label='third' name='third' />
27+
<MoneyInput label="third" name="third" />
2828

2929
<Button raised>Submit</Button>
3030
</Form>

0 commit comments

Comments
 (0)