|
1 |
| -import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; |
2 |
| -import { IgxInputDirective, IgxInputState, IgxSliderComponent, IgxSliderType } from 'igniteui-angular'; |
3 |
| -import { fromEvent, Subject } from 'rxjs'; |
4 |
| -import { takeUntil } from 'rxjs/operators'; |
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { IgxSliderType } from 'igniteui-angular'; |
5 | 3 |
|
6 | 4 | @Component({
|
7 | 5 | selector: 'app-slider-sample-1',
|
8 | 6 | styleUrls: ['./slider-sample-1.component.scss'],
|
9 | 7 | templateUrl: './slider-sample-1.component.html'
|
10 | 8 | })
|
11 |
| -export class SliderSample1Component implements OnInit, OnDestroy { |
12 |
| - |
13 |
| - @ViewChild(IgxSliderComponent, { static: true }) |
14 |
| - public slider: IgxSliderComponent; |
15 |
| - |
16 |
| - @ViewChild('lowerInput', { read: IgxInputDirective, static: true }) |
17 |
| - public lowerInput: IgxInputDirective; |
18 |
| - |
19 |
| - @ViewChild('upperInput', { read: IgxInputDirective, static: true }) |
20 |
| - public upperInput: IgxInputDirective; |
| 9 | +export class SliderSample1Component { |
21 | 10 |
|
22 | 11 | public sliderType = IgxSliderType;
|
23 |
| - public priceRange: PriceRange = new PriceRange(200, 800); |
24 |
| - public $destroyer = new Subject<boolean>(); |
| 12 | + public priceRange = { |
| 13 | + lower: 200, |
| 14 | + upper: 800 |
| 15 | + }; |
25 | 16 |
|
26 | 17 | constructor() { }
|
27 |
| - |
28 |
| - public ngOnInit() { |
29 |
| - fromEvent(this.lowerInput.nativeElement, 'input').pipe(takeUntil(this.$destroyer)) |
30 |
| - .subscribe((event) => { |
31 |
| - this.validate(this.lowerInput, event); |
32 |
| - }); |
33 |
| - |
34 |
| - fromEvent(this.lowerInput.nativeElement, 'blur').pipe(takeUntil(this.$destroyer)) |
35 |
| - .subscribe((event) => { |
36 |
| - this.validate(this.lowerInput, event); |
37 |
| - }); |
38 |
| - |
39 |
| - fromEvent(this.upperInput.nativeElement, 'input').pipe(takeUntil(this.$destroyer)) |
40 |
| - .subscribe((event) => { |
41 |
| - this.validate(this.upperInput, event); |
42 |
| - }); |
43 |
| - |
44 |
| - fromEvent(this.upperInput.nativeElement, 'blur').pipe(takeUntil(this.$destroyer)) |
45 |
| - .subscribe((event) => { |
46 |
| - this.validate(this.upperInput, event); |
47 |
| - }); |
48 |
| - } |
49 |
| - |
50 |
| - public ngOnDestroy() { |
51 |
| - this.$destroyer.next(true); |
52 |
| - } |
53 |
| - |
54 |
| - public validate(input: IgxInputDirective, event) { |
55 |
| - const val = parseInt(input.value, 10); |
56 |
| - if (isNaN(val) || val > this.slider.upperBound || val < this.slider.lowerBound) { |
57 |
| - input.valid = IgxInputState.INVALID; |
58 |
| - } else { |
59 |
| - input.valid = IgxInputState.VALID; |
60 |
| - this.updatePriceRange(val, event.currentTarget); |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - public updatePriceRange(value, event) { |
65 |
| - const prevPriceRange = this.priceRange; |
66 |
| - switch (event.id) { |
67 |
| - case 'lowerInput': { |
68 |
| - this.priceRange = new PriceRange(value, prevPriceRange.upper); |
69 |
| - break; |
70 |
| - } |
71 |
| - case 'upperInput': { |
72 |
| - this.priceRange = new PriceRange(prevPriceRange.lower, value); |
73 |
| - break; |
74 |
| - } |
75 |
| - } |
76 |
| - } |
77 |
| -} |
78 |
| -class PriceRange { |
79 |
| - constructor( |
80 |
| - public lower: number, |
81 |
| - public upper: number |
82 |
| - ) { |
83 |
| - } |
84 | 18 | }
|
0 commit comments