-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from silinternational/develop
Release
- Loading branch information
Showing
9 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,65 @@ | ||
<script> | ||
import { onMount } from 'svelte' | ||
import { generateRandomID } from '../../../random' | ||
export let id = generateRandomID('form-') | ||
export let saveToLocalStorage = false | ||
let form = {} | ||
onMount(() => { | ||
saveToLocalStorage && listenForBlurOnForm(form) | ||
}) | ||
$: saveToLocalStorage && restoreFormValues(form) | ||
const getValuesFromForm = (form) => Object.fromEntries(new FormData(form)) | ||
const storeFormValues = (formId, valuesFromForm) => { | ||
sessionStorage.setItem(formId, JSON.stringify(valuesFromForm)) | ||
} | ||
const setValuesOnForm = (form, valuesForForm) => { | ||
Object.entries(valuesForForm).forEach((keyValuePair) => { | ||
const [key, value] = keyValuePair | ||
form[key].value = value | ||
}) | ||
} | ||
const restoreFormValues = (form) => { | ||
const sessionStorageKey = form.id | ||
const formValuesJson = sessionStorage.getItem(sessionStorageKey) | ||
if (formValuesJson) { | ||
const valuesForForm = JSON.parse(formValuesJson) | ||
if (valuesForForm) { | ||
setValuesOnForm(form, valuesForForm) | ||
sessionStorage.removeItem(sessionStorageKey) | ||
} | ||
} | ||
} | ||
const onBlur = (value) => { | ||
if (value) { | ||
storeFormValues(form.id, getValuesFromForm(form)) | ||
} | ||
} | ||
const listenForBlurOnForm = (form) => { | ||
const inputs = form.querySelectorAll('input, textarea') | ||
inputs.forEach((input) => { | ||
input.addEventListener('blur', function () { | ||
onBlur(this.value) | ||
}) | ||
}) | ||
} | ||
</script> | ||
|
||
<style> | ||
:global(form > *) { | ||
margin-top: 2rem; | ||
} | ||
</style> | ||
|
||
<form class="w-100 {$$props.class}" on:submit|preventDefault autocomplete="off"> | ||
<form bind:this={form} {id} class="w-100 {$$props.class}" on:submit|preventDefault autocomplete="off"> | ||
<slot /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
<script> | ||
import { Meta, Template, Story } from '@storybook/addon-svelte-csf' | ||
import { TextField } from '../components/mdc' | ||
import { TextField, MoneyInput, TextArea, Button } from '../components/mdc' | ||
import { Form } from '../components/custom' | ||
import { copyAndModifyArgs } from './helpers' | ||
const args = { | ||
class: '', //only works for global classes | ||
onSubmit: () => {}, | ||
id: '', | ||
saveToLocalStorage: false, | ||
'on:submit': function () { | ||
sessionStorage.removeItem(this.id) | ||
alert('submitted, reloading page') | ||
location.reload() | ||
}, | ||
} | ||
</script> | ||
|
||
<Meta title="Molecule/Form" component={Form} /> | ||
|
||
<Template let:args> | ||
<Form on:submit={args.onSubmit} {...args}> | ||
<TextField /> | ||
<Form on:submit={args['on:submit']} {...args}> | ||
<TextField label='first' name='first'/> | ||
|
||
<TextArea label='second' name='second' rows={4}/> | ||
|
||
<MoneyInput label='third' name='third' /> | ||
|
||
<Button raised>Submit</Button> | ||
</Form> | ||
</Template> | ||
|
||
<Story name="Default" {args} /> | ||
|
||
<Story name="Id" args={copyAndModifyArgs(args, { id: '123' })} /> | ||
|
||
<Story name="Save To Local Storage" args={copyAndModifyArgs(args, { saveToLocalStorage: true })} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters