Skip to content

Commit

Permalink
feat: 支持Space
Browse files Browse the repository at this point in the history
  • Loading branch information
lili.21 committed Sep 4, 2023
1 parent e2c5aa8 commit 9a167e2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Replacing `<path>` with appropriate values.
- [x] Tag
- [x] Timeline
- [x] Collapse
- [ ] Select
- [x] Select
- [ ] SelectProps
- [ ] DatePicker
- [ ] TimePicker
Expand Down
3 changes: 2 additions & 1 deletion bin/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const TRANSFORMER_INQUIRER_CHOICES = [
'Timeline',
'Collapse',
'Select',
'Popconfirm'
'Popconfirm',
'Space'
]
.sort((a, b) => a.localeCompare(b))
.map((v) => ({
Expand Down
5 changes: 2 additions & 3 deletions test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Table } from 'antd'
import { Table, Space } from 'antd'

export const test = () => {
const paginationProps = { current: 1, showTotal: (t) => `${t}条` }
return <Table pagination={paginationProps} />
return <Space direction="horizon" />
}
27 changes: 27 additions & 0 deletions transforms/Popconfirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { removeAntdImportAndAddSemiImport } = require('./utils')
module.exports = function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)

removeAntdImportAndAddSemiImport(j, root, 'Popconfirm', 'Popconfirm')

// Find all JSXElements with the name "Popconfirm"
root.findJSXElements('Popconfirm').forEach((path) => {
const { openingElement } = path.value

// Find the "placement" attribute and replace it with "position"
const placementAttribute = openingElement.attributes.find(
(attr) => attr.name.name === 'placement'
)
if (placementAttribute) {
placementAttribute.name.name = 'position'
} else {
// semi的默认position为bottom,antd为top,所以需要默认添加 position="top" 属性
openingElement.attributes.push(
j.jsxAttribute(j.jsxIdentifier('position'), j.literal('top'))
)
}
})

return root.toSource()
}
29 changes: 29 additions & 0 deletions transforms/Space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { removeAntdImportAndAddSemiImport } = require('./utils')
module.exports = function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)

removeAntdImportAndAddSemiImport(j, root, 'Space', 'Space')

// Find all JSXElements with the name "Space"
root.findJSXElements('Space').forEach((path) => {
const { openingElement } = path.value

// Find the "direction" attribute and replace it with "vertical"
const directionAttribute = openingElement.attributes.find(
(attr) => attr.name.name === 'direction'
)
if (directionAttribute) {
if (directionAttribute.value.value === 'vertical') {
openingElement.attributes.push(
j.jsxAttribute(j.jsxIdentifier('vertical'))
)
}
openingElement.attributes = openingElement.attributes.filter(
(attr) => attr.name.name !== 'direction'
)
}
})

return root.toSource()
}

0 comments on commit 9a167e2

Please sign in to comment.