From 5563b3781ecb02b3bbcae338cf079db004abaf90 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Thu, 28 Feb 2019 09:35:11 -0500 Subject: [PATCH] more chapters --- .../01-text-inputs/app-a/App.svelte | 9 ++++ .../01-text-inputs/app-b/App.svelte | 9 ++++ .../06-bindings/01-text-inputs/text.md | 15 ++++++ .../02-numeric-inputs/app-a/App.svelte | 16 ++++++ .../02-numeric-inputs/app-b/App.svelte | 16 ++++++ .../06-bindings/02-numeric-inputs/text.md | 12 +++++ .../03-checkbox-inputs/app-a/App.svelte | 18 +++++++ .../03-checkbox-inputs/app-b/App.svelte | 18 +++++++ .../06-bindings/03-checkbox-inputs/text.md | 9 ++++ .../04-group-inputs/app-a/App.svelte | 54 +++++++++++++++++++ .../04-group-inputs/app-b/App.svelte | 52 ++++++++++++++++++ .../06-bindings/04-group-inputs/text.md | 36 +++++++++++++ .../05-textarea-inputs/app-a/App.svelte | 14 +++++ .../05-textarea-inputs/app-b/App.svelte | 14 +++++ .../06-bindings/05-textarea-inputs/text.md | 9 ++++ .../06-select-bindings/app-a/App.svelte | 39 ++++++++++++++ .../06-select-bindings/app-b/App.svelte | 39 ++++++++++++++ .../06-bindings/06-select-bindings/text.md | 13 +++++ site/content/tutorial/06-bindings/meta.json | 3 ++ site/content/tutorial/99-todo/99-todo/text.md | 3 +- 20 files changed, 397 insertions(+), 1 deletion(-) create mode 100644 site/content/tutorial/06-bindings/01-text-inputs/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/01-text-inputs/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/01-text-inputs/text.md create mode 100644 site/content/tutorial/06-bindings/02-numeric-inputs/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/02-numeric-inputs/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/02-numeric-inputs/text.md create mode 100644 site/content/tutorial/06-bindings/03-checkbox-inputs/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/03-checkbox-inputs/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/03-checkbox-inputs/text.md create mode 100644 site/content/tutorial/06-bindings/04-group-inputs/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/04-group-inputs/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/04-group-inputs/text.md create mode 100644 site/content/tutorial/06-bindings/05-textarea-inputs/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/05-textarea-inputs/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/05-textarea-inputs/text.md create mode 100644 site/content/tutorial/06-bindings/06-select-bindings/app-a/App.svelte create mode 100644 site/content/tutorial/06-bindings/06-select-bindings/app-b/App.svelte create mode 100644 site/content/tutorial/06-bindings/06-select-bindings/text.md create mode 100644 site/content/tutorial/06-bindings/meta.json diff --git a/site/content/tutorial/06-bindings/01-text-inputs/app-a/App.svelte b/site/content/tutorial/06-bindings/01-text-inputs/app-a/App.svelte new file mode 100644 index 0000000000..d3cf1a44ed --- /dev/null +++ b/site/content/tutorial/06-bindings/01-text-inputs/app-a/App.svelte @@ -0,0 +1,9 @@ + + + + +

Hello {name}!

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/01-text-inputs/app-b/App.svelte b/site/content/tutorial/06-bindings/01-text-inputs/app-b/App.svelte new file mode 100644 index 0000000000..662785bfce --- /dev/null +++ b/site/content/tutorial/06-bindings/01-text-inputs/app-b/App.svelte @@ -0,0 +1,9 @@ + + + + +

