Skip to content

Add support for classname resolution in jsx expressions #194

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

Closed
Closed
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
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"dependencies": {
"babel-plugin-react-css-modules": "^2.1.3",
"classnames": "^2.2.6",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"webpack": "^2.2.0-rc.3"
Expand Down
2 changes: 1 addition & 1 deletion demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
plugins: [
'transform-react-jsx',
[
'react-css-modules',
require('../dist/index.js').default,
{
context
}
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import optionsDefaults from './schemas/optionsDefaults';
import createObjectExpression from './createObjectExpression';
import requireCssModule from './requireCssModule';
import resolveStringLiteral from './resolveStringLiteral';
import resolveJSXExpression from './resolveJSXExpression';
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';

const ajv = new Ajv({
Expand Down Expand Up @@ -210,6 +211,18 @@ export default ({
}
);
} else if (t.isJSXExpressionContainer(attribute.value)) {
if(t.isCallExpression(attribute.value.expression)) {
resolveJSXExpression(
path,
filenameMap[filename].styleModuleImportMap,
attribute,
destinationName,
{
handleMissingStyleName
}
);
return;
}
if (!filenameMap[filename].importedHelperIndentifier) {
setupFileForRuntimeResolution(path, filename);
}
Expand Down
57 changes: 57 additions & 0 deletions src/resolveJSXExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @flow

import {
isJSXExpressionContainer,
isStringLiteral,
isObjectExpression,
JSXAttribute,
stringLiteral
} from 'babel-types';
import conditionalClassMerge from './conditionalClassMerge';
import getClassName from './getClassName';
import type {
StyleModuleImportMapType,
HandleMissingStyleNameOptionType
} from './types';

type OptionsType = {|
handleMissingStyleName: HandleMissingStyleNameOptionType
|};

/**
* Updates the className value of a JSX element using a provided styleName attribute.
*/
export default (
path: *,
styleModuleImportMap: StyleModuleImportMapType,
sourceAttribute: JSXAttribute,
destinationName: string,
options: OptionsType): void => {
const callExpressionArguments = sourceAttribute.value.expression.arguments;

for (let argumentIndex in callExpressionArguments) {
let argument = callExpressionArguments[argumentIndex];
if(isStringLiteral(argument)) {
callExpressionArguments[argumentIndex].value = getClassName(argument.value, styleModuleImportMap, options);
} else if(isObjectExpression(argument)) {
for (let propertyIndex in argument.properties){
let property = argument.properties[propertyIndex];
if(isStringLiteral(property.key)) {
property.key.value = getClassName(property.key.value, styleModuleImportMap, options);
}
}
}
}


const destinationAttribute = path.node.openingElement.attributes
.find((attribute) => {
return typeof attribute.name !== 'undefined' && attribute.name.name === destinationName;
});

if (destinationAttribute) {
throw new Error('Destination Attribute cannot be present on JSX Element when using JSX Expressions');
} else {
sourceAttribute.name.name = destinationName;
}
};