-
-
Notifications
You must be signed in to change notification settings - Fork 324
Reacting to Input with State docs #1099
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
Draft
parmeyjohn
wants to merge
7
commits into
reactive-python:new-docs
Choose a base branch
from
parmeyjohn:managing-state
base: new-docs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db98a5a
refactored docs for 'reacting to input with state'
parmeyjohn 9d05d27
Format examples
Archmonger 5ee7998
add tools and packages section, remove outdated references, and fix J…
Archmonger 30b6654
refine tutorial instructions and update language for clarity
Archmonger c494004
Merge branch 'new-docs' into pr/1099
Archmonger 77ff916
change examples folder
Archmonger 21413a1
Fix everthing besides the challenges
Archmonger 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
3 changes: 3 additions & 0 deletions
3
docs/examples/css/managing_state/conditional_form_component.css
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,3 @@ | ||
.Error { | ||
color: red; | ||
} | ||
13 changes: 13 additions & 0 deletions
13
docs/examples/css/managing_state/multiple_form_components.css
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,13 @@ | ||
section { | ||
border-bottom: 1px solid #aaa; | ||
padding: 20px; | ||
} | ||
h4 { | ||
color: #222; | ||
} | ||
body { | ||
margin: 0; | ||
} | ||
.Error { | ||
color: red; | ||
} |
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,28 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
height: 250px; | ||
} | ||
|
||
.background { | ||
width: 100vw; | ||
height: 100vh; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
background: #eee; | ||
} | ||
|
||
.background--active { | ||
background: #a6b5ff; | ||
} | ||
|
||
.picture { | ||
width: 200px; | ||
height: 200px; | ||
border-radius: 10px; | ||
} | ||
|
||
.picture--active { | ||
border: 5px solid #a6b5ff; | ||
} |
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,9 @@ | ||
from reactpy import hooks | ||
|
||
|
||
# start | ||
is_empty, set_is_empty = hooks.use_state(True) | ||
is_typing, set_is_typing = hooks.use_state(False) | ||
is_submitting, set_is_submitting = hooks.use_state(False) | ||
is_success, set_is_success = hooks.use_state(False) | ||
is_error, set_is_error = hooks.use_state(False) |
38 changes: 38 additions & 0 deletions
38
docs/examples/python/managing_state/alt_stateful_picture_component.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,38 @@ | ||
from reactpy import component, event, hooks, html | ||
|
||
|
||
# start | ||
@component | ||
def picture(): | ||
is_active, set_is_active = hooks.use_state(False) | ||
|
||
if (is_active): | ||
return html.div( | ||
{ | ||
"class_name": "background", | ||
"on_click": lambda event: set_is_active(False) | ||
}, | ||
html.img( | ||
{ | ||
"on_click": event(stop_propagation=True), | ||
"class_name": "picture picture--active", | ||
"alt": "Rainbow houses in Kampung Pelangi, Indonesia", | ||
"src": "https://i.imgur.com/5qwVYb1.jpeg" | ||
} | ||
) | ||
) | ||
else: | ||
return html.div( | ||
{ | ||
"class_name": "background background--active" | ||
}, | ||
html.img( | ||
{ | ||
"on_click": event(lambda event: set_is_active(True), | ||
stop_propagation=True), | ||
"class_name": "picture", | ||
"alt": "Rainbow houses in Kampung Pelangi, Indonesia", | ||
"src": "https://i.imgur.com/5qwVYb1.jpeg" | ||
} | ||
) | ||
) |
18 changes: 18 additions & 0 deletions
18
docs/examples/python/managing_state/basic_form_component.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,18 @@ | ||
from reactpy import component, html | ||
|
||
|
||
# start | ||
@component | ||
def form(status="empty"): | ||
if status == "success": | ||
return html.h1("That's right!") | ||
else: | ||
return html._( | ||
html.h2("City quiz"), | ||
html.p("In which city is there a billboard that turns air into drinkable water?"), | ||
html.form( | ||
html.textarea(), | ||
html.br(), | ||
html.button("Submit") | ||
) | ||
) |
45 changes: 45 additions & 0 deletions
45
docs/examples/python/managing_state/conditional_form_component.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 @@ | ||
from reactpy import component, html | ||
|
||
|
||
# start | ||
@component | ||
def error(status): | ||
if status == "error": | ||
return html.p( | ||
{"class_name": "error"}, | ||
"Good guess but a wrong answer. Try again!" | ||
) | ||
else: | ||
return "" | ||
|
||
|
||
@component | ||
def form(status="empty"): | ||
# Try status="submitting", "error", "success" | ||
if status == "success": | ||
return html.h1("That's right!") | ||
else: | ||
return html._( | ||
html.h2("City quiz"), | ||
html.p( | ||
"In which city is there a billboard that turns air into \ | ||
drinkable water?" | ||
), | ||
html.form( | ||
html.textarea( | ||
{ | ||
"disabled": "True" if status == "submitting" | ||
else "False" | ||
} | ||
), | ||
html.br(), | ||
html.button( | ||
{ | ||
"disabled": True if status == "empty" | ||
or status == "submitting" else "False" | ||
}, | ||
"Submit" | ||
), | ||
error(status) | ||
) | ||
) |
18 changes: 18 additions & 0 deletions
18
docs/examples/python/managing_state/multiple_form_components.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,18 @@ | ||
from reactpy import component, html | ||
from conditional_form_component import form | ||
|
||
|
||
# start | ||
@component | ||
def item(status): | ||
return html.section( | ||
html.h4("Form", status, ':'), | ||
form(status) | ||
) | ||
|
||
|
||
@component | ||
def app(): | ||
statuses = ["empty", "typing", "submitting", "success", "error"] | ||
status_list = [item(status) for status in statuses] | ||
return html._(status_list) |
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,6 @@ | ||
from reactpy import hooks | ||
|
||
|
||
# start | ||
answer, set_answer = hooks.use_state("") | ||
error, set_error = hooks.use_state(None) |
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,16 @@ | ||
from reactpy import component, html | ||
|
||
|
||
# start | ||
@component | ||
def picture(): | ||
return html.div( | ||
{"class_name": "background background--active"}, | ||
html.img( | ||
{ | ||
"class_name": "picture", | ||
"alt": "Rainbow houses in Kampung Pelangi, Indonesia", | ||
"src": "https://i.imgur.com/5qwVYb1.jpeg" | ||
} | ||
) | ||
) |
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,7 @@ | ||
from reactpy import hooks | ||
|
||
|
||
# start | ||
answer, set_answer = hooks.use_state("") | ||
error, set_error = hooks.use_state(None) | ||
status, set_status = hooks.use_state("typing") # 'typing', 'submitting', or 'success' |
69 changes: 69 additions & 0 deletions
69
docs/examples/python/managing_state/stateful_form_component.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,69 @@ | ||
from reactpy import component, event, html, hooks | ||
import asyncio | ||
|
||
|
||
async def submit_form(): | ||
await asyncio.wait(5) | ||
|
||
|
||
# start | ||
@component | ||
def error_msg(error): | ||
if error: | ||
return html.p( | ||
{"class_name": "error"}, | ||
"Good guess but a wrong answer. Try again!" | ||
) | ||
else: | ||
return "" | ||
|
||
|
||
@component | ||
def form(status="empty"): | ||
answer, set_answer = hooks.use_state("") | ||
error, set_error = hooks.use_state(None) | ||
status, set_status = hooks.use_state("typing") | ||
|
||
@event(prevent_default=True) | ||
async def handle_submit(event): | ||
set_status("submitting") | ||
try: | ||
await submit_form(answer) | ||
set_status("success") | ||
except Exception: | ||
set_status("typing") | ||
set_error(Exception) | ||
|
||
@event() | ||
def handle_textarea_change(event): | ||
set_answer(event["target"]["value"]) | ||
|
||
if status == "success": | ||
return html.h1("That's right!") | ||
else: | ||
return html._( | ||
html.h2("City quiz"), | ||
html.p( | ||
"In which city is there a billboard \ | ||
that turns air into drinkable water?" | ||
), | ||
html.form( | ||
{"on_submit": handle_submit}, | ||
html.textarea( | ||
{ | ||
"value": answer, | ||
"on_change": handle_textarea_change, | ||
"disabled": True if status == "submitting" else "False" | ||
} | ||
), | ||
html.br(), | ||
html.button( | ||
{ | ||
"disabled": True if status == "empty" | ||
or status == "submitting" else "False" | ||
}, | ||
"Submit" | ||
), | ||
error_msg(error) | ||
) | ||
) |
33 changes: 33 additions & 0 deletions
33
docs/examples/python/managing_state/stateful_picture_component.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,33 @@ | ||
from reactpy import component, event, hooks, html | ||
|
||
|
||
# start | ||
@component | ||
def picture(): | ||
is_active, set_is_active = hooks.use_state(False) | ||
background_class_name = "background" | ||
picture_class_name = "picture" | ||
|
||
if (is_active): | ||
picture_class_name += " picture--active" | ||
else: | ||
background_class_name += " background--active" | ||
|
||
@event(stop_propagation=True) | ||
def handle_click(event): | ||
set_is_active(True) | ||
|
||
return html.div( | ||
{ | ||
"class_name": background_class_name, | ||
"on_click": set_is_active(False) | ||
}, | ||
html.img( | ||
{ | ||
"on_click": handle_click, | ||
"class_name": picture_class_name, | ||
"alt": "Rainbow houses in Kampung Pelangi, Indonesia", | ||
"src": "https://i.imgur.com/5qwVYb1.jpeg" | ||
} | ||
) | ||
) |
Oops, something went wrong.
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.