angular / angular/components

feat(slider): Allow users to manually set custom min & max values on slider thumbs

Aperta
#27,208 7 commenti 1 reazione 0 assegnatari Vedi su GitHub
area: material/slider feature P3
Lingua principale
TypeScript
Stelle
25k
Fork
6.8k
Merge medio
1g 8h
PR unite (30g)
91

Descrizione

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

This is somewhat similar to an older issue with mat-checkbox (https://github.com/angular/components/issues/22149)

I want to ensure that the range in the slider is always greater than zero. E.g. if the "end" value is currently 9, then a user can only move the "start" thumb as high as 8.

I'm enforcing it by subscribing to `valueChanges` of the form controls associated with the slider input elements.

```
this.start.valueChanges.subscribe(() => {
if (this.start.value >= this.end.value) {
this.start.setValue(this.end.value - 1, { emitEvent: false });
}
});
```
Even though the code above guarantees the form control values satisfy the constraint, the underlying DOM `input` element doesn't reflect the programmatic change from the `setValue` call.

A workaround I'm currently using is injecting the `input` elements (via `@ViewChild`) and setting its `.value` directly.

### Reproduction

StackBlitz link: https://stackblitz.com/edit/benvwf-lmrtl4?file=src%2Fexample%2Fslider-range-example.ts
Steps to reproduce:
1. Drag the "start" thumb all the way to the right

### Expected Behavior

The "start" thumb stops at the "8" tick mark, never reaches the last "9".

### Actual Behavior

- The "start" thumb goes up to the "end" position ("9").
- The form control value (printed above the slider) shows "8" - out of sync with the actual thumb state in the DOM

### Environment

- Angular: 16
- CDK/Material: 16
- Browser(s): Firefox
- Operating System (e.g. Windows, macOS, Ubuntu): macOS

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con la riproduzione StackBlitz collegata e traccia il modo in cui i controlli del modulo di inizio e fine dello slider aggiornano i rispettivi elementi input sottostanti dopo setValue(..., { emitEvent: false }). Verifica che il fix mantenga sincronizzati la posizione del thumb, il valore del DOM e il valore del controllo del modulo, garantendo al contempo che il valore di inizio sia inferiore al valore di fine.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
angular, typescript
Ambito
frontend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.