Sample Designs
Checkboxes
Basic Checkbox
This checkbox starts in unchecked state and can be switched on and off by a single Action
Enabled/Disabled
Since a checkbox can be disabled both with checkmark and without it, it requires two State
s for disabled condition.
Direct assignment
With two extra Action
s it's now possible to set the desired State
of a checkbox to a particular condition without prior knowledge of its current State
. For instance, CHECK Action
is guaranteed to bring the checkbox into ON State
, unless it was disabled.
Overwriting disabled states
The same Action
s as above can be applied to disabled State
s, allowing to change the value of checkbox without enabling it. However, not that TOGGLE Action
will not work in disabled State
s.
Radio Group
Here we create an FSM
that stores a list of options in its Context
and allows to select one of them by dispathing a SELECT Action
.
Here we also create a single DISABLED State
, because we uphoisted the state of many controls rather than controlling a single one, as in Checkbox example, allowing us to preserve the visible state of every radio option when being DISABLED.
Also, a special UNSET Action
, that removes the selection: it works by not passing a Payload
, which triggers $index = -1
fallback to default value
Data Loader
The most notable example of a stateful control object in web programming is an asynchronous request, or, in general Javascript, a Promise type. The most basic variant of Mermaid representation of it is already a Yantrix code for an async call:
the more beautiful and best-practice approach to it though would look like this:
Dropdown Control
+Init
marks that INIT is the initialState
of theFSM
.+ByPass
also implies that transition through thisState
is synchronous, i.e. theState
is intermediary. That is emphasized by a special descriptor of the outgoingAction
: [-]#{items, selectedIndex}
describes a shape ofContext
for allStates
.items
is the list of dropdown values, andselectedIndex
stores currently selected item. Without extra expressions these values are copied from the precedingContext
#{items=[], selectedIndex = 0}
sets the initial value for thatContext
#{items, selectedIndex} <= sortBy($optionsList, 'id'), 0
fills bothContext
properties fromPayload
:items
is a sortedoptionsList
property, assuming it's a List of Objects that have property ofid
.selectedIndex
is set to 0, when the list of options is externally updated
Event integration
There's no big reason to create a FSM
for merely a dropdown unless you want to connect it to the global Event Stack
and build something bigger on Yantrix:
subscribe/click => OPEN
in CLOSED state produces OPENAction
on incomingclick
Event
, which transitions theFSM
into OPEN state- likewise,
subscribe/click SELECT (index)
produces SELECTAction
and passesindex
property fromEvent Meta
to itsPayload
, which transitions theFSM
into the SELECTEDState
subscribe/clickOutside => CLOSE
produces a CLOSEAction
to return the dropdown to the originalState
(CLOSED)- Both CLOSED and OPEN
States
emit correspondingEvents
, that are pipelined intoEvent Bus
and connect the component to others. - SELECTED is another sort of intermediary State: while
emit/selected (index) <= #{ selectedIndex }
lets theEvent Bus
know which item was selected, at the same timesubscribe/selected => CLOSE
transitions theFSM
back to CLOSEDState
via CLOSEAction
. It behaves similar to INITState
– theFSM
goes through it. However, unlike that case, here it will emitEvent
and wait for its settlement before it runs further processing.