-
-
Notifications
You must be signed in to change notification settings - Fork 331
docs for components sharing state #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d0fa617
docs for components sharing state
35e6ed6
Merge branch 'main' into main
rmorshea 58eb716
Add new line to end of file
rmorshea bc86bbd
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea 4052600
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea ecc40cf
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea 5a653ff
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea 48dfa10
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea bbd573c
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea c17084f
Update docs/source/adding-interactivity/components-sharing-state/_exa…
rmorshea 7b7608f
Merge branch 'main' into main
rmorshea c291e9c
making syncedinputs an example, moving to managing-state
b8b8bc5
Merge branch 'main' into main
acivitillo 565e2b4
fixing isort import issue in shared-component-state examples
1527290
Merge branch 'main' into main
acivitillo e0fb5e0
fixing link references
7f6ee8a
Merge branch 'main' of https://github.com/acivitillo/idom
0484d0c
Merge branch 'main' into main
rmorshea b0bf11b
Merge branch 'main' into main
rmorshea c0fa435
fixing docker build by upgrading npm and fast-json-path
95f59a1
Merge branch 'main' into main
rmorshea bcae270
Merge branch 'main' into main
rmorshea ea339c9
Update noxfile.py
rmorshea 942a302
Merge branch 'main' into main
rmorshea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/app.py
This file contains hidden or 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,45 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from idom import component, hooks, html, run | ||
|
|
||
|
|
||
| HERE = Path(__file__) | ||
| DATA_PATH = HERE.parent / "data.json" | ||
| sculpture_data = json.loads(DATA_PATH.read_text()) | ||
|
|
||
|
|
||
| @component | ||
| def FilterableList(): | ||
| value, set_value = hooks.use_state("") | ||
| return html.p(Search(value, set_value), html.hr(), Table(value, set_value)) | ||
|
|
||
|
|
||
| @component | ||
| def Search(value, set_value): | ||
| def handle_change(event): | ||
| set_value(event["target"]["value"]) | ||
|
|
||
| return html.label( | ||
| "Search by Food Name: ", html.input({"value": value, "onChange": handle_change}) | ||
| ) | ||
|
|
||
|
|
||
| @component | ||
| def Table(value, set_value): | ||
| rows = [] | ||
| for row in data: | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| name = html.td(row["name"]) | ||
| descr = html.td(row["description"]) | ||
| tr = html.tr(name, descr, value) | ||
| if value == "": | ||
| rows.append(tr) | ||
| else: | ||
| if value.lower() in row["name"].lower(): | ||
| rows.append(tr) | ||
| headers = html.tr(html.td(html.b("name")), html.td(html.b("description"))) | ||
| table = html.table(html.thead(headers), html.tbody(rows)) | ||
| return table | ||
|
|
||
|
|
||
| run(FilterableList) | ||
22 changes: 22 additions & 0 deletions
22
.../source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json
This file contains hidden or 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,22 @@ | ||
| [ | ||
| { | ||
| "name": "Sushi", | ||
| "description": "Sushi is a traditional Japanese dish of prepared vinegared rice", | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| "name": "Dal", | ||
| "description": "The most common way of preparing dal is in the form of a soup to which onions, tomatoes and various spices may be added", | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| "name": "Pierogi", | ||
| "description": "Pierogi are filled dumplings made by wrapping unleavened dough around a savoury or sweet filling and cooking in boiling water", | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| "name": "Shish Kebab", | ||
| "description": "Shish kebab is a popular meal of skewered and grilled cubes of meat", | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| { | ||
| "name": "Dim sum", | ||
| "description": "Dim sum is a large range of small dishes that Cantonese people traditionally enjoy in restaurants for breakfast and lunch", | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ] | ||
59 changes: 59 additions & 0 deletions
59
docs/source/adding-interactivity/components-sharing-state/index.rst
This file contains hidden or 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,59 @@ | ||
| Components Sharing State | ||
| ======================== | ||
|
|
||
| Sometimes you want the state of two components to always change together. To do it, you | ||
| need to be able to share state between those two components, to share state between | ||
| componets move state to the nearest parent. In React world this is known as "lifting | ||
| state up" and it is a very common thing to do. Let's look at 2 examples, also from | ||
| `React <https://beta.reactjs.org/learn/sharing-state-between-components>`__, | ||
| but translated to IDOM. | ||
|
|
||
| Sycned Inputs | ||
| ------------- | ||
|
|
||
| In the code below the two input boxes are syncronized, this happens because they share | ||
| state. The state is shared via the parent component ``SyncedInputs``. Check the ``value`` | ||
| and ``set_value`` variables. | ||
|
|
||
| .. code-block:: | ||
| :linenos: | ||
| :lineno-start: 14 | ||
|
|
||
| from idom import component, html, run, hooks | ||
|
|
||
| @component | ||
| def SyncedInputs(): | ||
| value, set_value = hooks.use_state("") | ||
| return html.p( | ||
| Input("First input", value, set_value), | ||
| Input("Second input", value, set_value), | ||
| ) | ||
|
|
||
|
|
||
| @component | ||
| def Input(label, value, set_value): | ||
| def handle_change(event): | ||
| set_value(event["target"]["value"]) | ||
|
|
||
| return html.label( | ||
| label + " ", html.input({"value": value, "onChange": handle_change}) | ||
| ) | ||
|
|
||
| run(SyncedInputs) | ||
rmorshea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| Filterable List | ||
| ---------------- | ||
|
|
||
| In the example below the search input and the list of elements below share the | ||
| same state, the state represents the food name. | ||
|
|
||
| Note how the component ``Table`` gets called at each change of state. The | ||
| component is observing the state and reacting to state changes automatically, | ||
| just like it would do in React. | ||
|
|
||
| .. idom:: _examples/filterable_list | ||
|
|
||
| .. note:: | ||
|
|
||
| Try typing a food name in the search bar. | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.