From d1a8d0d8c3b3b59bbf9d9168748b1181b693c39a Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 14 Dec 2017 12:13:15 +0800 Subject: [PATCH] fix --- components/_util/cloneElement.js | 5 ++-- components/_util/vnode.js | 46 ++++++++++++++++++++++++++++++++ components/align/Align.vue | 5 ++-- components/rate/Rate.vue | 3 ++- components/rate/Star.vue | 4 +-- 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 components/_util/vnode.js diff --git a/components/_util/cloneElement.js b/components/_util/cloneElement.js index 0756cb788..ec8b55c1f 100644 --- a/components/_util/cloneElement.js +++ b/components/_util/cloneElement.js @@ -1,7 +1,8 @@ -export default (node, props) => { +export default (node, nodeProps) => { + const { props, style, class: cls, attrs, key } = nodeProps if (node.componentOptions) { const propsData = node.componentOptions.propsData - Object.assign(propsData, props) + Object.assign(propsData, nodeProps) } return node } diff --git a/components/_util/vnode.js b/components/_util/vnode.js new file mode 100644 index 000000000..d6cb01df9 --- /dev/null +++ b/components/_util/vnode.js @@ -0,0 +1,46 @@ +export function cloneVNode (vnode, deep) { + const cloned = new vnode.constructor( + vnode.tag, + vnode.data, + vnode.children, + vnode.text, + vnode.elm, + vnode.context, + vnode.componentOptions, + vnode.asyncFactory + ) + cloned.ns = vnode.ns + cloned.isStatic = vnode.isStatic + cloned.key = vnode.key + cloned.isComment = vnode.isComment + cloned.isCloned = true + if (deep && vnode.children) { + cloned.children = cloneVNodes(vnode.children) + } + return cloned +} + +export function cloneVNodes (vnodes, deep) { + const len = vnodes.length + const res = new Array(len) + for (let i = 0; i < len; i++) { + res[i] = cloneVNode(vnodes[i], deep) + } + return res +} + +export function cloneElement (node, nodeProps) { + const { props, key } = nodeProps + if (node.componentOptions) { + Object.assign(node.componentOptions.propsData, props) + } + if (node.data) { + const data = node.data + const { style = data.style, class: cls = data.class, attrs = data.attrs } = nodeProps + Object.assign(node.data, { style, attrs, class: cls }) + } + if (key !== undefined) { + node.key = key + } + return node +} diff --git a/components/align/Align.vue b/components/align/Align.vue index 79466e570..ed63a80f6 100644 --- a/components/align/Align.vue +++ b/components/align/Align.vue @@ -2,7 +2,7 @@ import PropTypes from 'vue-types' import align from 'dom-align' import addEventListener from '../_util/Dom/addEventListener' -import cloneElement from '../_util/cloneElement' +import { cloneElement } from '../_util/vnode.js' import isWindow from './isWindow' function noop () { } @@ -106,7 +106,6 @@ export default { }, render () { - console.log(4) const { childrenProps } = this.$props const child = this.$slots.default[0] if (childrenProps) { @@ -116,7 +115,7 @@ export default { newProps[prop] = this.props[childrenProps[prop]] } } - return cloneElement(child, newProps) + return cloneElement(child, { props: newProps }) } return child }, diff --git a/components/rate/Rate.vue b/components/rate/Rate.vue index 927d2d98a..11494c2e1 100644 --- a/components/rate/Rate.vue +++ b/components/rate/Rate.vue @@ -2,6 +2,7 @@ import Star from './Star.vue' import Icon from '../icon' import { getOffsetLeft, deepClone } from './util' +import { cloneVNodes } from '../_util/vnode' export default { name: 'Rate', @@ -116,7 +117,7 @@ export default { onClick={this.onClick} onHover={this.onHover} key={i}> - {(this.hasDefaultSlot) ? (deepClone(this.$slots.default, createElement)) : this.character} + {(this.hasDefaultSlot) ? (cloneVNodes(this.$slots.default, true)) : this.character} ) }) diff --git a/components/rate/Star.vue b/components/rate/Star.vue index 28ea4c949..021eb85f2 100644 --- a/components/rate/Star.vue +++ b/components/rate/Star.vue @@ -1,5 +1,5 @@