@@ -80,24 +80,12 @@ const replaceDatagrid = (root, j) => {
80
80
81
81
// Replace Datagrid with DataTable
82
82
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 ) ;
96
84
97
85
const openingElement = j . jsxOpeningElement (
98
86
j . jsxIdentifier ( 'DataTable' ) ,
99
87
attributes ,
100
- node . openingElement . selfClosing
88
+ [ node . openingElement . selfClosing ]
101
89
) ;
102
90
const closingElement = j . jsxClosingElement (
103
91
j . jsxIdentifier ( 'DataTable' )
@@ -108,6 +96,55 @@ const replaceDatagrid = (root, j) => {
108
96
return true ;
109
97
} ;
110
98
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
+ / R a D a t a g r i d - / 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
+
111
148
const wrapChildren = ( root , j ) => {
112
149
// Find all instances of Datagrid
113
150
const datagridComponents = root . find ( j . JSXElement , {
0 commit comments