-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0ea6c8
commit 2246061
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -203,3 +203,61 @@ session.waitTillUserClosesSessionOr(exit) | |
|
||
Now if we run it with `./on-click.sc` and click the button, the script will terminate. | ||
|
||
## Reading updated values | ||
|
||
Some UI element values, like input boxes, can be changed by the user. We can read the changed value at any point of our | ||
code or install an onChange handler so that we read the value as soon as the user changes it. | ||
|
||
Let's see how we can just read the value. The following script will create an email input box and a button. Whenever | ||
the button is pressed, it will read the email and create a new paragraph with the email value. | ||
|
||
[read-changed-value.sc](../example-scripts/read-changed-value.sc) | ||
|
||
![read-value](images/tutorial/read-value.png) | ||
|
||
```scala | ||
#!/usr/bin/env -S scala-cli project.scala | ||
|
||
import org.terminal21.client.* | ||
import org.terminal21.client.components.* | ||
import org.terminal21.client.components.std.Paragraph | ||
import org.terminal21.client.components.chakra.* | ||
|
||
Sessions.withNewSession("read-changed-value-example", "Read Changed Value"): session => | ||
given ConnectedSession = session | ||
|
||
val email = Input(`type` = "email", value = "[email protected]") | ||
val output = Box() | ||
|
||
Seq( | ||
FormControl().withChildren( | ||
FormLabel(text = "Email address"), | ||
InputGroup().withChildren( | ||
InputLeftAddon().withChildren(EmailIcon()), | ||
), | ||
FormHelperText(text = "We'll never share your email.") | ||
), | ||
Button(text = "Read Value").onClick: () => | ||
val value = email.current.value | ||
output.current.addChildren(Paragraph(text = s"The value now is $value")).renderChanges() | ||
, | ||
output | ||
).render() | ||
|
||
session.waitTillUserClosesSession() | ||
``` | ||
|
||
The important bit is this: | ||
|
||
```scala | ||
Button(text = "Read Value").onClick: () => | ||
val value = email.current.value | ||
output.current.addChildren(Paragraph(text = s"The value now is $value")).renderChanges() | ||
``` | ||
|
||
When the button is clicked, we get the current state of the `email` input box via `email.current`. And then get it's value, `email.current.value`. | ||
|
||
Also in order to append a new paragraph to the `output`, we get the current state of it (which includes any previous paragraphs we have added) and then | ||
add a paragraph as a new child. Then we render the changes of `output` which includes the paragraphs. | ||
|
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,30 @@ | ||
#!/usr/bin/env -S scala-cli project.scala | ||
|
||
import org.terminal21.client.* | ||
import org.terminal21.client.components.* | ||
import org.terminal21.client.components.std.Paragraph | ||
import org.terminal21.client.components.chakra.* | ||
|
||
Sessions.withNewSession("read-changed-value-example", "Read Changed Value"): session => | ||
given ConnectedSession = session | ||
|
||
val email = Input(`type` = "email", value = "[email protected]") | ||
val output = Box() | ||
|
||
Seq( | ||
FormControl().withChildren( | ||
FormLabel(text = "Email address"), | ||
InputGroup().withChildren( | ||
InputLeftAddon().withChildren(EmailIcon()), | ||
), | ||
FormHelperText(text = "We'll never share your email.") | ||
), | ||
Button(text = "Read Value").onClick: () => | ||
val value = email.current.value | ||
output.current.addChildren(Paragraph(text = s"The value now is $value")).renderChanges() | ||
, | ||
output | ||
).render() | ||
|
||
session.waitTillUserClosesSession() |