-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kit):
Slider
readonly state (mobile chrome bug)
- Loading branch information
1 parent
6dfa459
commit 3eb0bbc
Showing
2 changed files
with
41 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 40 additions & 21 deletions
61
projects/kit/components/slider/slider-readonly.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,60 @@ | ||
import {Directive, HostListener, Input} from '@angular/core'; | ||
import {tuiDefaultProp} from '@taiga-ui/cdk'; | ||
import { | ||
ChangeDetectorRef, | ||
Directive, | ||
HostListener, | ||
Inject, | ||
Input, | ||
Optional, | ||
Self, | ||
} from '@angular/core'; | ||
import {NgControl} from '@angular/forms'; | ||
import {tuiDefaultProp, TuiDestroyService, watch} from '@taiga-ui/cdk'; | ||
import {Observable} from 'rxjs'; | ||
import {takeUntil} from 'rxjs/operators'; | ||
|
||
const SLIDER_INTERACTION_KEYS = new Set([ | ||
'ArrowLeft', | ||
'ArrowRight', | ||
'ArrowUp', | ||
'ArrowDown', | ||
'Home', | ||
'End', | ||
'PageUp', | ||
'PageDown', | ||
]); | ||
import {TuiSliderComponent} from './slider.component'; | ||
|
||
/** | ||
* Native <input type='range' readonly> doesn't work. | ||
* This directive imitates this native behaviour. | ||
*/ | ||
@Directive({ | ||
selector: 'input[tuiSlider][readonly]', | ||
providers: [TuiDestroyService], | ||
}) | ||
export class TuiSliderReadonlyDirective { | ||
private lastValue = this.slider.value; | ||
|
||
@Input() | ||
@tuiDefaultProp() | ||
readonly: '' | boolean = true; | ||
|
||
@HostListener('mousedown', ['$event']) | ||
@HostListener('touchstart', ['$event']) | ||
preventEvent(event: Event) { | ||
if (this.readonly === '' || this.readonly) { | ||
event.preventDefault(); | ||
get computedReadonly(): boolean { | ||
return this.readonly === '' || this.readonly; | ||
} | ||
|
||
constructor( | ||
@Optional() | ||
@Self() | ||
@Inject(NgControl) | ||
readonly ngControl: NgControl | null, | ||
@Inject(ChangeDetectorRef) changeDetectorRef: ChangeDetectorRef, | ||
@Inject(TuiDestroyService) destroy$: Observable<unknown>, | ||
@Inject(TuiSliderComponent) private readonly slider: TuiSliderComponent, | ||
) { | ||
if (ngControl !== null) { | ||
ngControl.valueChanges | ||
?.pipe(watch(changeDetectorRef), takeUntil(destroy$)) | ||
.subscribe(() => { | ||
this.lastValue = this.slider.value; | ||
}); | ||
} | ||
} | ||
|
||
@HostListener('keydown', ['$event']) | ||
preventKeyboardInteraction(event: KeyboardEvent) { | ||
if (SLIDER_INTERACTION_KEYS.has(event.key)) { | ||
this.preventEvent(event); | ||
@HostListener('input') | ||
onInput() { | ||
if (this.computedReadonly) { | ||
this.slider.value = this.lastValue; | ||
} | ||
} | ||
} |