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@Booleanfor a state that can betrueorfalse. - 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
Artboardand draw aBlockto be the button. - Select the
Blockthat was drawn. - Click on
State Definitions. Add aState Definitionand create a state calledloadingwith typedata2-core-primitives@Booleanand valuefalse. - Return to the beginning of the
Inspectorby 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 Clickand then onActionsto navigate to the action list. - Click on
+ Add Item. Change its type toSetterby clicking onf()and selectingsetter. - Click to navigate to the first action. In
Module, selectUIState. - In
Prop, click onPick Componentand select theBlockthat contains theloadingstate. 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 Itemto add another action. - Change its type to
Delayby 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 + Con Mac) to copy. - Hover over the last action and press
Ctrl + V(orCMD + Von Mac) to paste. - Click on this action and change the
Valuefromtruetofalse.
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
Texttype toIf. - Click on
Ifto edit. InCompare, click onf()and then click onuinext togetter. This will pre-fill theGetterwith theUIState. - Click on
Compareto edit theGetter, and select theBlockthat contains theloadingstate. InState Prop, don't forget to selectloading. - Now, return to
Textand let's finish configuring theIf. InThen, set the text for whenloadingistrue, so enterLoading.... - In
Else, enterClick here.
- Save the
Blockby clicking theSavebutton in theInspectoror by deselecting theBlock. - Click the
Previewbutton on theArtboardto see the result.