Skip to content

Commit

Permalink
Merge pull request #14 from mdauner/allow-initial-values
Browse files Browse the repository at this point in the history
Allow initial values
  • Loading branch information
mdauner authored Sep 30, 2019
2 parents 99d9be2 + 398aa6b commit e0867f1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ $ yarn add sveltejs-forms
{ id: 'react', title: 'React' },
{ id: 'angular', title: 'Angular' },
];
const initialValues = {
language: 'svelte'
}
</script>

<style lang="scss">
Expand All @@ -81,7 +85,11 @@ $ yarn add sveltejs-forms
}
</style>

<Form {schema} on:submit={handleSubmit} let:isSubmitting>
<Form
{schema} //optional
{initialValues} //optional
on:submit={handleSubmit}
let:isSubmitting>
<Input name="email" />
<Input name="password" type="password" />
<Select name="language" {options} />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sveltejs-forms",
"description": "Declarative forms for Svelte",
"homepage": "https://mdauner.github.io/sveltejs-forms/",
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"repository": "github:mdauner/sveltejs-forms",
"svelte": "src/components/components.module.js",
Expand Down
6 changes: 5 additions & 1 deletion src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
{ id: 'react', title: 'React' },
{ id: 'angular', title: 'Angular' },
];
const initialValues = {
language: 'svelte',
};
</script>

<style lang="scss">
Expand All @@ -51,7 +55,7 @@
}
</style>

<Form {schema} on:submit={handleSubmit} let:isSubmitting>
<Form {schema} {initialValues} on:submit={handleSubmit} let:isSubmitting>
<Input name="email" placeholder="Email" />
<Input name="password" type="password" placeholder="Password" />
<Select name="language" {options} />
Expand Down
5 changes: 3 additions & 2 deletions src/components/Form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { setContext, createEventDispatcher } from 'svelte';
import { writable } from 'svelte/store';
export let initialValues = {};
export let schema = null;
const dispatch = createEventDispatcher();
Expand All @@ -17,7 +18,7 @@
setContext(FORM, {
registerInput: name => {
$values[name] = '';
$values[name] = initialValues[name] || '';
$touched[name] = false;
$errors[name] = null;
},
Expand All @@ -35,7 +36,7 @@
function resetForm() {
Object.keys($values).forEach(name => {
$values[name] = '';
$values[name] = initialValues[name] || '';
$errors[name] = null;
$touched[name] = false;
});
Expand Down
11 changes: 11 additions & 0 deletions tests/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ describe('Form', () => {
});
});

it('sets initial values', async () => {
const { component } = render(App, {
props: { initialValues: { email: '[email protected]' } },
});

expect(component.form.$$.ctx.$values).toEqual({
email: '[email protected]',
language: '',
});
});

it('unregisters when field is removed', async () => {
const { component, getByText, getByPlaceholderText } = render(App);

Expand Down
8 changes: 7 additions & 1 deletion tests/TestApp.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export let onSubmit = () => {};
export let schema = null;
export let initialValues = {};
export let showOptionalField = false;
export let form = null;
Expand All @@ -13,7 +14,12 @@
];
</script>

<Form {schema} on:submit={onSubmit} let:isSubmitting bind:this={form}>
<Form
{schema}
{initialValues}
on:submit={onSubmit}
let:isSubmitting
bind:this={form}>
<Input name="email" placeholder="Email" />
{#if showOptionalField}
<Input name="optional" placeholder="Optional" />
Expand Down

0 comments on commit e0867f1

Please sign in to comment.