- Include the mask-directive folder in your project
- Include it in the declaration inside
app.module.ts
file
declarations: [AppComponent, MaskDirective]
- Call it in the
<input/>
tag where is necessary
The type of the input should betype="text"
<form>
<input
[formControl]="formControl"
type="text"
dateMask />
</form>
If no configuration is provided the default view will be: yyyy-MM-dd hh:mm:ss
The directive is able to copy the behaviour of the html type="date"
input allowing a greater control of the value visualization.
It can accept all time and date possible configurations (dd/mm/yyyy, yyyy/dd/mm and so on)
and also different separators.
The directive modifies the HTML value and also the value of the FormControl connected the the input in real time.
It stops the propagation of the key event and react according to the configuration.
It also applies a validation on the new value; if the validation is negative the directive returns the sigles corresponding to the configuration (eg. dd/mm/yyyy).
import { MaskConfigOptions, Separators, ArrowBehaviours } from "./mask-directive/mask-options";
To use the mask's configuration import the content of the
mask-options.ts
file and configure your own object of type MaskConfigOptions and then inject it in the directive.
<input
type="text"
dateMask
[maskOptions]="maskConfigOptions"
/>
MaskConfigOptions
:type
which contains all the possible configurationsSeparators
: enum with all the separators acceptedArrowBehaviours
: enum with all the arrow scrolling possibilities
To configure the date only style visualization provide a string which contains all the date sigle write in the desired order whitout separations: dd, MM, yy.
public myMaskConfigOptions: MaskConfigOptions = {
dateConfiguration: 'MMddyy'
};
To configure the time only visualization provide a string which contains at least hours and minutes sigles: hh, mm, ss.
The seconds sigle it's optional; use it if you want to show seconds
public myMaskConfigOptions: MaskConfigOptions = {
timeConfiguration: 'hhmm'
};
To configure the date-time full visualization provide both date and time configurations.
public myMaskConfigOptions: MaskConfigOptions = {
dateConfiguration: 'MMyydd',
timeConfiguration: 'ssmmhh'
};
MaskOptions type contains 3 properties to configure the date, the time and the date-time separators.
Use the enum Separators
to assign the desired value.
Each of these options has a default value: dash
for date, dot
for time, space
for date-time.
public myMaskConfigOptions: MaskConfigOptions = {
dateConfiguration: 'MMyydd',
timeConfiguration: 'ssmmhh',
dateSeparator: Separators.colon,
timeSeparator: Separators.space,
dateTimeSeparator: Separators.dot,
};
The ArrowBehaviours
enum contains 3 possible actions for up/down arrow keys.
enum ArrowBehaviours {
circular_without_position,
circular_with_position_and_control,
limited_with_control,
}
////
public myMaskConfigOptions: MaskConfigOptions = {
arrowBehaviours: ArrowBehaviours.limited_with_control
// etc
}
circular_without_position
: when up/down key is held down in every value of the date days increase or decrease by one, when the maximum or minimum days for the month is reached the month increase or decrease by one and the day start from one. The year has the same behaviour according to the month. The time values have circular behaviour.circular_with_position_and_control
: Every value increases or decreases by one, when you move to the next value with left/right key the validator verifies the whole date-time and if isn't valid the last edited value is erased.limited_with_control
: Every value increases or decreases by one, when the max or min is reached the cursor moves to the next value. Every time the validators run and if isn't valid arrow key event cannot proceed.
If it's present in the mask configuration in case of null or empty initial value it replace it with the actual date only after the first action
myMaskConfigOptions: MaskConfigOptions = {
ifNoEntryUseActualDate: true
// etc
}
They set the maximum and minimum value for years, the default values are 1970 and 2050.
myMaskConfigOptions: MaskConfigOptions = {
maxYear: 2000,
minYear: 1990,
// etc
}
- Left and Right arrows: they move the cursor to he next or previous value, if it doesn't exists the focus on the input blurs.
- Return and Tab keys aren't blocked by the directive but they have no behaviours.
- Letters and other keys aren't allowed
- Backspace and Delete erase the value and substitute it with the default sigle value.