@@ -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
213213const 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-
250237const 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