From df5db65397941150ad9ba4a7f04cbaf95940231c Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Mon, 17 Sep 2018 10:20:23 +0800 Subject: [PATCH] update wave & tag & tag snapshots --- components/_util/wave.jsx | 60 ++++++++++------ .../__snapshots__/RangePicker.test.js.snap | 2 +- components/locale-provider/index.jsx | 2 +- .../__tests__/__snapshots__/demo.test.js.snap | 12 ++-- components/tag/Tag.jsx | 29 ++++---- .../__tests__/__snapshots__/demo.test.js.snap | 50 ++++++++------ .../__snapshots__/index.test.js.snap | 13 ++++ components/tag/__tests__/index.test.js | 69 +++++++++++++------ components/tag/demo/control.md | 4 +- components/tag/demo/controlled.md | 2 +- components/tag/index.en-US.md | 2 +- components/tag/index.zh-CN.md | 2 +- 12 files changed, 160 insertions(+), 87 deletions(-) create mode 100644 components/tag/__tests__/__snapshots__/index.test.js.snap diff --git a/components/_util/wave.jsx b/components/_util/wave.jsx index 849b0c24e..82d4c733f 100644 --- a/components/_util/wave.jsx +++ b/components/_util/wave.jsx @@ -24,22 +24,18 @@ export default { return true }, - onClick (node) { + onClick (node, waveColor) { if (node.className.indexOf('-leave') >= 0) { return } this.removeExtraStyleNode() const { insertExtraNode } = this.$props - const extraNode = document.createElement('div') + this.extraNode = document.createElement('div') + const extraNode = this.extraNode extraNode.className = 'ant-click-animating-node' - const attributeName = insertExtraNode ? 'ant-click-animating' : 'ant-click-animating-without-extra-node' + const attributeName = this.getAttributeName() node.removeAttribute(attributeName) node.setAttribute(attributeName, 'true') - // Get wave color from target - const waveColor = - getComputedStyle(node).getPropertyValue('border-top-color') || // Firefox Compatible - getComputedStyle(node).getPropertyValue('border-color') || - getComputedStyle(node).getPropertyValue('background-color') // Not white or transparnt or grey if (waveColor && waveColor !== '#ffffff' && @@ -56,20 +52,14 @@ export default { if (insertExtraNode) { node.appendChild(extraNode) } - const transitionEnd = () => { - node.removeAttribute(attributeName) - this.removeExtraStyleNode() - if (insertExtraNode) { - node.removeChild(extraNode) - } - TransitionEvents.removeEndEventListener(node, transitionEnd) - } - TransitionEvents.addEndEventListener(node, transitionEnd) + TransitionEvents.addEndEventListener(node, this.onTransitionEnd) }, bindAnimationEvent (node) { - if (node.getAttribute('disabled') || - node.className.indexOf('disabled') >= 0) { + if (!node || + !node.getAttribute || + node.getAttribute('disabled') || + node.className.indexOf('disabled') >= 0) { return } const onClick = (e) => { @@ -77,7 +67,13 @@ export default { if (e.target.tagName === 'INPUT') { return } - setTimeout(() => this.onClick(node), 0) + this.resetEffect(node) + // Get wave color from target + const waveColor = + getComputedStyle(node).getPropertyValue('border-top-color') || // Firefox Compatible + getComputedStyle(node).getPropertyValue('border-color') || + getComputedStyle(node).getPropertyValue('background-color') + this.clickWaveTimeoutId = window.setTimeout(() => this.onClick(node, waveColor), 0) } node.addEventListener('click', onClick, true) return { @@ -86,7 +82,31 @@ export default { }, } }, + getAttributeName () { + const { insertExtraNode } = this.$props + return insertExtraNode ? 'ant-click-animating' : 'ant-click-animating-without-extra-node' + }, + resetEffect (node) { + if (!node || node === this.extraNode) { + return + } + const { insertExtraNode } = this.$props + const attributeName = this.getAttributeName() + node.removeAttribute(attributeName) + this.removeExtraStyleNode() + if (insertExtraNode && this.extraNode && node.contains(this.extraNode)) { + node.removeChild(this.extraNode) + } + TransitionEvents.removeEndEventListener(node, this.onTransitionEnd) + }, + + onTransitionEnd (e) { + if (!e || e.animationName !== 'fadeEffect') { + return + } + this.resetEffect(e.target) + }, removeExtraStyleNode () { if (this.styleForPesudo && document.body.contains(this.styleForPesudo)) { document.body.removeChild(this.styleForPesudo) diff --git a/components/date-picker/__tests__/__snapshots__/RangePicker.test.js.snap b/components/date-picker/__tests__/__snapshots__/RangePicker.test.js.snap index 6baa54090..bba446b62 100644 --- a/components/date-picker/__tests__/__snapshots__/RangePicker.test.js.snap +++ b/components/date-picker/__tests__/__snapshots__/RangePicker.test.js.snap @@ -685,7 +685,7 @@ exports[`RangePicker switch to corresponding month panel when click presetted ra diff --git a/components/locale-provider/index.jsx b/components/locale-provider/index.jsx index 4d39b1a23..db804ac3a 100644 --- a/components/locale-provider/index.jsx +++ b/components/locale-provider/index.jsx @@ -66,7 +66,7 @@ export default { changeConfirmLocale() }, render () { - return this.$slots.default[0] + return this.$slots.default ? this.$slots.default[0] : null }, } diff --git a/components/table/__tests__/__snapshots__/demo.test.js.snap b/components/table/__tests__/__snapshots__/demo.test.js.snap index 7d2df0b09..1db204cbc 100644 --- a/components/table/__tests__/__snapshots__/demo.test.js.snap +++ b/components/table/__tests__/__snapshots__/demo.test.js.snap @@ -67,7 +67,7 @@ exports[`renders ./components/table/demo/basic.md correctly 1`] = ` John Brown 32 New York No. 1 Lake Park -
nice
developer
+
nice
developer
Invite 一 John Brown
Delete
More actions
@@ -76,7 +76,7 @@ exports[`renders ./components/table/demo/basic.md correctly 1`] = ` Jim Green 42 London No. 1 Lake Park -
loser
+
loser
Invite 一 Jim Green
Delete
More actions
@@ -85,7 +85,7 @@ exports[`renders ./components/table/demo/basic.md correctly 1`] = ` Joe Black 32 Sidney No. 1 Lake Park -
cool
teacher
+
cool
teacher
Invite 一 Joe Black
Delete
More actions
@@ -2517,7 +2517,7 @@ exports[`renders ./components/table/demo/template.md correctly 1`] = ` Brown 32 New York No. 1 Lake Park -
nice
developer
+
nice
developer
Action 一 John
Delete
@@ -2526,7 +2526,7 @@ exports[`renders ./components/table/demo/template.md correctly 1`] = ` Green 42 London No. 1 Lake Park -
loser
+
loser
Action 一 Jim
Delete
@@ -2535,7 +2535,7 @@ exports[`renders ./components/table/demo/template.md correctly 1`] = ` Black 32 Sidney No. 1 Lake Park -
cool
teacher
+
cool
teacher
Action 一 Joe
Delete
diff --git a/components/tag/Tag.jsx b/components/tag/Tag.jsx index 4b32be623..6bfa106a7 100644 --- a/components/tag/Tag.jsx +++ b/components/tag/Tag.jsx @@ -14,6 +14,7 @@ export default { color: PropTypes.string, closable: PropTypes.bool, visible: PropTypes.bool, + afterClose: PropTypes.func, }, model: { prop: 'visible', @@ -46,12 +47,13 @@ export default { }, updated () { this.$nextTick(() => { - if (this.pre_visible && !this.$data._visible) { + const preVisible = this.pre_visible + this.pre_visible = this.$data._visible + if (preVisible && !this.$data._visible) { this.close() - } else if (!this.pre_visible && this.$data._visible) { + } else if (!preVisible && this.$data._visible) { this.show() } - this.pre_visible = this.$data._visible }) }, methods: { @@ -62,6 +64,7 @@ export default { return } this.setState({ _visible: false }) + this.$forceUpdate() }, close () { if (this.$data._closing || this.$data._closed) { @@ -111,7 +114,7 @@ export default { render () { const { prefixCls, closable, color } = this.$props - const closeIcon = closable ? : '' + const closeIcon = closable ? : '' const isPresetColor = this.isPresetColor(color) const cls = { [`${prefixCls}`]: true, @@ -123,9 +126,10 @@ export default { const tagStyle = { backgroundColor: (color && !isPresetColor) ? color : null, } - const tag = this.$data._closed ? : ( + const tag =
- ) - const transitionProps = this.$data._closed ? {} : getTransitionProps(`${prefixCls}-zoom`, { + const transitionProps = getTransitionProps(`${prefixCls}-zoom`, { afterLeave: () => this.animationEnd(undefined, false), afterEnter: () => this.animationEnd(undefined, true), }) return ( - - {tag} - + {this.$data._closed ? + : + {tag} + + } ) }, diff --git a/components/tag/__tests__/__snapshots__/demo.test.js.snap b/components/tag/__tests__/__snapshots__/demo.test.js.snap index 4e2eeb125..ef068a82e 100644 --- a/components/tag/__tests__/__snapshots__/demo.test.js.snap +++ b/components/tag/__tests__/__snapshots__/demo.test.js.snap @@ -2,10 +2,10 @@ exports[`renders ./components/tag/demo/basic.md correctly 1`] = `
-
Tag 1
- -
Tag 2
-
Prevent Default
+
Tag 1
+ +
Tag 2
+
Prevent Default
`; @@ -21,40 +21,48 @@ exports[`renders ./components/tag/demo/colorful.md correctly 1`] = `

Presets:

-
pink
-
red
-
orange
-
green
-
cyan
-
blue
-
purple
+
pink
+
red
+
orange
+
green
+
cyan
+
blue
+
purple

Custom:

-
#f50
-
#2db7f5
-
#87d068
-
#108ee9
+
#f50
+
#2db7f5
+
#87d068
+
#108ee9
`; exports[`renders ./components/tag/demo/control.md correctly 1`] = `
-
+
Unremovable
-
+
Tag 2 -
-
+
+
Tag 3Tag 3Tag 3Tag 3... -
-
New Tag +
+
New Tag
`; +exports[`renders ./components/tag/demo/controlled.md correctly 1`] = ` +
+
+ Movies +

+
+`; + exports[`renders ./components/tag/demo/hot-tags.md correctly 1`] = `
Categories:
diff --git a/components/tag/__tests__/__snapshots__/index.test.js.snap b/components/tag/__tests__/__snapshots__/index.test.js.snap new file mode 100644 index 000000000..667a1aa8e --- /dev/null +++ b/components/tag/__tests__/__snapshots__/index.test.js.snap @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Tag visibility can be controlled by visible with hidden as initial value 1`] = ``; + +exports[`Tag visibility can be controlled by visible with hidden as initial value 2`] = ``; + +exports[`Tag visibility can be controlled by visible with hidden as initial value 3`] = ``; + +exports[`Tag visibility can be controlled by visible with visible as initial value 1`] = `
`; + +exports[`Tag visibility can be controlled by visible with visible as initial value 2`] = `
`; + +exports[`Tag visibility can be controlled by visible with visible as initial value 3`] = `
`; diff --git a/components/tag/__tests__/index.test.js b/components/tag/__tests__/index.test.js index 9420ddeb7..eb68438d9 100644 --- a/components/tag/__tests__/index.test.js +++ b/components/tag/__tests__/index.test.js @@ -3,30 +3,25 @@ import { asyncExpect } from '@/tests/utils' import Tag from '..' describe('Tag', () => { - beforeAll(() => { - jest.useFakeTimers() - }) - - afterAll(() => { - jest.useRealTimers() - }) - - it('should be closable', () => { + it('should be closable', async () => { const onClose = jest.fn() const wrapper = mount({ render () { return }, + }, { sync: false, attachToDocument: true }) + await asyncExpect(() => { + expect(wrapper.findAll('.anticon-close').length).toBe(1) + expect(wrapper.findAll('.ant-tag').length).toBe(1) + wrapper.find('.anticon-close').trigger('click') + expect(onClose).toBeCalled() }) - expect(wrapper.findAll('.anticon-cross').length).toBe(1) - expect(wrapper.findAll('.ant-tag').length).toBe(1) - wrapper.find('.anticon-cross').trigger('click') - expect(onClose).toBeCalled() - jest.runAllTimers() - expect(wrapper.findAll('.ant-tag').length).toBe(0) + // await asyncExpect(() => { + // expect(wrapper.findAll('.ant-tag').length).toBe(0) + // }) }) - it('should not be closed when prevent default', () => { + it('should not be closed when prevent default', async () => { const onClose = (e) => { e.preventDefault() } @@ -34,11 +29,43 @@ describe('Tag', () => { render () { return }, + }, { sync: false }) + await asyncExpect(() => { + expect(wrapper.findAll('.anticon-close').length).toBe(1) + expect(wrapper.findAll('.ant-tag').length).toBe(1) + wrapper.find('.anticon-close').trigger('click') + }) + await asyncExpect(() => { + expect(wrapper.findAll('.ant-tag').length).toBe(1) + }, 0) + }) + describe('visibility', () => { + it('can be controlled by visible with visible as initial value', async () => { + const wrapper = mount(Tag, { propsData: { visible: true }, sync: false }) + await asyncExpect(() => { + expect(wrapper.html()).toMatchSnapshot() + wrapper.setProps({ visible: false }) + }) + await asyncExpect(() => { + expect(wrapper.html()).toMatchSnapshot() + wrapper.setProps({ visible: true }) + }) + await asyncExpect(() => { + expect(wrapper.html()).toMatchSnapshot() + }) + }) + + it('can be controlled by visible with hidden as initial value', async () => { + const wrapper = mount(Tag, { propsData: { visible: false }, sync: false }) + await asyncExpect(() => { + expect(wrapper.html()).toMatchSnapshot() + wrapper.setProps({ visible: true }) + }) + await asyncExpect(() => { + expect(wrapper.html()).toMatchSnapshot() + wrapper.setProps({ visible: false }) + }) + await asyncExpect(() => { expect(wrapper.html()).toMatchSnapshot() }) }) - expect(wrapper.findAll('.anticon-cross').length).toBe(1) - expect(wrapper.findAll('.ant-tag').length).toBe(1) - wrapper.find('.anticon-cross').trigger('click') - jest.runAllTimers() - expect(wrapper.findAll('.ant-tag').length).toBe(1) }) }) diff --git a/components/tag/demo/control.md b/components/tag/demo/control.md index 2c04ef110..61953cbab 100644 --- a/components/tag/demo/control.md +++ b/components/tag/demo/control.md @@ -14,11 +14,11 @@ It's based on `afterClose` event, which will be triggered while the close animat
diff --git a/components/tag/demo/controlled.md b/components/tag/demo/controlled.md index 029d48532..744450360 100644 --- a/components/tag/demo/controlled.md +++ b/components/tag/demo/controlled.md @@ -13,7 +13,7 @@ By using the `visible` prop, you can control the close state of Tag.
Movies diff --git a/components/tag/index.en-US.md b/components/tag/index.en-US.md index fec89ff0c..1c3e56974 100644 --- a/components/tag/index.en-US.md +++ b/components/tag/index.en-US.md @@ -9,7 +9,7 @@ | afterClose | Callback executed when close animation is completed | () => void | - | | closable | Whether the Tag can be closed | boolean | `false` | | color | Color of the Tag | string | - | -| visible | Whether the Tag is closed or not | boolean | `true` | +| visible(v-model) | Whether the Tag is closed or not | boolean | `true` | ### Tag Events diff --git a/components/tag/index.zh-CN.md b/components/tag/index.zh-CN.md index a6957fd8a..d1d7cd014 100644 --- a/components/tag/index.zh-CN.md +++ b/components/tag/index.zh-CN.md @@ -9,7 +9,7 @@ | afterClose | 关闭动画完成后的回调 | () => void | - | | closable | 标签是否可以关闭 | boolean | false | | color | 标签色 | string | - | -| visible | 是否显示标签 | boolean | `true` | +| visible(v-model) | 是否显示标签 | boolean | `true` | ### 事件 | 事件名称 | 说明 | 回调参数 |