Skip to content

Commit d70f0a4

Browse files
committed
codemod -> if exist display label instead of source
1 parent 61fb0d2 commit d70f0a4

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

packages/ra-core/codemods/__testfixtures__/replace-Datagrid-DataTable-ManyChildren.input.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const PostList = () => (
2020
<Datagrid>
2121
<TextField source="id" />
2222
<EmailField source="email" />
23+
<EmailField
24+
source="author_email"
25+
label="Email of the author of the post"
26+
/>
2327
<NumberField source="nb_vues" />
2428
<NumberField
2529
source="price"

packages/ra-core/codemods/__testfixtures__/replace-Datagrid-DataTable-ManyChildren.output.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ const PostList = () => (
2121
<DataTable.Col source="email">
2222
<EmailField source="email" />
2323
</DataTable.Col>
24+
<DataTable.Col label="Email of the author of the post">
25+
<EmailField
26+
source="author_email"
27+
label="Email of the author of the post"
28+
/>
29+
</DataTable.Col>
2430
<DataTable.NumberCol source="nb_vues" />
2531
<DataTable.NumberCol
2632
source="price"
2733
locales="fr-FR"
2834
options={{ style: 'currency', currency: 'USD' }}
2935
/>
30-
3136
<DataTable.Col source="title" />
3237
<DataTable.Col source="userId">
3338
<ReferenceField source="userId" reference="users">

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

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (file, api: j.API) => {
1414
return root.toSource();
1515
}
1616

17-
wrapChildren(root, j);
17+
transformChildren(root, j);
1818
cleanImports(root, j);
1919

2020
return root.toSource({ quote: 'single', lineTerminator: '\n' });
@@ -139,7 +139,7 @@ const cleanAttributes = (node, j) => {
139139
return sxRenamedAttributes;
140140
};
141141

142-
const wrapChildren = (root, j) => {
142+
const transformChildren = (root, j) => {
143143
// Find all instances of Datagrid
144144
const datagridComponents = root.find(j.JSXElement, {
145145
openingElement: {
@@ -176,7 +176,7 @@ const transformChild = (root, j, child) => {
176176
!['source', 'label', 'empty'].includes(attr.name.name)
177177
)
178178
) {
179-
newChild = replaceTextField(j, child);
179+
child.openingElement.name.name = 'DataTable.Col';
180180
} else if (
181181
j.JSXElement.check(child) &&
182182
child.openingElement.name.type === 'JSXIdentifier' &&
@@ -189,28 +189,31 @@ const transformChild = (root, j, child) => {
189189
)
190190
)
191191
) {
192-
newChild = replaceNumberField(j, child);
192+
child.openingElement.name.name = 'DataTable.NumberCol';
193193
} else {
194194
newChild = wrapChild(j, child);
195-
}
196195

197-
// Replace the original child with the new child
198-
root.find(j.JSXElement, {
199-
openingElement: {
200-
name: {
201-
type: 'JSXIdentifier',
202-
name: 'DataTable',
196+
// Replace the original child with the new child
197+
root.find(j.JSXElement, {
198+
openingElement: {
199+
name: {
200+
type: 'JSXIdentifier',
201+
name: 'DataTable',
202+
},
203203
},
204-
},
205-
}).forEach(dataTableComponent => {
206-
dataTableComponent.value.children =
207-
dataTableComponent.value.children.map(c =>
208-
c === child ? newChild : c
209-
);
210-
});
204+
}).forEach(dataTableComponent => {
205+
dataTableComponent.value.children =
206+
dataTableComponent.value.children.map(c =>
207+
c === child ? newChild : c
208+
);
209+
});
210+
}
211211
};
212212

213213
const wrapChild = (j, child) => {
214+
const labelAttribute = child.openingElement.attributes.find(
215+
attr => j.JSXAttribute.check(attr) && attr.name.name === 'label'
216+
);
214217
const sourceAttribute = child.openingElement.attributes.find(
215218
attr => j.JSXAttribute.check(attr) && attr.name.name === 'source'
216219
);
@@ -219,34 +222,18 @@ const wrapChild = (j, child) => {
219222
return j.jsxElement(
220223
j.jsxOpeningElement(
221224
j.jsxIdentifier('DataTable.Col'),
222-
!sourceAttribute ? [] : [sourceAttribute],
225+
labelAttribute
226+
? [labelAttribute]
227+
: sourceAttribute
228+
? [sourceAttribute]
229+
: [],
223230
false
224231
),
225232
j.jsxClosingElement(j.jsxIdentifier('DataTable.Col')),
226233
[j.jsxText('\n'), child, j.jsxText('\n')]
227234
);
228235
};
229236

230-
const replaceTextField = (j, child) => {
231-
return j.jsxElement(
232-
j.jsxOpeningElement(
233-
j.jsxIdentifier('DataTable.Col'),
234-
child.openingElement.attributes,
235-
true
236-
)
237-
);
238-
};
239-
240-
const replaceNumberField = (j, child) => {
241-
return j.jsxElement(
242-
j.jsxOpeningElement(
243-
j.jsxIdentifier('DataTable.NumberCol'),
244-
child.openingElement.attributes,
245-
true
246-
)
247-
);
248-
};
249-
250237
const cleanImports = (root, j) => {
251238
// Check if there is still a use of TextField in the code
252239
const textFieldUsage = root.find(j.JSXElement, {

0 commit comments

Comments
 (0)