Hello {name}!

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/01-text-inputs/text.md b/site/content/tutorial/06-bindings/01-text-inputs/text.md new file mode 100644 index 0000000000..0082586133 --- /dev/null +++ b/site/content/tutorial/06-bindings/01-text-inputs/text.md @@ -0,0 +1,15 @@ +--- +title: Text inputs +--- + +As a general rule, data flow in Svelte is *top down* — a parent component can set props on a child component, and a component can set attributes on an element, but not the other way around. + +Sometimes it's useful to break that rule. Take the case of the `` element in this component — we *could* add an `on:input` event handler that set the value of `name` to `event.target.value`, but it's a bit... boilerplatey. It gets even worse with other kinds of form element, as we'll see. + +Instead, we can use the `bind:value` directive: + +```html + +``` + +This means that not only will changes to the value of `name` update the input value, but changes to the input value will update `name`. \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/02-numeric-inputs/app-a/App.svelte b/site/content/tutorial/06-bindings/02-numeric-inputs/app-a/App.svelte new file mode 100644 index 0000000000..ac4fadf012 --- /dev/null +++ b/site/content/tutorial/06-bindings/02-numeric-inputs/app-a/App.svelte @@ -0,0 +1,16 @@ + + + + + + +

{a} + {b} = {a + b}

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/02-numeric-inputs/app-b/App.svelte b/site/content/tutorial/06-bindings/02-numeric-inputs/app-b/App.svelte new file mode 100644 index 0000000000..798d57e021 --- /dev/null +++ b/site/content/tutorial/06-bindings/02-numeric-inputs/app-b/App.svelte @@ -0,0 +1,16 @@ + + + + + + +

