Skip to content

className no longer merges if it is falsely #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/conditionalClassMerge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @flow

import {
binaryExpression,
conditionalExpression,
stringLiteral
} from 'babel-types';

export default (
classNameExpression: any,
styleNameExpression: any,
): any => {
return binaryExpression(
'+',
conditionalExpression(
classNameExpression,
binaryExpression(
'+',
classNameExpression,
stringLiteral(' ')
),
stringLiteral('')
),
styleNameExpression
);
};
14 changes: 4 additions & 10 deletions src/replaceJsxExpressionContainer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @flow

import BabelTypes, {
binaryExpression,
Identifier,
Expand All @@ -8,9 +7,9 @@ import BabelTypes, {
jSXAttribute,
JSXAttribute,
jSXExpressionContainer,
jSXIdentifier,
stringLiteral
jSXIdentifier
} from 'babel-types';
import conditionalClassMerge from './conditionalClassMerge';

export default (
t: BabelTypes,
Expand Down Expand Up @@ -55,14 +54,9 @@ export default (
path.node.openingElement.attributes.push(jSXAttribute(
jSXIdentifier('className'),
jSXExpressionContainer(
binaryExpression(
'+',
conditionalClassMerge(
classNameAttribute.value.expression,
binaryExpression(
'+',
stringLiteral(' '),
styleNameExpression
)
styleNameExpression
)
)
));
Expand Down
7 changes: 3 additions & 4 deletions src/resolveStringLiteral.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow

import {
binaryExpression,
isJSXExpressionContainer,
isStringLiteral,
JSXAttribute,
stringLiteral
} from 'babel-types';
import conditionalClassMerge from './conditionalClassMerge';
import getClassName from './getClassName';
import type {
StyleModuleImportMapType
Expand All @@ -27,10 +27,9 @@ export default (path: Object, styleModuleImportMap: StyleModuleImportMapType, st
if (isStringLiteral(classNameAttribute.value)) {
classNameAttribute.value.value += ' ' + resolvedStyleName;
} else if (isJSXExpressionContainer(classNameAttribute.value)) {
classNameAttribute.value.expression = binaryExpression(
'+',
classNameAttribute.value.expression = conditionalClassMerge(
classNameAttribute.value.expression,
stringLiteral(' ' + resolvedStyleName)
stringLiteral(resolvedStyleName)
);
} else {
throw new Error('Unexpected attribute value.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const _styleModuleImportMap = {
};
<div className="apple banana bar__a"></div>;

<div className={this.props.className + ' bar__a'}></div>;
<div className={(this.props.className ? this.props.className + ' ' : '') + 'bar__a'}></div>;

<div className={(Math.random() > 0.5 ? 'apple' : 'banana') + ' bar__a'}></div>;
<div className={((Math.random() > 0.5 ? 'apple' : 'banana') ? (Math.random() > 0.5 ? 'apple' : 'banana') + ' ' : '') + 'bar__a'}></div>;

<div className={this.props.className + (' ' + _getClassName(foo, _styleModuleImportMap))}></div>;
<div className={(this.props.className ? this.props.className + ' ' : '') + _getClassName(foo, _styleModuleImportMap)}></div>;