Skip to content

Commit

Permalink
fix: use cloneVNode when altering props in render functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Jun 5, 2023
1 parent af65683 commit 5e50eb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/runtime/components/elements/AvatarGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, computed, defineComponent } from 'vue'
import { h, cloneVNode, computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { defu } from 'defu'
import { classNames, getSlotsChildren } from '../../utils'
Expand Down Expand Up @@ -39,18 +39,20 @@ export default defineComponent({
const max = computed(() => typeof props.max === 'string' ? parseInt(props.max, 10) : props.max)

const clones = computed(() => children.value.map((node, index) => {
const vProps: any = {}

if (!props.max || (max.value && index < max.value)) {
if (props.size) {
node.props.size = props.size
vProps.size = props.size
}

node.props.class = node.props.class || ''
node.props.class += ` ${classNames(
vProps.class = node.props.class || ''
vProps.class += ` ${classNames(
ui.value.ring,
ui.value.margin
)}`

return node
return cloneVNode(node, vProps)
}

if (max.value !== undefined && index === max.value) {
Expand Down
22 changes: 12 additions & 10 deletions src/runtime/components/elements/ButtonGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, computed, defineComponent } from 'vue'
import { h, cloneVNode, computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { defu } from 'defu'
import { getSlotsChildren } from '../../utils'
Expand Down Expand Up @@ -44,28 +44,30 @@ export default defineComponent({
}[ui.value.rounded]))

const clones = computed(() => children.value.map((node, index) => {
const vProps: any = {}

if (props.size) {
node.props.size = props.size
vProps.size = props.size
}

node.props.class = node.props.class || ''
node.props.class += ' !shadow-none'
node.props.ui = node.props.ui || {}
node.props.ui.rounded = ''
vProps.class = node.props.class || ''
vProps.class += ' !shadow-none'
vProps.ui = node.props.ui || {}
vProps.ui.rounded = ''

if (index === 0) {
node.props.ui.rounded = rounded.value.left
vProps.ui.rounded = rounded.value.left
}

if (index > 0) {
node.props.class += ' -ml-px'
vProps.class += ' -ml-px'
}

if (index === children.value.length - 1) {
node.props.ui.rounded = rounded.value.right
vProps.ui.rounded = rounded.value.right
}

return node
return cloneVNode(node, vProps)
}))

return () => h('div', { class: [ui.value.wrapper, ui.value.rounded, ui.value.shadow] }, clones.value)
Expand Down
14 changes: 8 additions & 6 deletions src/runtime/components/forms/FormGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, computed, defineComponent } from 'vue'
import { h, cloneVNode, computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { defu } from 'defu'
import { getSlotsChildren } from '../../utils'
Expand Down Expand Up @@ -53,18 +53,20 @@ export default defineComponent({
const children = computed(() => getSlotsChildren(slots))

const clones = computed(() => children.value.map((node) => {
const vProps: any = {}

if (props.error) {
node.props.oldColor = node.props.color
node.props.color = 'red'
vProps.oldColor = node.props.color
vProps.color = 'red'
} else {
node.props.color = node.props.oldColor
vProps.color = vProps.oldColor
}

if (props.name) {
node.props.name = props.name
vProps.name = props.name
}

return node
return cloneVNode(node, vProps)
}))

return () => h('div', { class: [ui.value.wrapper] }, [
Expand Down

1 comment on commit 5e50eb9

@vercel
Copy link

@vercel vercel bot commented on 5e50eb9 Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxtlabs.vercel.app
ui.nuxtlabs.com
ui-nuxtlabs.vercel.app

Please sign in to comment.