Skip to content

Commit 6eb9631

Browse files
committed
add send_message ex
1 parent 632f968 commit 6eb9631

File tree

13 files changed

+47
-47
lines changed

13 files changed

+47
-47
lines changed

docs/source/_exts/idom_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class WidgetExample(SphinxDirective):
2525

2626
option_spec = {
2727
"result-is-default-tab": directives.flag,
28-
"activate-result": directives.flag,
28+
"activate-button": directives.flag,
2929
}
3030

3131
def run(self):
@@ -37,7 +37,7 @@ def run(self):
3737

3838
show_linenos = "linenos" in self.options
3939
live_example_is_default_tab = "result-is-default-tab" in self.options
40-
activate_result = "activate-result" in self.options
40+
activate_result = "activate-button" not in self.options
4141

4242
ex_files = get_example_files_by_name(example_name)
4343
if not ex_files:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from idom import component, event, html, run, use_state
2+
3+
4+
@component
5+
def App():
6+
is_sent, set_is_sent = use_state(False)
7+
message, set_message = use_state("")
8+
9+
if is_sent:
10+
return html.div(
11+
html.h1("Message sent!"),
12+
html.button(
13+
{"onClick": lambda event: set_is_sent(False)}, "Send new message?"
14+
),
15+
)
16+
17+
@event(prevent_default=True)
18+
def handle_submit(event):
19+
set_message("")
20+
set_is_sent(True)
21+
22+
return html.form(
23+
{"onSubmit": handle_submit, "style": {"display": "inline-grid"}},
24+
html.textarea(
25+
{
26+
"placeholder": "Your message here...",
27+
"value": message,
28+
"onChange": lambda event: set_message(event["value"]),
29+
}
30+
),
31+
html.button({"type": "submit"}, "Send"),
32+
)
33+
34+
35+
run(App)

docs/source/adding-interactivity/components-with-state.rst

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ increment the ``index`` and, as a result, change what image is displayed. Howeve
1717
does not work:
1818

1919
.. idom:: _examples/when_variables_arent_enough
20-
:activate-result:
2120

2221
.. note::
2322

@@ -86,7 +85,6 @@ We'll talk more about what this is doing :ref:`shortly <your first hook>`, but f
8685
now let's just verify that this does in fact fix the problems from before:
8786

8887
.. idom:: _examples/adding_state_variable
89-
:activate-result:
9088

9189

9290
Your First Hook
@@ -104,6 +102,8 @@ words, you'll "use" hooks at the top of your component in the same way you might
104102
"import" modules at the top of your Python files.
105103

106104

105+
.. _Introduction to use_state:
106+
107107
Introduction to ``use_state``
108108
-----------------------------
109109

@@ -301,7 +301,6 @@ you need to in one component. For example, in the example below we've added a
301301
when the user clicks the "Show details" button is this description shown:
302302

303303
.. idom:: _examples/multiple_state_variables
304-
:activate-result:
305304

306305
It's generally a good idea to define separate state variables if the data they represent
307306
is unrelated. In this case, ``index`` corresponds to what sculpture information is being
@@ -328,8 +327,7 @@ changes to its logic. Try clicking the buttons inside each of the galleries. Not
328327
their state is independent:
329328

330329
.. idom:: _examples/isolated_state
331-
:activate-result:
332-
:result-is-default-tab:
330+
:result-is-default-tab:
333331

334332
This is what makes state different from regular variables that you might declare at the
335333
top of your module. State is not tied to a particular function call or a place in the

docs/source/adding-interactivity/index.rst

-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ define synchronous or asynchronous functions that are triggered when a particula
6060
interaction occurs like clicking, hovering, of focusing on form inputs, and more.
6161

6262
.. idom:: _examples/button_prints_message
63-
:activate-result:
6463

6564
It may feel weird to define a function within a function like this, but doing so allows
6665
the ``handle_event`` function to access information from within the scope of the
@@ -90,7 +89,6 @@ updated with a "hook" called ``use_state()`` that creates a **state variable** a
9089
**state setter** respectively:
9190

9291
.. idom:: _examples/adding_state_variable
93-
:activate-result:
9492

9593
In IDOM, ``use_state``, as well as any other function whose name starts with ``use``, is
9694
called a "hook". These are special functions that should only be called while IDOM is
@@ -132,7 +130,6 @@ recipient between the time the "Send" button was clicked and the moment the mess
132130
actually sent?
133131

