diff --git a/packages/components/codemods/upgradeV1Buttons/transformV1Buttons.ts b/packages/components/codemods/upgradeV1Buttons/transformV1Buttons.ts index 2bdbd7a606b..35e09175ed9 100644 --- a/packages/components/codemods/upgradeV1Buttons/transformV1Buttons.ts +++ b/packages/components/codemods/upgradeV1Buttons/transformV1Buttons.ts @@ -1,5 +1,23 @@ import ts from 'typescript' -import { createJsxElementWithChildren, createProp, createStringProp } from '../utils' +import { type ButtonProps as V1ButtonProps } from '~components/Button' +import { type ButtonProps as RCButtonProps } from '~components/__rc__/Button' +import { + createJsxElementWithChildren, + createProp, + createStringProp, + getPropValueText, +} from '../utils' + +const getNewSizeValue = ( + oldValue: Exclude, +): Exclude => { + switch (oldValue) { + case 'small': + return 'medium' + case 'regular': + return 'large' + } +} /** * @returns @@ -22,6 +40,14 @@ const transformProp = ( return createProp('data-testid', propValue) case 'disabled': return createProp('isDisabled', propValue) + case 'size': { + if (!propValue) return createStringProp('size', 'large') + + const sizeValue = getPropValueText(propValue) as Exclude + return sizeValue + ? createStringProp('size', getNewSizeValue(sizeValue)) + : createProp('size', propValue) + } default: return undefined } @@ -33,6 +59,7 @@ export const transformV1Buttons = ( tagName: string = 'Button', ): ts.Node => { let childrenValue: ts.JsxAttributeValue | undefined + let hasSizeProp = false const newAttributes = node.attributes.properties.reduce((acc, attr) => { if (ts.isJsxAttribute(attr)) { @@ -49,6 +76,10 @@ export const transformV1Buttons = ( return acc } + if (propName === 'size') { + hasSizeProp = true + } + const newProp = transformProp(propName, attr.initializer) if (newProp === null) return acc @@ -67,5 +98,9 @@ export const transformV1Buttons = ( newAttributes.push(createProp('hasHiddenLabel')) } + if (!hasSizeProp) { + newAttributes.push(createStringProp('size', 'large')) + } + return createJsxElementWithChildren(tagName, newAttributes, childrenValue) }