Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Slider: fix issue #2877 mark fill inverted slider #2894

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/mantine-core/src/Slider/Marks/Marks.tsx
Expand Up @@ -17,6 +17,7 @@ export interface MarksProps extends DefaultProps<MarksStylesNames> {
onChange(value: number): void;
offset?: number;
disabled: boolean;
inverted?: boolean;
}

export function Marks({
Expand All @@ -32,6 +33,7 @@ export function Marks({
onChange,
disabled,
unstyled,
inverted,
}: MarksProps) {
const { classes, cx } = useStyles(
{ size, color, disabled },
Expand All @@ -46,7 +48,7 @@ export function Marks({
>
<div
className={cx(classes.mark, {
[classes.markFilled]: isMarkFilled({ mark, value, offset }),
[classes.markFilled]: isMarkFilled({ mark, value, offset, inverted }),
})}
/>
{mark.label && (
Expand Down
14 changes: 14 additions & 0 deletions src/mantine-core/src/Slider/Marks/is-mark-filled.test.ts
Expand Up @@ -24,4 +24,18 @@ describe('@mantine/core/Slider/is-mark-filled', () => {
expect(isMarkFilled({ mark: { value: 0 }, offset: -10, value: 90 })).toBe(true);
expect(isMarkFilled({ mark: { value: -10 }, offset: -20, value: 90 })).toBe(true);
});

it('correctly detects filled mark with inverted slider', () => {
expect(isMarkFilled({ mark: { value: 10 }, value: 90, inverted: true })).toBe(false);
expect(isMarkFilled({ mark: { value: 90 }, value: 80, inverted: true })).toBe(true);
});
it('correctly detects filled mark with offset and inverted slider', () => {
expect(isMarkFilled({ mark: { value: 10 }, offset: 10, value: 90, inverted: true })).toBe(true);
expect(isMarkFilled({ mark: { value: 100 }, offset: 30, value: 90, inverted: true })).toBe(
true
);
expect(isMarkFilled({ mark: { value: 40 }, offset: 10, value: 90, inverted: true })).toBe(
false
);
});
});
9 changes: 7 additions & 2 deletions src/mantine-core/src/Slider/Marks/is-mark-filled.ts
Expand Up @@ -2,10 +2,15 @@ interface IsMarkFilled {
mark: { value: number; label?: any };
offset?: number;
value: number;
inverted?: boolean;
}

export function isMarkFilled({ mark, offset, value }: IsMarkFilled) {
return typeof offset === 'number'
export function isMarkFilled({ mark, offset, value, inverted = false }: IsMarkFilled) {
return inverted
? typeof offset === 'number'
? mark.value <= offset || mark.value >= value
: mark.value >= value
: typeof offset === 'number'
? mark.value >= offset && mark.value <= value
: mark.value <= value;
}
1 change: 1 addition & 0 deletions src/mantine-core/src/Slider/Track/Track.tsx
Expand Up @@ -69,6 +69,7 @@ export function Track({
styles={styles}
disabled={disabled}
unstyled={unstyled}
inverted={inverted}
/>
</div>
);
Expand Down