Interface States
In this tutorial, we will learn how to use interface states, also called UI States
.
Introduction
While some data should be stored in tables, like user information, products, etc., other data, like loading indicators, are temporary and should only be kept while the user interacts with the application. These temporary data are called user interface states, or UI States
.
Creating a UI State
UI States are defined in the State Definitions
parameter of Blocks
. To create a new state:
- Click on
State Definitions
. - Click on
+ Add Item
. - Click on the state that was added to edit it.
- In
Key
, type the name of the state. - In
Component
, select the type of this state. For example,data2-core-primitives@Boolean
for a state that can betrue
orfalse
. - In
Initial Value
, set the initial value of the state.
To access a UIState
, use the component data2-core-types@Getter
with Module
as UIState
and Prop
as the Block
that contains the state, and State Prop
as the name of the state.
To change a UIState
, use the component data2-core-types@Setter
with Module
as UIState
, Prop
as the Block
that contains the state, State Prop
as the name of the state, and Value
as the new value.
Example: Button with Loading State
In this example, we will create a button with a loading
state. The button will have a UI State called loading
with a value of false
and type data2-core-primitives@Boolean
. On the button's On Click
, we will change the loading
state to true
, and after 2 seconds, change it back to false
.
Step-by-Step
- Create an
Artboard
and draw aBlock
to be the button. - Select the
Block
that was drawn. - Click on
State Definitions
. Add aState Definition
and create a state calledloading
with typedata2-core-primitives@Boolean
and valuefalse
. - Return to the beginning of the
Inspector
by clicking on< Inspector
.
- In
On Click
, click onf()
and selectSeries
.
Series
is a component that executes a list of actions in sequence. Each action is executed after the previous one is completed.
- Click on
On Click
and then onActions
to navigate to the action list. - Click on
+ Add Item
. Change its type toSetter
by clicking onf()
and selectingsetter
. - Click to navigate to the first action. In
Module
, selectUIState
. - In
Prop
, click onPick Component
and select theBlock
that contains theloading
state. If you cannot click on the state name, don't worry; after selecting theBlock
, you can choose the state you want to add from the list inState Prop
. - In
Value
, click onf()
, selectBoolean
, and set the value totrue
(enable the control). - Return to the action list.
- Click on
+ Add Item
to add another action. - Change its type to
Delay
by clicking onf()
and selectingDelay
. - Click to edit the action. In
Delay
, set it to2000
(2 seconds). - Return to the action list.
- Add another action.
- Now we can copy the first action we added. Hover over the action and press
Ctrl + C
(orCMD + C
on Mac) to copy. - Hover over the last action and press
Ctrl + V
(orCMD + V
on Mac) to paste. - Click on this action and change the
Value
fromtrue
tofalse
.
Now, we will use the loading
value to show different text on the button. To do this, we will add Text
to the Block
and change the text according to the loading
value.
- Return to the root of the
Inspector
. - Change the
Text
type toIf
. - Click on
If
to edit. InCompare
, click onf()
and then click onui
next togetter
. This will pre-fill theGetter
with theUIState
. - Click on
Compare
to edit theGetter
, and select theBlock
that contains theloading
state. InState Prop
, don't forget to selectloading
. - Now, return to
Text
and let's finish configuring theIf
. InThen
, set the text for whenloading
istrue
, so enterLoading...
. - In
Else
, enterClick here
.
- Save the
Block
by clicking theSave
button in theInspector
or by deselecting theBlock
. - Click the
Preview
button on theArtboard
to see the result.