[new feature] Slider: add drag-start、drag-end event

This commit is contained in:
陈嘉涵 2019-05-27 20:34:11 +08:00
parent b02773a455
commit d2a6a692c5
5 changed files with 47 additions and 8 deletions

View File

@ -20,6 +20,10 @@
- 支持`reset`方法传入`code`参数 - 支持`reset`方法传入`code`参数
##### Button
- 新增`icon`属性
##### ImagePreview ##### ImagePreview
- 新增`close-on-popstate`属性 - 新增`close-on-popstate`属性
@ -30,6 +34,11 @@
- 新增`right-icon`属性 - 新增`right-icon`属性
- 新增`right-icon`插槽 - 新增`right-icon`插槽
##### Slider
- 新增`drag-start`事件
- 新增`drag-end`事件
### [v2.0.0-beta.1](https://github.com/youzan/vant/tree/v2.0.0-beta.1) ### [v2.0.0-beta.1](https://github.com/youzan/vant/tree/v2.0.0-beta.1)

View File

@ -105,6 +105,8 @@ export default {
| Event | Description | Arguments | | Event | Description | Arguments |
|------|------|------| |------|------|------|
| change | Triggered after value change | value: current rate | | change | Triggered after value change | value: current rate |
| drag-start | Triggered when start drag | - |
| drag-end | Triggered when end drag | - |
### Slots ### Slots

View File

@ -1,4 +1,4 @@
import { use } from '../utils'; import { use, isDef } from '../utils';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { preventDefault } from '../utils/event'; import { preventDefault } from '../utils/event';
@ -30,17 +30,26 @@ export default sfc({
methods: { methods: {
onTouchStart(event) { onTouchStart(event) {
if (this.disabled) return; if (this.disabled) {
return;
}
this.touchStart(event); this.touchStart(event);
this.startValue = this.format(this.value); this.startValue = this.format(this.value);
this.dragStatus = 'start';
}, },
onTouchMove(event) { onTouchMove(event) {
if (this.disabled) {
return;
}
if (this.dragStatus === 'start') {
this.dragStatus = 'draging';
this.$emit('drag-start');
}
preventDefault(event, true); preventDefault(event, true);
if (this.disabled) return;
this.touchMove(event); this.touchMove(event);
const rect = this.$el.getBoundingClientRect(); const rect = this.$el.getBoundingClientRect();
@ -53,8 +62,18 @@ export default sfc({
}, },
onTouchEnd() { onTouchEnd() {
if (this.disabled) return; if (this.disabled) {
this.updateValue(this.newValue, true); return;
}
if (isDef(this.newValue)) {
this.updateValue(this.newValue, true);
}
if (this.dragStatus === 'draging') {
this.$emit('drag-end');
this.dragStatus = '';
}
}, },
onClick(event) { onClick(event) {

View File

@ -1,5 +1,5 @@
import Slider from '..'; import Slider from '..';
import { mount, triggerDrag, trigger } from '../../../test/utils'; import { mount, trigger, triggerDrag } from '../../../test/utils';
function mockGetBoundingClientRect(vertical) { function mockGetBoundingClientRect(vertical) {
Element.prototype.getBoundingClientRect = jest.fn(() => ({ Element.prototype.getBoundingClientRect = jest.fn(() => ({
@ -24,13 +24,20 @@ test('drag button', () => {
wrapper.setProps({ value }); wrapper.setProps({ value });
}); });
const button = wrapper.find('.van-slider__button'); const button = wrapper.find('.van-slider__button');
triggerDrag(button, 50, 0); triggerDrag(button, 50, 0);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
expect(wrapper.emitted('drag-start')).toBeFalsy();
expect(wrapper.emitted('drag-end')).toBeFalsy();
wrapper.setData({ disabled: false }); wrapper.setData({ disabled: false });
trigger(button, 'touchstart', 0, 0);
trigger(button, 'touchend', 0, 0);
triggerDrag(button, 50, 0); triggerDrag(button, 50, 0);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
expect(wrapper.emitted('drag-start')).toBeTruthy();
expect(wrapper.emitted('drag-end')).toBeTruthy();
}); });
it('click bar', () => { it('click bar', () => {

View File

@ -107,6 +107,8 @@ Slider 垂直展示时,高度为 100% 父元素高度
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
|------|------|------| |------|------|------|
| change | 进度值改变后触发 | value: 当前进度 | | change | 进度值改变后触发 | value: 当前进度 |
| drag-start | 开始拖动时触发 | - |
| drag-end | 结束拖动时触发 | - |
### Slots ### Slots