134132
.. idom:: _examples/print_chat_message
135-
:activate-result:
136133

137134
As it turns out, changing the message recipient after pressing send does not change
138135
where the message ulitmately goes. However, one could imagine a bug where the recipient

docs/source/adding-interactivity/responding-to-events.rst

-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Adding Event Handlers
1313
To start out we'll just display a button that, for the moment, doesn't do anything:
1414

1515
.. idom:: _examples/button_does_nothing
16-
:activate-result:
1716

1817
To add an event handler to this button we'll do three things:
1918

@@ -22,7 +21,6 @@ To add an event handler to this button we'll do three things:
2221
3. Add an ``"onClick": handle_event`` attribute to the ``<button>`` element.
2322

2423
.. idom:: _examples/button_prints_event
25-
:activate-result:
2624

2725
.. note::
2826

@@ -35,7 +33,6 @@ component. That's important if you want to use any arguments that may have beend
3533
your component in the handler:
3634

3735
.. idom:: _examples/button_prints_message
38-
:activate-result:
3936

4037
With all that said, since our ``handle_event`` function isn't doing that much work, if
4138
we wanted to streamline our component definition, we could pass in our event handler as a
@@ -81,7 +78,6 @@ common while still giving its usages customizablity. Consider the case below whe
8178
want to create a generic ``Button`` component that can be used for a variety of purpose:
8279

8380
.. idom:: _examples/button_handler_as_arg
84-
:activate-result:
8581

8682

8783
Async Event Handlers
@@ -95,7 +91,6 @@ message in the first button. However, because the event handler is asynchronous,
9591
handler for the second button is still able to respond:
9692

9793
.. idom:: _examples/button_async_handlers
98-
:activate-result:
9994

10095

10196
Event Data Serialization
@@ -109,7 +104,6 @@ elements. Normally this would be accessible via ``event.target.currenTime``, but
109104
it's simply passed in under the key ``currentTime``:
110105

111106
.. idom:: _examples/audio_player
112-
:activate-result:
113107

114108

115109
Client-side Event Behavior
@@ -130,7 +124,6 @@ using the :func:`~idom.core.events.event` decorator and setting ``prevent_defaul
130124
example, we can stop a link from going to the specified URL:
131125

132126
.. idom:: _examples/prevent_default_event_actions
133-
:activate-result:
134127

135128
Unfortunately this means you cannot conditionally prevent default behavior in response
136129
to event data without writing :ref:`Custom Javascript Components`.
@@ -148,4 +141,3 @@ will cause the handler for the outer blue one to fire. Conversely, when it's off
148141
the handler for the red element will fire.
149142

150143
.. idom:: _examples/stop_event_propagation
151-
:activate-result:

docs/source/adding-interactivity/state-as-a-snapshot.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ sent!":
99

1010
.. image:: _static/direct-state-change.png
1111

12-
12+
IDOM works a bit differently though. Previously, you'll have seen how, in order to
13+
change the view you need to :ref:`"set state" <Introduction to use_state>`. Doing so
14+
then triggers a re-render of the view.
1315

1416
.. image:: _static/idom-state-change.png
17+
18+
...
19+
20+
.. idom:: _examples/send_message

docs/source/creating-interfaces/index.rst

