-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE(web-react): Rename height and maxHeight ModalDialog p…
…rops and enhance them #DS-1134
- Loading branch information
Showing
14 changed files
with
560 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/codemods/src/transforms/v2/web-react/__testfixtures__/modal-custom-height.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { ModalDialog } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<ModalDialog preferredHeightOnMobile="400px" /> | ||
<ModalDialog preferredHeightFromTabletUp="500px" /> | ||
<ModalDialog maxHeightFromTabletUp="600px" /> | ||
<ModalDialog preferredHeightOnMobile="400px" preferredHeightFromTabletUp="500px" /> | ||
</> | ||
); |
22 changes: 22 additions & 0 deletions
22
...ages/codemods/src/transforms/v2/web-react/__testfixtures__/modal-custom-height.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { ModalDialog } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<ModalDialog height={{ | ||
mobile: '400px' | ||
}} /> | ||
<ModalDialog height={{ | ||
tablet: '500px' | ||
}} /> | ||
<ModalDialog maxHeight={{ | ||
tablet: '600px' | ||
}} /> | ||
<ModalDialog | ||
height={{ | ||
mobile: '400px', | ||
tablet: '500px' | ||
}} /> | ||
</> | ||
); |
3 changes: 3 additions & 0 deletions
3
packages/codemods/src/transforms/v2/web-react/__tests__/modal-custom-height.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testTransform } from '../../../../../tests/testUtils'; | ||
|
||
testTransform(__dirname, 'modal-custom-height'); |
108 changes: 108 additions & 0 deletions
108
packages/codemods/src/transforms/v2/web-react/modal-custom-height.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
|
||
const transform = (fileInfo: FileInfo, api: API) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
// Find import statements for the specific module and ModalDialog specifier | ||
const importStatements = root.find(j.ImportDeclaration, { | ||
source: { | ||
value: (value: string) => /^@lmc-eu\/spirit-web-react(\/.*)?$/.test(value), | ||
}, | ||
}); | ||
|
||
// Check if the module is imported | ||
if (importStatements.length > 0) { | ||
const modalDialogSpecifier = importStatements.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
name: 'ModalDialog', | ||
}, | ||
}); | ||
|
||
// Check if ModalDialog specifier is present | ||
if (modalDialogSpecifier.length > 0) { | ||
// Find ModalDialog components in the module | ||
const modalDialogComponents = root.find(j.JSXOpeningElement, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'ModalDialog', | ||
}, | ||
}); | ||
|
||
modalDialogComponents.forEach((path) => { | ||
const attributes = path.node.attributes || []; | ||
|
||
let preferredHeightOnMobile: string | null = null; | ||
let preferredHeightFromTabletUp: string | null = null; | ||
let maxHeightFromTabletUp: string | null = null; | ||
|
||
// Iterate over attributes to find and remove the specific props | ||
path.node.attributes = attributes.filter((attr) => { | ||
if (j.JSXAttribute.check(attr)) { | ||
const attrName = attr.name.name; | ||
|
||
if (attrName === 'preferredHeightOnMobile' && attr.value && j.Literal.check(attr.value)) { | ||
preferredHeightOnMobile = attr.value.value as string; | ||
|
||
return false; | ||
} | ||
|
||
if (attrName === 'preferredHeightFromTabletUp' && attr.value && j.Literal.check(attr.value)) { | ||
preferredHeightFromTabletUp = attr.value.value as string; | ||
|
||
return false; | ||
} | ||
|
||
if (attrName === 'maxHeightFromTabletUp' && attr.value && j.Literal.check(attr.value)) { | ||
maxHeightFromTabletUp = attr.value.value as string; | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
// Create new height and maxHeight attributes | ||
if (preferredHeightOnMobile || preferredHeightFromTabletUp) { | ||
const heightObjectProperties = []; | ||
|
||
if (preferredHeightOnMobile) { | ||
heightObjectProperties.push(j.property('init', j.identifier('mobile'), j.literal(preferredHeightOnMobile))); | ||
} | ||
|
||
if (preferredHeightFromTabletUp) { | ||
heightObjectProperties.push( | ||
j.property('init', j.identifier('tablet'), j.literal(preferredHeightFromTabletUp)), | ||
); | ||
} | ||
|
||
const heightAttribute = j.jsxAttribute( | ||
j.jsxIdentifier('height'), | ||
j.jsxExpressionContainer(j.objectExpression(heightObjectProperties)), | ||
); | ||
|
||
path.node.attributes.push(heightAttribute); | ||
} | ||
|
||
if (maxHeightFromTabletUp) { | ||
const maxHeightObjectProperties = [ | ||
j.property('init', j.identifier('tablet'), j.literal(maxHeightFromTabletUp)), | ||
]; | ||
|
||
const maxHeightAttribute = j.jsxAttribute( | ||
j.jsxIdentifier('maxHeight'), | ||
j.jsxExpressionContainer(j.objectExpression(maxHeightObjectProperties)), | ||
); | ||
|
||
path.node.attributes.push(maxHeightAttribute); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return root.toSource({ quote: 'single' }); | ||
}; | ||
|
||
export default transform; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.