Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Steps/Dot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ import SliderContext from '../context';
export interface DotProps {
prefixCls: string;
value: number;
marksValue: number[];
style?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
activeStyle?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
}

export default function Dot(props: DotProps) {
const { prefixCls, value, style, activeStyle } = props;
const { prefixCls, marksValue, value, style, activeStyle } = props;
const { min, max, direction, included, includedStart, includedEnd } =
React.useContext(SliderContext);

const dotClassName = `${prefixCls}-dot`;
const active = included && includedStart <= value && value <= includedEnd;

// It defines the className for the marks dots.
const marksDotClassName = `${prefixCls}-marks-dot`;
const marksDot = marksValue.indexOf(value) >= 0;

// ============================ Offset ============================
let mergedStyle = {
...getDirectionStyle(direction, value, min, max),
Expand All @@ -35,6 +40,7 @@ export default function Dot(props: DotProps) {
<span
className={classNames(dotClassName, {
[`${dotClassName}-active`]: active,
[marksDotClassName]: marksDot,
})}
style={mergedStyle}
/>
Expand Down
7 changes: 7 additions & 0 deletions src/Steps/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import type { InternalMarkObj } from '../Marks';
import type { DotProps } from './Dot';
import SliderContext from '../context';
import Dot from './Dot';

Expand All @@ -15,6 +16,8 @@ export default function Steps(props: StepsProps) {
const { prefixCls, marks, dots, style, activeStyle } = props;
const { min, max, step } = React.useContext(SliderContext);

const marksValueRef = React.useRef<DotProps['marksValue']>([]);

const stepDots = React.useMemo(() => {
const dotSet = new Set<number>();

Expand All @@ -23,6 +26,9 @@ export default function Steps(props: StepsProps) {
dotSet.add(mark.value);
});

//Fill marksValue
marksValueRef.current = Array.from(dotSet);

// Fill dots
if (dots && step !== null) {
let current = min;
Expand All @@ -40,6 +46,7 @@ export default function Steps(props: StepsProps) {
{stepDots.map((dotValue) => (
<Dot
prefixCls={prefixCls}
marksValue={marksValueRef.current}
key={dotValue}
value={dotValue}
style={style}
Expand Down
2 changes: 2 additions & 0 deletions tests/marks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ describe('marks', () => {
const marks = { 0: 0, 30: '30', 99: '', 100: '100' };

const { container } = render(<Slider value={30} marks={marks} />);
expect(container.getElementsByClassName('rc-slider-marks-dot')).toHaveLength(3);
expect(container.getElementsByClassName('rc-slider-mark-text')).toHaveLength(3);
expect(container.getElementsByClassName('rc-slider-mark-text')[0].innerHTML).toBe('0');
expect(container.getElementsByClassName('rc-slider-mark-text')[1].innerHTML).toBe('30');
expect(container.getElementsByClassName('rc-slider-mark-text')[2].innerHTML).toBe('100');

const { container: container2 } = render(<Slider range value={[0, 30]} marks={marks} />);
expect(container2.getElementsByClassName('rc-slider-marks-dot')).toHaveLength(3);
expect(container2.getElementsByClassName('rc-slider-mark-text')).toHaveLength(3);
expect(container2.getElementsByClassName('rc-slider-mark-text')[0].innerHTML).toBe('0');
expect(container2.getElementsByClassName('rc-slider-mark-text')[1].innerHTML).toBe('30');
Expand Down