Skip to content

Commit 067279f

Browse files
committed
Identify where your state should live
1 parent e4be299 commit 067279f

File tree

6 files changed

+213
-180
lines changed

6 files changed

+213
-180
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
padding: 5px;
3+
}
4+
label {
5+
display: block;
6+
margin-top: 5px;
7+
margin-bottom: 5px;
8+
}
9+
th {
10+
padding-top: 5px;
11+
}
12+
td {
13+
padding: 2px;
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from reactpy import component, html
2+
3+
4+
# start
5+
@component
6+
def search_bar(filter_text, in_stock_only):
7+
return html.form(
8+
html.input(
9+
{
10+
"type": "text",
11+
"value": filter_text,
12+
"placeholder": "Search...",
13+
}
14+
),
15+
html.p(
16+
html.input({"type": "checkbox", "checked": in_stock_only}),
17+
"Only show products in stock",
18+
),
19+
)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from reactpy import component, html, use_state
2+
3+
4+
# start
5+
@component
6+
def filterable_product_table(products):
7+
filter_text, set_filter_text = use_state("")
8+
in_stock_only, set_in_stock_only = use_state(False)
9+
10+
return html.div(
11+
search_bar(filter_text=filter_text, in_stock_only=in_stock_only),
12+
product_table(
13+
products=products, filter_text=filter_text, in_stock_only=in_stock_only
14+
),
15+
)
16+
17+
18+
@component
19+
def product_category_row(category):
20+
return html.tr(
21+
html.th({"colspan": 2}, category),
22+
)
23+
24+
25+
@component
26+
def product_row(product):
27+
if product["stocked"]:
28+
name = product["name"]
29+
else:
30+
name = html.span({"style": {"color": "red"}}, product["name"])
31+
32+
return html.tr(
33+
html.td(name),
34+
html.td(product["price"]),
35+
)
36+
37+
38+
@component
39+
def product_table(products, filter_text, in_stock_only):
40+
rows = []
41+
last_category = None
42+
43+
for product in products:
44+
if filter_text.lower() not in product["name"].lower():
45+
continue
46+
if in_stock_only and not product["stocked"]:
47+
continue
48+
if product["category"] != last_category:
49+
rows.append(
50+
product_category_row(product["category"], key=product["category"])
51+
)
52+
rows.append(product_row(product, key=product["name"]))
53+
last_category = product["category"]
54+
55+
return html.table(
56+
html.thead(
57+
html.tr(
58+
html.th("Name"),
59+
html.th("Price"),
60+
),
61+
),
62+
html.tbody(rows),
63+
)
64+
65+
66+
@component
67+
def search_bar(filter_text, in_stock_only):
68+
return html.form(
69+
html.input({"type": "text", "value": filter_text, "placeholder": "Search..."}),
70+
html.label(
71+
html.input({"type": "checkbox", "checked": in_stock_only}),
72+
"Only show products in stock",
73+
),
74+
)
75+
76+
77+
PRODUCTS = [
78+
{"category": "Fruits", "price": "$1", "stocked": True, "name": "Apple"},
79+
{"category": "Fruits", "price": "$1", "stocked": True, "name": "Dragonfruit"},
80+
{"category": "Fruits", "price": "$2", "stocked": False, "name": "Passionfruit"},
81+
{"category": "Vegetables", "price": "$2", "stocked": True, "name": "Spinach"},
82+
{"category": "Vegetables", "price": "$4", "stocked": False, "name": "Pumpkin"},
83+
{"category": "Vegetables", "price": "$1", "stocked": True, "name": "Peas"},
84+
]
85+
86+
87+
@component
88+
def app():
89+
return filterable_product_table(PRODUCTS)
90+
91+
92+
# end
93+
if __name__ == "__main__":
94+
from reactpy import run
95+
from reactpy.utils import _read_docs_css
96+
97+
@component
98+
def styled_app():
99+
return html._(html.style(_read_docs_css()), app())
100+
101+
run(styled_app)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from reactpy import component, use_state
2+
3+
4+
# start
5+
@component
6+
def filterable_product_table(products):
7+
filter_text, set_filter_text = use_state("")
8+
in_stock_only, set_in_stock_only = use_state(False)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from reactpy import html
2+
3+
filter_text = ""
4+
in_stock_only = False
5+
products = ()
6+
7+
def search_bar(**_kw):
8+
...
9+
10+
def product_table(**_kw):
11+
...
12+
13+
# start
14+
html.div(
15+
search_bar(filter_text=filter_text, in_stock_only=in_stock_only),
16+
product_table(products=products, filter_text=filter_text, in_stock_only=in_stock_only),
17+
)

0 commit comments

Comments
 (0)