-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tutorial for component binding (#5009)
Closes #5008 Co-authored-by: Stephane Vanraes <stephane.vanraes@fjordline.com> Co-authored-by: pngwn <pnda007@gmail.com>
- Loading branch information
1 parent
92422fd
commit f9b796c
Showing
5 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
site/content/tutorial/06-bindings/14-component-this/app-a/App.svelte
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
import InputField from './InputField.svelte'; | ||
</script> | ||
|
||
<InputField /> |
9 changes: 9 additions & 0 deletions
9
site/content/tutorial/06-bindings/14-component-this/app-a/InputField.svelte
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
let input; | ||
export function focus() { | ||
input.focus(); | ||
} | ||
</script> | ||
|
||
<input bind:this={input} /> |
9 changes: 9 additions & 0 deletions
9
site/content/tutorial/06-bindings/14-component-this/app-b/App.svelte
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
import InputField from './InputField.svelte'; | ||
let field; | ||
</script> | ||
|
||
<InputField bind:this={field}/> | ||
|
||
<button on:click={() => field.focus()}>Focus field</button> |
9 changes: 9 additions & 0 deletions
9
site/content/tutorial/06-bindings/14-component-this/app-b/InputField.svelte
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
let input; | ||
export function focus() { | ||
input.focus(); | ||
} | ||
</script> | ||
|
||
<input bind:this={input} /> |
19 changes: 19 additions & 0 deletions
19
site/content/tutorial/06-bindings/14-component-this/text.md
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: Binding to component instances | ||
--- | ||
|
||
Just as you can bind to DOM elements, you can bind to component instances themselves. For example, we can bind the instance of `<InputField>` to a prop named `field` in the same way we did when binding DOM Elements | ||
|
||
```html | ||
<InputField bind:this={field} /> | ||
``` | ||
|
||
Now we can programmatically interact with this component using `field`. | ||
|
||
```html | ||
<button on:click="{() => field.focus()}"> | ||
Focus field | ||
</button> | ||
``` | ||
|
||
> Note that we can't do `{field.focus}` since field is undefined when the button is first rendered and throws an error. |