Skip to content

Commit b50dbed

Browse files
Allow callback for "dotStyle" and "activeDotStyle" props - with tests (#830)
* feat: callback for "dotStyle" and "activeDotStyle" * fix: test * fix: style lookup in test * fix: style * fix: prettier * fix: fn argument * fix: test style values * fix: offset in second test
1 parent 410717a commit b50dbed

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ The following APIs are shared by Slider and Range.
9797
| handleStyle | Array[Object] \| Object | `[{}]` | The style used for handle. (`both for slider(`Object`) and range(`Array of Object`), the array will be used for multi handle following element order`) |
9898
| trackStyle | Array[Object] \| Object | `[{}]` | The style used for track. (`both for slider(`Object`) and range(`Array of Object`), the array will be used for multi track following element order`)|
9999
| railStyle | Object | `{}` | The style used for the track base color. |
100-
| dotStyle | Object | `{}` | The style used for the dots. |
101-
| activeDotStyle | Object | `{}` | The style used for the active dots. |
100+
| dotStyle | Object \| (dotValue) => Object | `{}` | The style used for the dots. |
101+
| activeDotStyle | Object \| (dotValue) => Object | `{}` | The style used for the active dots. |
102102

103103
### Slider
104104

src/Slider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export interface SliderProps<ValueType = number | number[]> {
7272
trackStyle?: React.CSSProperties | React.CSSProperties[];
7373
handleStyle?: React.CSSProperties | React.CSSProperties[];
7474
railStyle?: React.CSSProperties;
75-
dotStyle?: React.CSSProperties;
76-
activeDotStyle?: React.CSSProperties;
75+
dotStyle?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
76+
activeDotStyle?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
7777

7878
// Decorations
7979
marks?: Record<string | number, React.ReactNode | MarkObj>;

src/Steps/Dot.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import SliderContext from '../context';
66
export interface DotProps {
77
prefixCls: string;
88
value: number;
9-
style?: React.CSSProperties;
10-
activeStyle?: React.CSSProperties;
9+
style?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
10+
activeStyle?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
1111
}
1212

1313
export default function Dot(props: DotProps) {
@@ -21,13 +21,13 @@ export default function Dot(props: DotProps) {
2121
// ============================ Offset ============================
2222
let mergedStyle = {
2323
...getDirectionStyle(direction, value, min, max),
24-
...style,
24+
...(typeof style === 'function' ? style(value) : style),
2525
};
2626

2727
if (active) {
2828
mergedStyle = {
2929
...mergedStyle,
30-
...activeStyle,
30+
...(typeof activeStyle === 'function' ? activeStyle(value) : activeStyle),
3131
};
3232
}
3333

src/Steps/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface StepsProps {
77
prefixCls: string;
88
marks: InternalMarkObj[];
99
dots?: boolean;
10-
style?: React.CSSProperties;
11-
activeStyle?: React.CSSProperties;
10+
style?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
11+
activeStyle?: React.CSSProperties | ((dotValue: number) => React.CSSProperties);
1212
}
1313

1414
export default function Steps(props: StepsProps) {

tests/common.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ describe('Common', () => {
5151
expect(container2.getElementsByClassName('rc-slider-dot-active')).toHaveLength(4);
5252
});
5353

54+
it('should render dots correctly when dotStyle is dynamic`', () => {
55+
const { container: container1 } = render(
56+
<Slider value={50} step={10} dots dotStyle={(dotValue) => ({ width: `${dotValue}px` })} />,
57+
);
58+
expect(container1.getElementsByClassName('rc-slider-dot')[1]).toHaveStyle(
59+
'left: 10%; transform: translateX(-50%); width: 10px',
60+
);
61+
expect(container1.getElementsByClassName('rc-slider-dot')[2]).toHaveStyle(
62+
'left: 20%; transform: translateX(-50%); width: 20px',
63+
);
64+
65+
const { container: container2 } = render(
66+
<Slider
67+
range
68+
value={[20, 50]}
69+
step={10}
70+
dots
71+
activeDotStyle={(dotValue) => ({ width: `${dotValue}px` })}
72+
/>,
73+
);
74+
expect(container2.getElementsByClassName('rc-slider-dot-active')[1]).toHaveStyle(
75+
'left: 30%; transform: translateX(-50%); width: 30px',
76+
);
77+
expect(container2.getElementsByClassName('rc-slider-dot-active')[2]).toHaveStyle(
78+
'left: 40%; transform: translateX(-50%); width: 40px',
79+
);
80+
});
81+
5482
it('should not set value greater than `max` or smaller `min`', () => {
5583
const { container: container1 } = render(<Slider value={0} min={10} />);
5684
expect(

0 commit comments

Comments
 (0)