Skip to content

Commit

Permalink
Merge pull request #443 from jpudysz/feature/babel
Browse files Browse the repository at this point in the history
fix: issue with useVariants and conditional children
  • Loading branch information
jpudysz authored Dec 15, 2024
2 parents da1f1a6 + 37bc297 commit 64c1887
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
61 changes: 58 additions & 3 deletions plugin/__tests__/variants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ pluginTester({
})
return (
<React.Fragment key="asd">
<Fragment key="asd">
<Text style={styles.container}>Hello world</Text>
</React.Fragment>
</Fragment>
)
}
Expand Down Expand Up @@ -761,6 +761,61 @@ pluginTester({
895830154
)
`
}
},
{
title: 'Should correctly handle complex conditions with variants',
code: `
import React from 'react'
import { Text } from 'react-native'
import { StyleSheet } from 'react-native-unistyles'
export const Example = ({ error, children, name }) => {
styles.useVariants({
error: !!error
});
return children ? (
<Text ref={ref} style={styles.label} {...props}>
{children}
</Text>
) : (
<Text ref={ref} style={styles.label} {...props}>
{name}
</Text>
)
}
const styles = StyleSheet.create(theme => ({}))
`,
output: `
import { Text } from 'react-native-unistyles/components/native/Text'
import React from 'react'
import { StyleSheet, Variants } from 'react-native-unistyles'
export const Example = ({ error, children, name }) => {
const __uni__variants = {
error: !!error
}
styles.useVariants(__uni__variants)
return children ? (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{children}
</Text>
</Variants>
) : (
<Variants variants={__uni__variants}>
<Text ref={ref} style={[styles.label]} {...props}>
{name}
</Text>
</Variants>
)
}
const styles = StyleSheet.create(theme => ({}), 895830154)
`
},
]
})
17 changes: 9 additions & 8 deletions plugin/variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ function wrapVariants(t, path) {
false
),
t.jsxClosingElement(t.jsxIdentifier('Variants')),
[path]
[]
)

if (t.isJSXFragment(path)) {
wrapperElement.children = path.children
}

// other case for React.Fragment
const element = path.openingElement && path.openingElement.name
const isWrappedInRegularFragment = t.isJSXFragment(path)
const isWrappedInFragment = (t.isJSXMemberExpression(element) && element.object.name === 'React' && element.property.name === 'Fragment') || (t.isJSXIdentifier(element) && element.name === 'Fragment')

wrapperElement.children = (isWrappedInRegularFragment || isWrappedInFragment)
? path.children
: [path]

if (t.isJSXMemberExpression(element) && element.object.name === 'React' && element.property.name === 'Fragment') {
wrapperElement.children = path.children
// copy Fragment props like key
if (isWrappedInFragment) {
wrapperElement.openingElement.attributes.push(...path.openingElement.attributes)
}

Expand Down

0 comments on commit 64c1887

Please sign in to comment.