Skip to content

Commit 11da53c

Browse files
feat: add readonly state for slider (#2978)
Co-authored-by: Akshat Patel <[email protected]>
1 parent 3cbed35 commit 11da53c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/slider/slider.component.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ import { EventService } from "carbon-components-angular/utils";
7272
<ng-container *ngIf="!isTemplate(label)">{{label}}</ng-container>
7373
<ng-template *ngIf="isTemplate(label)" [ngTemplateOutlet]="label"></ng-template>
7474
</label>
75-
<div class="cds--slider-container">
75+
<div
76+
class="cds--slider-container"
77+
[ngClass]="{ 'cds--slider-container--readonly': readonly }">
7678
<label [id]="bottomRangeId" class="cds--slider__range-label">
7779
<ng-content select="[minLabel]"></ng-content>
7880
</label>
7981
<div
8082
class="cds--slider"
8183
(click)="onClick($event)"
82-
[ngClass]="{'cds--slider--disabled': disabled}">
84+
[ngClass]="{
85+
'cds--slider--disabled': disabled,
86+
'cds--slider--readonly': readonly
87+
}">
8388
<ng-container *ngIf="!isRange()">
8489
<div class="cds--slider__thumb-wrapper"
8590
[ngStyle]="{insetInlineStart: getFractionComplete(value) * 100 + '%'}">
@@ -276,6 +281,18 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
276281
get disabled() {
277282
return this._disabled;
278283
}
284+
/** Set to `true` for a readonly state. */
285+
@Input() set readonly(v: boolean) {
286+
this._readonly = v;
287+
// for some reason `this.input` never exists here, so we have to query for it here too
288+
const inputs = this.getInputs();
289+
if (inputs && inputs.length > 0) {
290+
inputs.forEach(input => input.readOnly = v);
291+
}
292+
}
293+
get readonly() {
294+
return this._readonly;
295+
}
279296
/** Emits every time a new value is selected */
280297
@Output() valueChange: EventEmitter<number | number[]> = new EventEmitter();
281298
@HostBinding("class.cds--form-item") hostClass = true;
@@ -297,6 +314,7 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
297314
protected _value = [this.min];
298315
protected _previousValue = [this.min];
299316
protected _disabled = false;
317+
protected _readonly = false;
300318
protected _focusedThumbIndex = 0;
301319

302320
constructor(
@@ -455,7 +473,7 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
455473
* Will assign the value to the closest thumb if in range mode.
456474
* */
457475
onClick(event) {
458-
if (this.disabled) { return; }
476+
if (this.disabled || this.readonly) { return; }
459477
const trackLeft = this.track.nativeElement.getBoundingClientRect().left;
460478
const trackValue = this.convertToValue(event.clientX - trackLeft);
461479
if (this.isRange()) {
@@ -478,7 +496,7 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
478496

479497
/** Mouse move handler. Responsible for updating the value and visual selection based on mouse movement */
480498
onMouseMove(event) {
481-
if (this.disabled || !this.isMouseDown) { return; }
499+
if (this.disabled || this.readonly || !this.isMouseDown) { return; }
482500
const track = this.track.nativeElement.getBoundingClientRect();
483501

484502
let value;
@@ -513,7 +531,7 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
513531
*/
514532
onMouseDown(event, index = 0) {
515533
event.preventDefault();
516-
if (this.disabled) { return; }
534+
if (this.disabled || this.readonly) { return; }
517535
this._focusedThumbIndex = index;
518536
this.thumbs.toArray()[index].nativeElement.focus();
519537
this.isMouseDown = true;
@@ -530,7 +548,7 @@ export class Slider implements AfterViewInit, ControlValueAccessor {
530548
* @param {boolean} thumb If true then `thumb` is pressed down, otherwise `thumb2` is pressed down.
531549
*/
532550
onKeyDown(event: KeyboardEvent, index = 0) {
533-
if (this.disableArrowKeys) {
551+
if (this.disableArrowKeys || this.readonly) {
534552
return;
535553
}
536554
const multiplier = event.shiftKey ? this.shiftMultiplier : 1;

src/slider/slider.stories.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Template = (args) => ({
2727
[skeleton]="skeleton"
2828
[shiftMultiplier]="shiftMultiplier"
2929
[disabled]="disabled"
30+
[readonly]="readonly"
3031
aria-Label="Label for slider value"
3132
(valueChange)="valueChange($event)">
3233
<span minLabel>{{minLabel}}</span>
@@ -44,6 +45,7 @@ Basic.args = {
4445
minLabel: "0",
4546
maxLabel: "100",
4647
disabled: false,
48+
readonly: false,
4749
shiftMultiplier: 4,
4850
theme: "dark"
4951
};
@@ -68,6 +70,7 @@ const RangeTemplate = (args) => ({
6870
[value]="value"
6971
[shiftMultiplier]="shiftMultiplier"
7072
[disabled]="disabled"
73+
[readonly]="readonly"
7174
aria-Label="Label for slider value"
7275
(valueChange)="valueChange($event)">
7376
<span minLabel>{{minLabel}}</span>

0 commit comments

Comments
 (0)