{a} + {b} = {a + b}

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/02-numeric-inputs/text.md b/site/content/tutorial/06-bindings/02-numeric-inputs/text.md new file mode 100644 index 0000000000..1ede244301 --- /dev/null +++ b/site/content/tutorial/06-bindings/02-numeric-inputs/text.md @@ -0,0 +1,12 @@ +--- +title: Numeric inputs +--- + +In the DOM, everything is a string. That's unhelpful when you're dealing with numeric inputs — `type="number"` and `type="range"` — as it means you have to remember to coerce `input.value` before using it. + +With `bind:value`, Svelte takes care of it for you: + +```html + + +``` \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/03-checkbox-inputs/app-a/App.svelte b/site/content/tutorial/06-bindings/03-checkbox-inputs/app-a/App.svelte new file mode 100644 index 0000000000..2847dad485 --- /dev/null +++ b/site/content/tutorial/06-bindings/03-checkbox-inputs/app-a/App.svelte @@ -0,0 +1,18 @@ + + + + +{#if yes} +

Thank you. We will bombard your inbox and sell your personal details.

+{:else} +

You must opt in to continue. If you're not paying, you're the product.

+{/if} + + \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/03-checkbox-inputs/app-b/App.svelte b/site/content/tutorial/06-bindings/03-checkbox-inputs/app-b/App.svelte new file mode 100644 index 0000000000..b82d31e783 --- /dev/null +++ b/site/content/tutorial/06-bindings/03-checkbox-inputs/app-b/App.svelte @@ -0,0 +1,18 @@ + + + + +{#if yes} +

Thank you. We will bombard your inbox and sell your personal details.

+{:else} +

You must opt in to continue. If you're not paying, you're the product.

+{/if} + + \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/03-checkbox-inputs/text.md b/site/content/tutorial/06-bindings/03-checkbox-inputs/text.md new file mode 100644 index 0000000000..621d9fbb7b --- /dev/null +++ b/site/content/tutorial/06-bindings/03-checkbox-inputs/text.md @@ -0,0 +1,9 @@ +--- +title: Checkbox inputs +--- + +Checkboxes are used for toggling between states. Instead of binding to `input.value`, we bind to `input.checked`: + +```html + +``` \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/04-group-inputs/app-a/App.svelte b/site/content/tutorial/06-bindings/04-group-inputs/app-a/App.svelte new file mode 100644 index 0000000000..5ea04f4e95 --- /dev/null +++ b/site/content/tutorial/06-bindings/04-group-inputs/app-a/App.svelte @@ -0,0 +1,54 @@ + + +

Size

+ + + + + + + +

Flavours

+ + + + + + + +{#if flavours.length === 0} +

Please select at least one flavour

+{:else if flavours.length > scoops} +

Can't order more flavours than scoops!

+{:else} +

+ You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} + of {join(flavours)} +

+{/if} diff --git a/site/content/tutorial/06-bindings/04-group-inputs/app-b/App.svelte b/site/content/tutorial/06-bindings/04-group-inputs/app-b/App.svelte new file mode 100644 index 0000000000..a46c61c7e7 --- /dev/null +++ b/site/content/tutorial/06-bindings/04-group-inputs/app-b/App.svelte @@ -0,0 +1,52 @@ + + +

Size

+ + + + + + + +

Flavours

+ +{#each menu as flavour} + +{/each} + +{#if flavours.length === 0} +

Please select at least one flavour

+{:else if flavours.length > scoops} +

Can't order more flavours than scoops!

+{:else} +

+ You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} + of {join(flavours)} +

+{/if} diff --git a/site/content/tutorial/06-bindings/04-group-inputs/text.md b/site/content/tutorial/06-bindings/04-group-inputs/text.md new file mode 100644 index 0000000000..97edb8db76 --- /dev/null +++ b/site/content/tutorial/06-bindings/04-group-inputs/text.md @@ -0,0 +1,36 @@ +--- +title: Group inputs +--- + +If you have multiple inputs relating to the same value, you can use `bind:group` along with the `value` attribute. Radio inputs in the same group are mutually exclusive; checkbox inputs in the same group form an array of selected values. + +Add `bind:group` to each input: + +```html + +``` + +In this case, we could make the code simpler by moving the checkbox inputs into an `each` block. First, add a `menu` variable to the ` + + + + + +{@html rendered} \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/05-textarea-inputs/app-b/App.svelte b/site/content/tutorial/06-bindings/05-textarea-inputs/app-b/App.svelte new file mode 100644 index 0000000000..4081569dd1 --- /dev/null +++ b/site/content/tutorial/06-bindings/05-textarea-inputs/app-b/App.svelte @@ -0,0 +1,14 @@ + + + + + + +{@html rendered} \ No newline at end of file diff --git a/site/content/tutorial/06-bindings/05-textarea-inputs/text.md b/site/content/tutorial/06-bindings/05-textarea-inputs/text.md new file mode 100644 index 0000000000..a19d561719 --- /dev/null +++ b/site/content/tutorial/06-bindings/05-textarea-inputs/text.md @@ -0,0 +1,9 @@ +--- +title: Textarea inputs +--- + +The ` + let questions = [ + { id: 1, text: `Where did you go to school?` }, + { id: 2, text: `What is your mother's name?` }, + { id: 3, text: `What is another personal fact that an attacker could easily find with Google?` } + ]; + + let selected; + + let answer = ''; + + function handleSubmit() { + alert(`answered question ${selected.id} (${selected.text}) with "${answer}"`); + } + + + + +

Insecurity questions

+ +
+ + + + + +
+ +

selected question {selected ? selected.id : '[waiting...]'}

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/06-select-bindings/app-b/App.svelte b/site/content/tutorial/06-bindings/06-select-bindings/app-b/App.svelte new file mode 100644 index 0000000000..3f1e7fa7e1 --- /dev/null +++ b/site/content/tutorial/06-bindings/06-select-bindings/app-b/App.svelte @@ -0,0 +1,39 @@ + + + + +

Insecurity questions

+ +
+ + + + + +
+ +

selected question {selected ? selected.id : '[waiting...]'}

\ No newline at end of file diff --git a/site/content/tutorial/06-bindings/06-select-bindings/text.md b/site/content/tutorial/06-bindings/06-select-bindings/text.md new file mode 100644 index 0000000000..f4972557ab --- /dev/null +++ b/site/content/tutorial/06-bindings/06-select-bindings/text.md @@ -0,0 +1,13 @@ +--- +title: Select bindings +--- + +We can also use `bind:value` with ` +``` + +Note that the `