Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
5 changes: 3 additions & 2 deletions src/Steps/Dot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import SliderContext from '../context';

export interface DotProps {
prefixCls: string;
className?: string;
value: 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, className, value, style, activeStyle } = props;
const { min, max, direction, included, includedStart, includedEnd } =
React.useContext(SliderContext);

Expand All @@ -35,7 +36,7 @@ export default function Dot(props: DotProps) {
<span
className={classNames(dotClassName, {
[`${dotClassName}-active`]: active,
})}
}, className)}
style={mergedStyle}
/>
);
Expand Down
37 changes: 26 additions & 11 deletions src/Steps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import type { InternalMarkObj } from '../Marks';
import SliderContext from '../context';
import Dot from './Dot';
import classNames from 'classnames';

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

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

// Add marks
marks.forEach((mark) => {
dotSet.add(mark.value);
});

// Set marksValue
const uniqueMarksValue = Array.from(dotSet);

// Fill dots
if (dots && step !== null) {
let current = min;
Expand All @@ -32,20 +36,31 @@ export default function Steps(props: StepsProps) {
}
}

return Array.from(dotSet);
return {
marksValue: uniqueMarksValue,
stepDots: Array.from(dotSet),
};
}, [min, max, step, dots, marks]);

return (
<div className={`${prefixCls}-step`}>
{stepDots.map((dotValue) => (
<Dot
prefixCls={prefixCls}
key={dotValue}
value={dotValue}
style={style}
activeStyle={activeStyle}
/>
))}
{stepDots.map((dotValue) => {
// Check whether it is a marks dot
const isMarksDot = marksValue.indexOf(dotValue) >= 0;

return (
<Dot
prefixCls={prefixCls}
className={classNames({
[`${prefixCls}-marks-dot`]: isMarksDot,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[`${prefixCls}-marks-dot`]: isMarksDot,
+markedDot,

@MadCcc An additional?

Copy link
Member

@MadCcc MadCcc Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, current design do not need a className to style dot with mark. It's better to append customized className to this element.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll write it down when I have time.

})}
key={dotValue}
value={dotValue}
style={style}
activeStyle={activeStyle}
/>
);
})}
</div>
);
}
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