Skip to content

Commit

Permalink
feat(upgradeV1Buttons): transform size prop
Browse files Browse the repository at this point in the history
  • Loading branch information
HeartSquared committed Dec 19, 2024
1 parent 9c04236 commit e35370a
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<V1ButtonProps['size'], undefined>,
): Exclude<RCButtonProps['size'], undefined> => {
switch (oldValue) {
case 'small':
return 'medium'
case 'regular':
return 'large'
}
}

/**
* @returns
Expand All @@ -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<V1ButtonProps['size'], undefined>
return sizeValue
? createStringProp('size', getNewSizeValue(sizeValue))
: createProp('size', propValue)
}
default:
return undefined
}
Expand All @@ -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<ts.JsxAttributeLike[]>((acc, attr) => {
if (ts.isJsxAttribute(attr)) {
Expand All @@ -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
Expand All @@ -67,5 +98,9 @@ export const transformV1Buttons = (
newAttributes.push(createProp('hasHiddenLabel'))
}

if (!hasSizeProp) {
newAttributes.push(createStringProp('size', 'large'))
}

return createJsxElementWithChildren(tagName, newAttributes, childrenValue)
}

0 comments on commit e35370a

Please sign in to comment.