Repository: mui/material-ui (TypeScript)
Date: 2022-07-15 19:40:25+00:00
Task type: Bug Fix

Issue:
[Slider] Remove not needed mark in range Sliders
### Duplicates

- [X] I have searched the existing issues

### Latest version

- [X] I have tested the latest version

### Current behavior 😯

Whenever both the start and the end points have been dragged to the end of the slider an extra mark has been added at the end of the slider which is visible when both the drag points aren't at the end.

### Expected behavior 🤔

The extra mark in the slider must be removed and retain the required marks that has been provided in the props.

### Steps to reproduce 🕹

Steps:

1. Add marks to the range slider for both range start and end as follows.
![image](https://user-images.githubusercontent.com/73927837/159708742-1cfaf227-9339-47b0-8ee6-763e367ed64f.png)
2. Drag both the range points to the end of the slider.
![image](https://user-images.githubusercontent.com/73927837/159708852-d4ae0846-499c-439c-84f8-f67f63036244.png)
3. Now drag both the range points from the end to another position.
![image](https://user-images.githubusercontent.com/73927837/159708966-f056f8bb-9ad0-488f-ad33-c107a3d133a7.png)
4. Now we will be able to get three marks where the third one isn't a required one and must have been removed.


### Context 🔦

We have been using the range slider for a requirement in our project, where the user needs to choose certain range and the user needs to be provided with the labels of their selections. Since the marks might overwrite one another we needed to keep the marks to the minimal amount, In this case its getting added up by one.

### Your environment 🌎

<details>
  <summary>`npx @mui/5.5.2`</summary>

```
  Chrome latest version
  Output from `npx @mui/5.5.2` goes here.
```
</details>



Issue discussion:
Hi @SooriyanDSRC, could you please provide a codesandbox showing the problem? It may be that a certain combination of props causes the problem.
I face the same issue. @michaldudak here is a codesandbox link in case it helps.

[https://codesandbox.io/s/material-demo-forked-tlu40e?file=/demo.js](https://codesandbox.io/s/material-demo-forked-tlu40e?file=/demo.js)
Right, definitely a bug. Would you like to contribute to the project and look for a fix?
@michaldudak Sure, I would be happy to work on the fix. The issue seems to be arising due to both marks having the same key as shown in the below image.

<img width="534" alt="image" src="https://user-images.githubusercontent.com/20108695/178384725-cb30fe96-267c-40aa-94fc-3625497256ed.png">

How does this look?

```diff
diff --git a/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js b/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
index 4536cbe2c1..5559e274d4 100644
--- a/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
+++ b/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
@@ -210,7 +210,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
           }
 
           return (
-            <React.Fragment key={mark.value}>
+            <React.Fragment key={index}>
               <Mark
                 data-index={index}
                 {...markProps}
```

Looks good to me. Please create a PR with this change.

Solution patch:
diff --git a/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js b/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
--- a/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
+++ b/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js
@@ -210,7 +210,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
           }
 
           return (
-            <React.Fragment key={mark.value}>
+            <React.Fragment key={index}>
               <Mark
                 data-index={index}
                 {...markProps}


Test patch:
diff --git a/packages/mui-material/src/Slider/Slider.test.js b/packages/mui-material/src/Slider/Slider.test.js
--- a/packages/mui-material/src/Slider/Slider.test.js
+++ b/packages/mui-material/src/Slider/Slider.test.js
@@ -987,6 +987,18 @@ describe('<Slider />', () => {
     expect(container.querySelectorAll(`.${classes.mark}[data-index="2"]`).length).to.equal(1);
   });
 
+  it('should correctly display mark labels when ranges slider have the same start and end', () => {
+    const getMarks = (value) => value.map((val) => ({ value: val, label: val }));
+
+    const { container, setProps } = render(
+      <Slider value={[100, 100]} marks={getMarks([100, 100])} />,
+    );
+    expect(container.querySelectorAll(`.${classes.markLabel}`).length).to.equal(2);
+
+    setProps({ value: [40, 60], marks: getMarks([40, 60]) });
+    expect(container.querySelectorAll(`.${classes.markLabel}`).length).to.equal(2);
+  });
+
   it('should pass "name" and "value" as part of the event.target for onChange', () => {
     const handleChange = stub().callsFake((event) => event.target);
     const { getByRole } = render(