diff --git a/docs/markdown/changelog.zh-CN.md b/docs/markdown/changelog.zh-CN.md index 6ae2b78da..cdb578042 100644 --- a/docs/markdown/changelog.zh-CN.md +++ b/docs/markdown/changelog.zh-CN.md @@ -7,6 +7,10 @@ - 新增`gutter`属性 - 支持`String`类型的`size`属性 +##### SwipeCell + +- 支持自动计算`left-width`和`right-width` + ##### Toast - 新增`onOpened`属性 diff --git a/packages/swipe-cell/demo/index.vue b/packages/swipe-cell/demo/index.vue index 54b9eb0ca..27f07c5b0 100644 --- a/packages/swipe-cell/demo/index.vue +++ b/packages/swipe-cell/demo/index.vue @@ -3,10 +3,7 @@ {{ $t('tips') }} - + - + rightWidth * threshold && rightWidth > 0) { + if ( + direction === 'right' && + -offset > computedRightWidth * threshold && + computedRightWidth > 0 + ) { this.open('right'); // left - } else if (direction === 'left' && offset > leftWidth * threshold && leftWidth > 0) { + } else if ( + direction === 'left' && + offset > computedLeftWidth * threshold && + computedLeftWidth > 0 + ) { this.open('left'); // reset } else { @@ -145,17 +177,17 @@ export default sfc({ this.swiping = false; }} > - {this.leftWidth ? ( -
+ {this.slots('left') && ( +
{this.slots('left')}
- ) : null} + )} {this.slots()} - {this.rightWidth ? ( -
+ {this.slots('right') && ( +
{this.slots('right')}
- ) : null} + )}
); diff --git a/packages/swipe-cell/test/__snapshots__/index.spec.js.snap b/packages/swipe-cell/test/__snapshots__/index.spec.js.snap index c8120c2fd..cd219a7f9 100644 --- a/packages/swipe-cell/test/__snapshots__/index.spec.js.snap +++ b/packages/swipe-cell/test/__snapshots__/index.spec.js.snap @@ -1,10 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`auto calc width 1`] = ` +
+
+
Left
+
Right
+
+
+`; + exports[`drag and show left part 1`] = `
-
-
+
Left
+
Right
`; @@ -12,8 +21,8 @@ exports[`drag and show left part 1`] = ` exports[`drag and show left part 2`] = `
-
-
+
Left
+
Right
`; @@ -21,8 +30,8 @@ exports[`drag and show left part 2`] = ` exports[`drag and show left part 3`] = `
-
-
+
Left
+
Right
`; @@ -30,8 +39,8 @@ exports[`drag and show left part 3`] = ` exports[`drag and show left part 4`] = `
-
-
+
Left
+
Right
`; @@ -39,14 +48,8 @@ exports[`drag and show left part 4`] = ` exports[`drag and show right part 1`] = `
-
-
+
Left
+
Right
`; - -exports[`width equals zero 1`] = ` -
-
-
-`; diff --git a/packages/swipe-cell/test/index.spec.js b/packages/swipe-cell/test/index.spec.js index 3bf7fe273..5b6d956db 100644 --- a/packages/swipe-cell/test/index.spec.js +++ b/packages/swipe-cell/test/index.spec.js @@ -1,14 +1,24 @@ import SwipeCell from '..'; -import { mount, triggerDrag } from '../../../test/utils'; +import { mount, triggerDrag, later } from '../../../test/utils'; const THRESHOLD = 0.15; const defaultProps = { propsData: { leftWidth: 100, rightWidth: 100 + }, + scopedSlots: { + left: () => 'Left', + right: () => 'Right' } }; +function mockGetBoundingClientRect(vertical) { + Element.prototype.getBoundingClientRect = jest.fn(() => ({ + width: 50 + })); +} + it('drag and show left part', () => { const wrapper = mount(SwipeCell, defaultProps); @@ -37,6 +47,7 @@ it('on close prop', () => { let instance; const wrapper = mount(SwipeCell, { + ...defaultProps, propsData: { ...defaultProps.propsData, onClose(pos, ins) { @@ -45,6 +56,7 @@ it('on close prop', () => { } } }); + wrapper.trigger('click'); expect(position).toEqual(undefined); @@ -66,16 +78,6 @@ it('on close prop', () => { expect(wrapper.vm.offset).toEqual(0); }); -it('width equals zero', () => { - const wrapper = mount(SwipeCell, { - propsData: { - leftWidth: 0, - rightWidth: 0 - } - }); - expect(wrapper).toMatchSnapshot(); -}); - it('should reset after drag', () => { const wrapper = mount(SwipeCell, defaultProps); @@ -94,3 +96,15 @@ it('disabled prop', () => { triggerDrag(wrapper, 50, 0); expect(wrapper.vm.offset).toEqual(0); }); + +it('auto calc width', async () => { + mockGetBoundingClientRect(); + + const wrapper = mount(SwipeCell, { + scopedSlots: defaultProps.scopedSlots + }); + + await later(); + triggerDrag(wrapper, 100, 0); + expect(wrapper).toMatchSnapshot(); +}); diff --git a/packages/swipe-cell/zh-CN.md b/packages/swipe-cell/zh-CN.md index 7a4e483b7..93bd7faef 100644 --- a/packages/swipe-cell/zh-CN.md +++ b/packages/swipe-cell/zh-CN.md @@ -87,10 +87,10 @@ export default { | 参数 | 说明 | 类型 | 默认值 | 版本 | |------|------|------|------|------| -| left-width | 左侧滑动区域宽度 | `Number` | `0` | - | -| right-width | 右侧滑动区域宽度 | `Number` | `0` | - | | on-close | 关闭时的回调函数 | `Function` | - | - | | disabled | 是否禁用滑动 | `Boolean` | `false` | 1.3.4 | +| left-width | 指定左侧滑动区域宽度 | `Number` | `auto` | - | +| right-width | 指定右侧滑动区域宽度 | `Number` | `auto` | - | ### Slots