Skip to content

Commit

Permalink
Merge pull request #164 from silinternational/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
hobbitronics authored Oct 4, 2022
2 parents 26ecd7e + b766b2d commit d32f4b2
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 8 deletions.
58 changes: 57 additions & 1 deletion components/custom/Form/Form.svelte
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>
6 changes: 4 additions & 2 deletions components/mdc/TextInput/MoneyInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export let label = ''
export let value = ''
export let step = '0.01'
export let placeholder = ''
export let name = ''
export let maxValue = undefined
export let minValue = undefined
export let autofocus = false
Expand Down Expand Up @@ -87,10 +88,11 @@ const focus = (node) => autofocus && node.focus()
on:keydown
on:keypress
on:keyup
{required}
{maxlength}
{disabled}
{maxlength}
{name}
{placeholder}
{required}
/>
{#if showErrorIcon}
<span class="mdc-text-field__affix mdc-text-field__affix--suffix">
Expand Down
2 changes: 2 additions & 0 deletions components/mdc/TextInput/TextArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export let autofocus = false
export let rtl = false
export let required = false
export let description = ''
export let name = ''
const labelID = generateRandomID('textarea-label-')
Expand Down Expand Up @@ -86,6 +87,7 @@ label {
aria-describedby="{labelID}-helper-id"
{rows}
maxlength="524288"
{name}
{required}
{placeholder}
bind:value
Expand Down
2 changes: 2 additions & 0 deletions components/mdc/TextInput/TextField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export let disabled = false
export let required = false
export let icon = ''
export let description = ''
export let name = ''
const labelID = generateRandomID('text-label-')
Expand Down Expand Up @@ -77,6 +78,7 @@ const focus = (node) => autofocus && node.focus()
on:keydown
on:keypress
on:keyup
{name}
{required}
{disabled}
maxlength="524288"
Expand Down
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ declare module '@silintl/ui-components' {
interface MoneyInputProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
label?: string
value?: number
name?: string
placeholder?: string
autofocus?: boolean
disabled?: boolean
Expand Down Expand Up @@ -228,6 +229,7 @@ declare module '@silintl/ui-components' {
interface TextAreaProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
label?: string
value?: string
name?: string
placeholder?: string
rows?: number | string
required?: boolean
Expand All @@ -241,6 +243,7 @@ declare module '@silintl/ui-components' {
interface TextFieldProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
label?: string
value?: string
name?: string
placeholder?: string
maxlength?: number
autofocus?: boolean
Expand Down Expand Up @@ -300,7 +303,8 @@ declare module '@silintl/ui-components' {
export class FileDropArea extends SvelteComponentTyped<FileDropAreaProps> {}

interface FormProps extends svelte.JSX.HTMLAttributes<HTMLElementTagNameMap['div']> {
// no exported members
id?: string
saveToLocalStorage?: boolean
}
export class Form extends SvelteComponentTyped<FormProps> {}

Expand Down
25 changes: 21 additions & 4 deletions stories/Form.stories.svelte
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 })} />
3 changes: 3 additions & 0 deletions stories/MoneyInput.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ function setValues(max, step) {
<Story name="Description" args={copyAndModifyArgs(args, { description: 'a description' })} />

<Story name="Test step" args={{ ...args, label: '' }} />

<Story name="Name" args={copyAndModifyArgs(args, { name: 'money' })} />

3 changes: 3 additions & 0 deletions stories/TextArea.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let title = 'TextArea'
const args = {
label: title,
name: '',
row: 8,
class: '', //only works for global classes
}
Expand Down Expand Up @@ -35,3 +36,5 @@ const args = {
<Story name="Required" args={copyAndModifyArgs(args, { required: true })} />

<Story name="Description" args={copyAndModifyArgs(args, { description: 'a description' })} />

<Story name="Name" args={copyAndModifyArgs(args, { name: 'area' })} />
2 changes: 2 additions & 0 deletions stories/TextField.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ let lastKey = ''
<Story name="Required" args={copyAndModifyArgs(args, { required: true })} />

<Story name="Description" args={copyAndModifyArgs(args, { description: 'a description' })} />

<Story name="Name" args={copyAndModifyArgs(args, { name: 'field' })} />

0 comments on commit d32f4b2

Please sign in to comment.