ngx-custom-numeric-range-form-field
Version:
Angular material numeric range form field
111 lines (85 loc) • 4.55 kB
Markdown
# ngx-numeric-range-form-field
An Angular Material UI numeric range input form field. It is based on custom form field control and control value accessor which allows inserting range numbers, minimum and maximum.

<p align="start">
<a href="https://www.npmjs.com/package/ngx-custom-numeric-range-form-field"><img alt="weekly downloads from npm" src="https://img.shields.io/npm/dw/ngx-custom-numeric-range-form-field.svg?style=flat-square"></a>
<a href="https://www.npmjs.com/package/ngx-custom-numeric-range-form-field"><img alt="npm version" src="https://img.shields.io/npm/v/ngx-custom-numeric-range-form-field.svg?style=flat-square"></a>
</p>
[](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)
[](https://github.com/prettier/prettier)
[](https://app.fossa.com/projects/git%2Bgithub.com%2FOubayda-Khayata%2Fngx-numeric-range-form-field?utm_source=share_link)
# Feature
- Two inputs as one field.
- Auto range validation.
- Supports reactive forms.
**[View live demo on StackBlitz.](https://ngx-custom-numeric-range-form-field.stackblitz.io)**
# Install
```shell
npm install ngx-custom-numeric-range-form-field
```
# Usage
In component HTML:
```html
<ngx-numeric-range-form-field
[formControl]="rangeControl"
label="Numeric range"
(blurred)="onBlur()"
(enterPressed)="onEnter()"
(numericRangeChanged)="onValueChange($event)"
></ngx-numeric-range-form-field>
```
In component.ts:
```typescript
form: FormGroup;
constructor() {
this.form = new FormGroup({
range: new FormControl({
minimum: 10,
maximum: 100,
}, [
Validators.required, //optional
Validators.min(10), //optional
Validators.max(100), //optional
]),
});
}
get rangeControl(): FormControl {
return this.form.get('range') as FormControl;
}
onBlur(): void {
console.log('Value', this.rangeControl.value);
}
onEnter(): void {
console.log('Enter pressed!');
}
onValueChange(value: any): void {
console.log('Changed value: ', value);
}
```
Customizable input and output decorators:
```typescript
label: string; // Label of the control
appearance: 'legacy' | 'standard' | 'fill' | 'outline' = 'outline';
floatLabel: 'always' | 'never' | 'auto' = 'always';
minPlaceholder = 'From'; // Placeholder of the minimum value control
maxPlaceholder = 'To'; // Placeholder of the maximum value control
readonly = false; // Indicator wether the both controls are readonly
minReadonly = false; // Indicator wether the minimum control is readonly
maxReadonly = false; // Indicator wether the maximum control is readonly
resettable = true; // Indicator wether the both controls are resettable
required: boolean; // Required validation
requiredErrorMessage = 'Field is required!'; // Customizable error message when field is required
minimumErrorMessage = 'Minimum has been reached!'; // Customizable error message when field has min validation
maximumErrorMessage = 'Maximum has exceeded!'; // Customizable error message when field has max validation
invalidRangeErrorMessage = 'Inserted range is not valid!'; // Customizable error message when field has invalid numeric range
minimumControlName = 'minimum'; // Customizable minimum control name
maximumControlName = 'maximum'; // Customizable maximum control name
updateOn: 'change' | 'blur' | 'submit' = 'change'; // The event name for control to update upon
controlStyle = ''; // Custom control style
blurred = new EventEmitter<void>(); // Event which emits where user leaves control (focus out)
enterPressed = new EventEmitter<void>(); // Event which emits when enter is pressed
numericRangeChanged = new EventEmitter<any>(); // Event which emits when one of range value is changed
```
# License
Apache License
Copyright (c) 2022 Dino Klicek, Edited by Oubayda Khayata