TEMPLATE DIRECTIVE
Overview
Directives is a way in Still.js to deal with some UI specific features on the template side of things, which also provides capabilities to handle DOM tree and elements eliminating the need for diracte manipulating it in some specific situations.
In Still.js, directives are added as HTML tag properties enclosed in parentheses (e.g., (click)), visually distinguishing them from standard HTML attributes.
There are some directives available, and they are addressing things in different categories such as event binding
, form binding
, looping
, rendering
& conditional
, form validation
, follow a list bellow:
Event Directive
Those directives allow to tie specific kind of event to HTML form input by specifying the component method to be called, follow the list:
Directive | Applicable to | Description | Use context |
(click) | Any HTML element/tag | It adds the click event to the target element by providing access to the component methods. |
|
(change) | HTML <select>/combobox element | It binds a component method to a combo-box allowing detect change on it. |
|
The (click)
Directive example:
Click event binding considering a component implemented method.
This is the component code snippet | |
---|---|
The (change)
Directive example:
Change event binding for HTML <select> tag/combobox ti check whenever a value gets selected.
Changes in the regular HTML input can be handled through the state change in the bound state as shown in the (value) directive example.
Event Binding considerations
In general, Still.js concerns especially with click event not to miss the capability to bind component methods, anyway, therefore, when it comes to other events (except for onchange for combobox) the approach to follow is regular DOM manipulation and JavaScript since Still.js is pure javaScript.
Container Directives
Those directives are placed in any HTML element that can enclose other elements thereby providing features like looping through a list, rendering/not rendering, hide/unhide:
Directive | Applicable to | Description | Use context |
(forEach) | Any HTML wrapper elemen (e.g. <div>, <section>, <span>, <select>, etc.) | Allow looping through a list of elements while printing each element in the UI. |
|
(renderIf) | Any HTML wrapper elemen (e.g. <div>, <section>, <span>, <select>, etc.) | Render/Not render an element according to a flag value (true/false) |
|
(showIf) | Any HTML wrapper elemen (e.g. <div>, <section>, <span>, <select>, etc.) | Hide/Unhide render an element according to a flag value (true/false) |
|
The (forEach)
Directive example:
It loops through a list/array of any type of element allowing the maping of it to some kind of desirable output.
Result:
The (renderIf)
Directive example:
This comes into place especially in it comes to handle parts of the App that can or cannot render according to the user permission, if it does not reneders, it woun't even in the DOM tree.
(renderIf) works with flag, which true will make it to render, and false not. The flag is defined in the component itself and needs to be annotated with @Prop
annotation. When referencing the flag variable it's required to use the self.
prefix.
The (showIf)
Directive example:
In contrast to (renderIf), the (showIf) is to be used if we still need to render the UI/component part but keep it hidden.
(renderIf) works with flag, which true will make it to render, and false not. The flag is defined in the component itself and needs to be annotated with @Prop
annotation. When referencing the flag variable it's required to use the self.
prefix.
Animated result:
Form Binding Directives
Directives in the Form binding category allows to handle 2-way binding as wey as handle validation as needed when dealing with form submit, follow hte list:
Directive | Applicable to | Description | Use context |
(formRef)
(required) or (validator) |
HTML form/form tag | Privides a form referance variable to allow handling form validation, however, only makes sense if validation is in place |
|
(value) | HTML form input element (e.g. <input>, <select>, except for radio and checkbox, as thier bind is done with (field) directive instead) | Provides two-way binding for a form input against a component state |
|
(field) | HTML form, Radio button and Checkboxes | Provides two-way binding for a form input against a component state |
|
(labelAfter) | Radio button and Checkboxes text label | Puts the specified text label right after (beside) the Radiobutton/Checkbox |
|
(labelBefore) | Radio button and Checkboxes text label | Puts the specified text label right before (beside) the Radiobutton/Checkbox |
|
The (formRef)
Directive example:
For (formRef)
to be effective, form inputs must use validation directives, be bound to component state variables via (value)
, and include either (required)
and/or (validator)
directives to enable validation.
At the end of the day, the validation variable which is assigned to the (formRef)
then is provided with validate()
method among others, when it gets call, if all validations are passing it'll return true, otherwise it's false, see the result:
Animated result:
The (value)
Directive example:
In addition to be a complement for the (formRef)
when it comes to validation, (value)
directive allows 2-way biding with component state variables.
(renderIf) works with flag, which true will make it to render, and false not. The flag is defined in the component itself and needs to be annotated with @Prop
annotation. When referencing the flag variable it's required to use the self.
prefix.
Animated result:
Form Validation Directives
Validation Directives make it quite easy to handle validation in Still.js very few lines where only the directive concerning the specific validation needs to be used, follow hte list:
Directive | Applicable to | Description | Use context |
(required) | HTML form input element (e.g. <input>, <select>) | Mark a form elemen as required which gets triggered when validation checked through (formRef) |
|
(validator) | HTML <input> tag | Specify of valid data type for an input field |
|
(validator-max)
|
HTML numeric <input> tag | Specify the max value allowed in the field |
|
(validator-max-warn)
|
HTML numeric <input> tag | Specify message to display if max allowed value is violeted |
|
(validator-min)
|
HTML numeric <input> tag | Specify the min value allowed in the field |
|
(validator-min-warn)
|
HTML numeric <input> tag | Specify message to display if min allowed value is violeted |
|
(validator-trigger)
|
HTML <input> tag | Specify (validator) should trigger (e.g. when typing, when focus, when blur) |
|
The (required)
Directive example:
It defines the HTML form field/input element as mandatory thereby making it marked for validation, therefore, proper validation requires the form to have a reference (see this example), and validation is verified through it.
The (validator)
Directive example:
This directive works with (value)
to define valid input types for form elements. Still.js offers 7 default validators, and custom validators can also be implemented.
Follow the built-in Validators:
- number - Allow only number in the target filed
- alphanumeric - Allow both number and text but not special characters
- text - Allow anything
- email - Allow email only
- phone - Allow Telephone number format
- date - Allow data
- dateUS - Allow date in USA format
Using a validator
To use it we only need to assign its name to the directive (e.g. (validator)="number"
) hence, every name has to be unique.
Animated result:
When used in an input text field, unless we override the trigger (by defining (validator-trigger)
directive) the validation happens as we type the value. When it comes to combo-box, the validator only takes affect through the (formRef)
by calling .validate()
as in this example.
Implementing Custom Validator
Custom validators are typically added at the application level in StillAppSetup
(app-setup.js
). However, they can also be registered elsewhere, like directly within a component, using the .addValidator()
utility method.
This is the component code snippet | |
---|---|
Animated result:
The (validator-trigger)
directive
This state when the validation will happen if (required)
and/or (validator)
is/are defined, therefore, there are 3 type as follow:
- typing - Default for input text, it makes the validation to trigger as we're typing.
- losefocus - Check validation when the cursor is moved out from the target input.
- focus - Checks validation when the cursor is placed inside the target input.