Skip to content

Commit

Permalink
feat(Form): add success prop so Form can remove stored values internally
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbitronics committed Oct 5, 2022
1 parent 44ba3a9 commit 8aedd72
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 7 additions & 0 deletions components/custom/Form/Form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateRandomID } from '../../../random'
export let id = generateRandomID('form-')
export let saveToLocalStorage = false
export let success = false
let form = {}
Expand All @@ -12,6 +13,12 @@ onMount(() => {
})
$: saveToLocalStorage && restoreFormValues(form)
$: success && resetForm(form)
const resetForm = (form) => {
form.reset()
sessionStorage.removeItem(id)
}
const getValuesFromForm = (form) => Object.fromEntries(new FormData(form))
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ declare module '@silintl/ui-components' {
interface FormProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
id?: string
saveToLocalStorage?: boolean
success?: boolean
}
export class Form extends SvelteComponentTyped<FormProps> {}

Expand Down
12 changes: 6 additions & 6 deletions stories/Form.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const args = {
class: '', //only works for global classes
id: '',
saveToLocalStorage: false,
success: false,
'on:submit': function () {
sessionStorage.removeItem(this.id)
alert('submitted, reloading page')
location.reload()
alert('submitted successfully, form will now reset and remove any saved values')
this.success = true
},
}
</script>
Expand All @@ -20,11 +20,11 @@ const args = {

<Template let:args>
<Form on:submit={args['on:submit']} {...args}>
<TextField label='first' name='first'/>
<TextField label="first" name="first" />

<TextArea label='second' name='second' rows={4}/>
<TextArea label="second" name="second" rows={4} />

<MoneyInput label='third' name='third' />
<MoneyInput label="third" name="third" />

<Button raised>Submit</Button>
</Form>
Expand Down

0 comments on commit 8aedd72

Please sign in to comment.