-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ create them we need to add an ``@component`` `decorator
9595
practice we'll quickly make a ``Photo`` component:
9696

9797
.. idom:: _examples/simple_photo
98-
:activate-result:
9998

10099
.. card::
101100
:link: your-first-components
@@ -118,7 +117,6 @@ One case where we might want to do this is if items in a todo list come from a l
118117
data that we want to sort and filter:
119118

120119
.. idom:: _examples/todo_list_with_keys
121-
:activate-result:
122120

123121
.. card::
124122
:link: rendering-data

docs/source/creating-interfaces/rendering-data.rst

-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ This list of elements can then be passed into a parent ``<ul>`` element:
5353
The last thing we have to do is return this from a component:
5454

5555
.. idom:: _examples/todo_from_list
56-
:activate-result:
5756

5857

5958
Filtering and Sorting Elements
@@ -107,7 +106,6 @@ and then ordering the elements based on the ``priority``:
107106
We could then add this code to our ``DataList`` component:
108107

109108
.. idom:: _examples/sorted_and_filtered_todo_list
110-
:activate-result:
111109

112110

113111
Organizing Items With Keys
@@ -151,7 +149,6 @@ or ``sort_by_priority`` the items in our ``<ul>`` element would change. Given th
151149
here's how we'd change our component:
152150

153151
.. idom:: _examples/todo_list_with_keys
154-
:activate-result:
155152

156153

157154
Keys for Components

docs/source/creating-interfaces/your-first-components.rst

-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ like classes - with ``CamelCase``. So consider what we would do if we wanted to
1919
and then :ref:`display <Running IDOM>` a ``Photo`` component:
2020

2121
.. idom:: _examples/simple_photo
22-
:activate-result:
2322

2423
.. warning::
2524

@@ -41,7 +40,6 @@ components. This is part of what makes components so powerful - you can define a
4140
component once and use it wherever and however you need to:
4241

4342
.. idom:: _examples/nested_photos
44-
:activate-result:
4543

4644

4745
Parametrizing Components
@@ -53,7 +51,6 @@ are parametrized by dictionaries, since components behave like typical functions
5351
give them positional and keyword arguments as you would normally:
5452

5553
.. idom:: _examples/parametrized_photos
56-
:activate-result:
5754

5855

5956
Conditional Rendering
@@ -65,19 +62,16 @@ have been completed. Below we have a basic implementation for such a list except
6562
the ``Item`` component doesn't change based on whether it's ``done``:
6663

6764
.. idom:: _examples/todo_list
68-
:activate-result:
6965

7066
Let's imagine that we want to add a ✔ to the items which have been marked ``done=True``.
7167
One way to do this might be to write an ``if`` statement where we return one ``li``
7268
element if the item is ``done`` and a different one if it's not:
7369

7470
.. idom:: _examples/bad_conditional_todo_list
75-
:activate-result:
7671

7772
As you can see this accomplishes our goal! However, notice how similar ``html.li(name, "
7873
✔")`` and ``html.li(name)`` are. While in this case it isn't especially harmful, we
7974
could make our code a little easier to read and maintain by using an "inline" ``if``
8075
statement.
8176

8277
.. idom:: _examples/good_conditional_todo_list
83-
:activate-result:

docs/source/getting-started/index.rst

-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ This should automatically open up a browser window to a page that looks like thi
6262
.. card::
6363

6464
.. idom-view:: _examples/sample_app
65-
:activate-result:
6665

6766
If you get a ``RuntimeError`` similar to the following:
6867

@@ -96,7 +95,6 @@ been installed. Running a tiny "hello world" application just requires the follo
9695
code:
9796

9897
.. idom:: _examples/hello_world
99-
:activate-result:
10098

10199
.. card::
102100
:link: running-idom

docs/source/getting-started/running-idom.rst

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ implementations whose dependencies have all been installed. Running a tiny "hell
77
application just requires the following code:
88

99
.. idom:: _examples/hello_world
10-
:activate-result:
1110

1211
.. note::
1312

@@ -56,7 +55,6 @@ Among other things, running in this mode:
5655
Errors will be displayed where the uppermost component is located in the view:
5756

5857
.. idom:: _examples/debug_error_example
59-
:activate-result:
6058

6159

6260
Choosing a Server Implementation

docs/source/index.rst

-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ To get a rough idea of how to write apps in IDOM, take a look at the tiny `"hell
5151
<https://en.wikipedia.org/wiki/%22Hello,_World!%22_program>`__ application below:
5252

5353
.. idom:: getting-started/_examples/hello_world
54-
:activate-result:
5554

5655
.. hint::
5756

@@ -120,7 +119,6 @@ and organized into components. Then, you'll use this knowledge to create interfa
120119
raw data:
121120

122121
.. idom:: creating-interfaces/_examples/todo_list_with_keys
123-
:activate-result:
124122

125123
.. card::
126124
:link: creating-interfaces/index
@@ -144,7 +142,6 @@ updated with a "hook" called ``use_state()`` that creates a **state variable** a
144142
**state setter** respectively:
145143

146144
.. idom:: adding-interactivity/_examples/adding_state_variable
147-
:activate-result:
148145

149146
In IDOM, ``use_state``, as well as any other function whose name starts with ``use``, is
150147
called a "hook". These are special functions that should only be called while IDOM is

0 commit comments

Comments
 (0)