-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/core] Slider: Fix
restrictToMarks
prop not working with a…
…rrow and Home/End keys correctly
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 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
38 changes: 38 additions & 0 deletions
38
...@mantine/core/src/components/Slider/utils/get-step-mark-value/get-step-mark-value.test.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
getFirstMarkValue, | ||
getLastMarkValue, | ||
getNextMarkValue, | ||
getPreviousMarkValue, | ||
} from './get-step-mark-value'; | ||
|
||
describe('@mantine/core/Slider/get-step-mark-value', () => { | ||
it('returns first mark value', () => { | ||
expect(getFirstMarkValue([{ value: 1 }, { value: 2 }])).toBe(2); | ||
expect(getFirstMarkValue([{ value: 1 }])).toBe(1); | ||
expect(getFirstMarkValue([])).toBe(0); | ||
}); | ||
|
||
it('returns last mark value', () => { | ||
expect(getLastMarkValue([{ value: 1 }, { value: 2 }])).toBe(2); | ||
expect(getLastMarkValue([{ value: 1 }])).toBe(1); | ||
expect(getLastMarkValue([])).toBe(100); | ||
}); | ||
|
||
it('returns next mark value', () => { | ||
expect(getNextMarkValue(1, [{ value: 1 }, { value: 2 }])).toBe(2); | ||
expect(getNextMarkValue(1, [{ value: 2 }, { value: 3 }])).toBe(2); | ||
expect(getNextMarkValue(1, [{ value: 0 }, { value: 2 }])).toBe(2); | ||
expect(getNextMarkValue(1, [{ value: 0 }, { value: 1 }])).toBe(1); | ||
expect(getNextMarkValue(1, [{ value: 1 }])).toBe(1); | ||
expect(getNextMarkValue(1, [])).toBe(1); | ||
}); | ||
|
||
it('returns previous mark value', () => { | ||
expect(getPreviousMarkValue(1, [{ value: 1 }, { value: 2 }])).toBe(1); | ||
expect(getPreviousMarkValue(1, [{ value: 2 }, { value: 3 }])).toBe(1); | ||
expect(getPreviousMarkValue(1, [{ value: 0 }, { value: 2 }])).toBe(0); | ||
expect(getPreviousMarkValue(1, [{ value: 0 }, { value: 1 }])).toBe(0); | ||
expect(getPreviousMarkValue(1, [{ value: 1 }])).toBe(1); | ||
expect(getPreviousMarkValue(1, [])).toBe(1); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...ages/@mantine/core/src/components/Slider/utils/get-step-mark-value/get-step-mark-value.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function getNextMarkValue( | ||
currentValue: number, | ||
marks: { value: number; label?: React.ReactNode }[] | ||
) { | ||
const sortedMarks = [...marks].sort((a, b) => a.value - b.value); | ||
const nextMark = sortedMarks.find((mark) => mark.value > currentValue); | ||
return nextMark ? nextMark.value : currentValue; | ||
} | ||
|
||
export function getPreviousMarkValue( | ||
currentValue: number, | ||
marks: { value: number; label?: React.ReactNode }[] | ||
) { | ||
const sortedMarks = [...marks].sort((a, b) => b.value - a.value); | ||
const previousMark = sortedMarks.find((mark) => mark.value < currentValue); | ||
return previousMark ? previousMark.value : currentValue; | ||
} | ||
|
||
export function getFirstMarkValue(marks: { value: number; label?: React.ReactNode }[]) { | ||
const sortedMarks = [...marks].sort((a, b) => b.value - a.value); | ||
return sortedMarks.length > 0 ? sortedMarks[0].value : 0; | ||
} | ||
|
||
export function getLastMarkValue(marks: { value: number; label?: React.ReactNode }[]) { | ||
const sortedMarks = [...marks].sort((a, b) => a.value - b.value); | ||
return sortedMarks.length > 0 ? sortedMarks[sortedMarks.length - 1].value : 100; | ||
} |