Skip to content

Commit 1ea7b29

Browse files
committed
codemod -> replace the & .RaDatagrid-xxxx key of the sx prop of the Datagrid component by & .RaDataTable-xxxx
1 parent 63ec8cf commit 1ea7b29

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

packages/ra-core/codemods/replace-Datagrid-DataTable.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,12 @@ const replaceDatagrid = (root, j) => {
8080

8181
// Replace Datagrid with DataTable
8282
datagridComponents.replaceWith(({ node }) => {
83-
// remove the `optimized` attribute if it exists
84-
const filtredAttributes = node.openingElement.attributes.filter(
85-
attr =>
86-
!(j.JSXAttribute.check(attr) && attr.name.name === 'optimized')
87-
);
88-
89-
// rename the `rowStyle` attribute to `rowSx` if it exists
90-
const attributes = filtredAttributes.map(attr => {
91-
if (j.JSXAttribute.check(attr) && attr.name.name === 'rowStyle') {
92-
return j.jsxAttribute(j.jsxIdentifier('rowSx'), attr.value);
93-
}
94-
return attr;
95-
});
83+
const attributes = cleanAttributes(node, j);
9684

9785
const openingElement = j.jsxOpeningElement(
9886
j.jsxIdentifier('DataTable'),
9987
attributes,
100-
node.openingElement.selfClosing
88+
[node.openingElement.selfClosing]
10189
);
10290
const closingElement = j.jsxClosingElement(
10391
j.jsxIdentifier('DataTable')
@@ -108,6 +96,55 @@ const replaceDatagrid = (root, j) => {
10896
return true;
10997
};
11098

99+
const cleanAttributes = (node, j) => {
100+
// remove the `optimized` attribute if it exists
101+
const filtredAttributes = node.openingElement.attributes.filter(
102+
attr => !(j.JSXAttribute.check(attr) && attr.name.name === 'optimized')
103+
);
104+
105+
// rename the `rowStyle` attribute to `rowSx` if it exists
106+
const rowSxRenamedAttributes = filtredAttributes.map(attr => {
107+
if (j.JSXAttribute.check(attr) && attr.name.name === 'rowStyle') {
108+
return j.jsxAttribute(j.jsxIdentifier('rowSx'), attr.value);
109+
}
110+
return attr;
111+
});
112+
113+
// rename the keys of the "sx" prop from "& .RaDatagrid-xxxx" to "& .RaDataTable-xxxx"
114+
const sxRenamedAttributes = rowSxRenamedAttributes.map(attr => {
115+
if (
116+
j.JSXAttribute.check(attr) &&
117+
attr.name.name === 'sx' &&
118+
j.JSXExpressionContainer.check(attr.value)
119+
) {
120+
const expression = attr.value.expression;
121+
if (j.ObjectExpression.check(expression)) {
122+
const properties = expression.properties.map(prop => {
123+
if (
124+
j.ObjectProperty.check(prop) &&
125+
j.Literal.check(prop.key) &&
126+
typeof prop.key.value === 'string'
127+
) {
128+
const newKey = prop.key.value.replace(
129+
/RaDatagrid-/g,
130+
'RaDataTable-'
131+
);
132+
return j.objectProperty(j.literal(newKey), prop.value);
133+
}
134+
return prop;
135+
});
136+
return j.jsxAttribute(
137+
j.jsxIdentifier('sx'),
138+
j.jsxExpressionContainer(j.objectExpression(properties))
139+
);
140+
}
141+
}
142+
return attr;
143+
});
144+
145+
return sxRenamedAttributes;
146+
};
147+
111148
const wrapChildren = (root, j) => {
112149
// Find all instances of Datagrid
113150
const datagridComponents = root.find(j.JSXElement, {

0 commit comments

Comments
 (0)