Skip to content

Commit 86a8a7a

Browse files
authored
feat: deprecate onAfterChange for onChangeComplete (#953)
1 parent 5d70e74 commit 86a8a7a

File tree

9 files changed

+48
-18
lines changed

9 files changed

+48
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ The following APIs are shared by Slider and Range.
112112
| dots | boolean | `false` | When the `step` value is greater than 1, you can set the `dots` to `true` if you want to render the slider with dots. |
113113
| onBeforeChange | Function | NOOP | `onBeforeChange` will be triggered when `ontouchstart` or `onmousedown` is triggered. |
114114
| onChange | Function | NOOP | `onChange` will be triggered while the value of Slider changing. |
115-
| onAfterChange | Function | NOOP | `onAfterChange` will be triggered when `ontouchend` or `onmouseup` is triggered. |
115+
| onChangeComplete | Function | NOOP | `onChangeComplete` will be triggered when `ontouchend` or `onmouseup` is triggered. |
116116
| minimumTrackStyle | Object | | please use `trackStyle` instead. (`only used for slider, just for compatibility , will be deprecate at [email protected] `) |
117117
| maximumTrackStyle | Object | | please use `railStyle` instead (`only used for slider, just for compatibility , will be deprecate at [email protected]`) |
118118
| 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`) |

docs/examples/debug.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default () => {
5252
console.log('Change:', nextValues);
5353
// setValue(nextValues as any);
5454
}}
55-
onAfterChange={(v) => {
55+
onChangeComplete={(v) => {
5656
console.log('AfterChange:', v);
5757
}}
5858
// value={value}

docs/examples/marks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default () => (
2525
<div>
2626
<div style={style}>
2727
<p>Slider with marks, `step=null`</p>
28-
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} onAfterChange={(v) => console.log('AfterChange:', v)} />
28+
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} onChangeComplete={(v) => console.log('AfterChange:', v)} />
2929
</div>
3030

3131
<div style={style}>

docs/examples/range.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export default () => (
194194
</div>
195195
<div style={style}>
196196
<p>Basic Range,`step=20, dots` </p>
197-
<Slider range dots step={20} defaultValue={[20, 40]} onAfterChange={log} />
197+
<Slider range dots step={20} defaultValue={[20, 40]} onChangeComplete={log} />
198198
</div>
199199
<div style={style}>
200200
<p>Basic Range,disabled</p>

docs/examples/slider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class NullableSlider extends React.Component<any, any> {
4646
<Slider
4747
value={this.state.value}
4848
onChange={this.onSliderChange}
49-
onAfterChange={this.onAfterChange}
49+
onChangeComplete={this.onAfterChange}
5050
/>
5151
<button type="button" onClick={this.reset}>
5252
Reset
@@ -93,7 +93,7 @@ class CustomizedSlider extends React.Component<any, any> {
9393
<Slider
9494
value={this.state.value}
9595
onChange={this.onSliderChange}
96-
onAfterChange={this.onAfterChange}
96+
onChangeComplete={this.onAfterChange}
9797
/>
9898
);
9999
}
@@ -199,7 +199,7 @@ export default () => (
199199
</div>
200200
<div style={style}>
201201
<p>Basic Slider,`step=20, dots`</p>
202-
<Slider dots step={20} defaultValue={100} onAfterChange={log} />
202+
<Slider dots step={20} defaultValue={100} onChangeComplete={log} />
203203
</div>
204204
<div style={style}>
205205
<p>
@@ -210,7 +210,7 @@ export default () => (
210210
dots
211211
step={20}
212212
defaultValue={100}
213-
onAfterChange={log}
213+
onChangeComplete={log}
214214
dotStyle={{ borderColor: 'orange' }}
215215
activeDotStyle={{ borderColor: 'yellow' }}
216216
/>

src/Slider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ export interface SliderProps<ValueType = number | number[]> {
6161
onChange?: (value: ValueType) => void;
6262
/** @deprecated It's always better to use `onChange` instead */
6363
onBeforeChange?: (value: ValueType) => void;
64-
/** @deprecated It's always better to use `onChange` instead */
64+
/** @deprecated Use `onChangeComplete` instead */
6565
onAfterChange?: (value: ValueType) => void;
66+
onChangeComplete?: (value: ValueType) => void;
6667

6768
// Cross
6869
allowCross?: boolean;
@@ -131,6 +132,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
131132
onChange,
132133
onBeforeChange,
133134
onAfterChange,
135+
onChangeComplete,
134136

135137
// Cross
136138
allowCross = true,
@@ -290,6 +292,8 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
290292

291293
const finishChange = () => {
292294
onAfterChange?.(getTriggerValue(rawValuesRef.current));
295+
warning(!onAfterChange, '[rc-slider] `onAfterChange` is deprecated. Please use `onChangeComplete` instead.');
296+
onChangeComplete?.(getTriggerValue(rawValuesRef.current));
293297
};
294298

295299
const [draggingIndex, draggingValue, cacheValues, onStartDrag] = useDrag(

tests/Range.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('Range', () => {
110110
it('it should trigger onAfterChange when key pressed', () => {
111111
const onAfterChange = jest.fn();
112112
const { container } = render(
113-
<Slider range defaultValue={[20, 50]} onAfterChange={onAfterChange} />,
113+
<Slider range defaultValue={[20, 50]} onChangeComplete={onAfterChange} />,
114114
);
115115

116116
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[1], {
@@ -128,7 +128,7 @@ describe('Range', () => {
128128
it('should not change value from keyboard events when disabled', () => {
129129
const onAfterChange = jest.fn();
130130
const { container } = render(
131-
<Slider range keyboard={false} defaultValue={[20, 50]} onAfterChange={onAfterChange} />,
131+
<Slider range keyboard={false} defaultValue={[20, 50]} onChangeComplete={onAfterChange} />,
132132
);
133133

134134
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[1], {

tests/Slider.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('Slider', () => {
138138

139139
it('it should trigger onAfterChange when key pressed', () => {
140140
const onAfterChange = jest.fn();
141-
const { container } = render(<Slider defaultValue={50} onAfterChange={onAfterChange} />);
141+
const { container } = render(<Slider defaultValue={50} onChangeComplete={onAfterChange} />);
142142

143143
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
144144
keyCode: keyCode.RIGHT,
@@ -189,7 +189,7 @@ describe('Slider', () => {
189189
it('decreases the value when key "left" is pressed', () => {
190190
const onChange = jest.fn();
191191
const onChangeComplete = jest.fn();
192-
const { container } = render(<Slider defaultValue={50} onChange={onChange} onAfterChange={onChangeComplete} />);
192+
const { container } = render(<Slider defaultValue={50} onChange={onChange} onChangeComplete={onChangeComplete} />);
193193

194194
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
195195
keyCode: keyCode.LEFT,
@@ -560,7 +560,7 @@ describe('Slider', () => {
560560
<Slider
561561
onBeforeChange={onBeforeChange}
562562
onChange={onChange}
563-
onAfterChange={onAfterChange}
563+
onChangeComplete={onAfterChange}
564564
/>,
565565
);
566566
fireEvent.mouseDown(container.querySelector('.rc-slider'), {
@@ -621,7 +621,7 @@ describe('Slider', () => {
621621

622622
it('onAfterChange should return number', () => {
623623
const onAfterChange = jest.fn();
624-
const { container } = render(<Slider onAfterChange={onAfterChange} />);
624+
const { container } = render(<Slider onChangeComplete={onAfterChange} />);
625625
fireEvent.mouseDown(container.querySelector('.rc-slider'), {
626626
clientX: 20,
627627
});

tests/common.test.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ describe('Common', () => {
296296
value={0}
297297
marks={marks}
298298
onChange={sliderOnChange}
299-
onAfterChange={sliderOnAfterChange}
299+
onChangeComplete={sliderOnAfterChange}
300300
/>,
301301
);
302302
const sliderHandleWrapper = container.querySelector(`#${labelId}`);
@@ -311,7 +311,7 @@ describe('Common', () => {
311311

312312
const rangeOnAfterChange = jest.fn();
313313
const { container: container2 } = render(
314-
<Slider range value={[0, 1]} marks={marks} onAfterChange={rangeOnAfterChange} />,
314+
<Slider range value={[0, 1]} marks={marks} onChangeComplete={rangeOnAfterChange} />,
315315
);
316316
const rangeHandleWrapper = container2.querySelector(`#${labelId}`);
317317
fireEvent.click(rangeHandleWrapper);
@@ -325,7 +325,7 @@ describe('Common', () => {
325325
const sliderOnChange = jest.fn();
326326
const sliderOnAfterChange = jest.fn();
327327
const { container } = render(
328-
<Slider value={0} onChange={sliderOnChange} onAfterChange={sliderOnAfterChange} />,
328+
<Slider value={0} onChange={sliderOnChange} onChangeComplete={sliderOnAfterChange} />,
329329
);
330330

331331
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
@@ -342,6 +342,32 @@ describe('Common', () => {
342342
expect(sliderOnAfterChange).toHaveBeenCalledTimes(1);
343343
});
344344

345+
it('deprecate onAfterChange', () => {
346+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
347+
const onChangeComplete = jest.fn();
348+
const onAfterChange = jest.fn();
349+
const { container } = render(
350+
<Slider value={0} onChangeComplete={onChangeComplete} onAfterChange={onAfterChange} />,
351+
);
352+
353+
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
354+
keyCode: KeyCode.UP,
355+
});
356+
357+
expect(onChangeComplete).not.toHaveBeenCalled();
358+
expect(onAfterChange).not.toHaveBeenCalled();
359+
360+
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
361+
keyCode: KeyCode.UP,
362+
});
363+
expect(onChangeComplete).toHaveBeenCalledTimes(1);
364+
expect(onAfterChange).toHaveBeenCalledTimes(1);
365+
expect(errSpy).toHaveBeenCalledWith(
366+
'Warning: [rc-slider] `onAfterChange` is deprecated. Please use `onChangeComplete` instead.',
367+
);
368+
errSpy.mockRestore();
369+
})
370+
345371
// Move to antd instead
346372
// it('the tooltip should be attach to the container with the id tooltip', () => {
347373
// const SliderWithTooltip = createSliderWithTooltip(Slider);

0 commit comments

Comments
 (0)