Skip to content

Commit

Permalink
feat: Upload, Input, InputNumber, Radio组件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
lili.21 committed Oct 10, 2023
1 parent 834f7b2 commit ff609b9
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ Replacing `<path>` with appropriate values.
- [ ] SelectProps
- [ ] DatePicker
- [ ] TimePicker
- [ ] Input
- [ ] InputNumber
- [ ] Radio
- [x] Input
- [x] InputNumber
- [x] Radio
- [ ] Switch
- [ ] Checkbox
- [ ] Upload
- [x] Upload
- [ ] AutoComplete
- [ ] Dropdown
- [ ] Form
Expand Down
6 changes: 5 additions & 1 deletion bin/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ const TRANSFORMER_INQUIRER_CHOICES = [
'Select',
'Popconfirm',
'Space',
'Button'
'Button',
'InputNumber',
'Radio',
'Upload',
'Input'
]
.sort((a, b) => a.localeCompare(b))
.map((v) => ({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"dependencies": {
"@swc/core": "^1.3.82",
"chalk": "4",
"execa": "4.0.3",
"globby": "11.0.1",
"inquirer": "7.3.3",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions test.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Table, Space, Button } from 'antd'
import { Table, Space, Button, Upload } from 'antd'

export const test = () => {
return (
<Button type="link" danger>
数据大盘
</Button>
)
return <Upload />
}
9 changes: 9 additions & 0 deletions transforms/Input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { removeAntdImportAndAddSemiImport } = require('./utils')
module.exports = function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)

removeAntdImportAndAddSemiImport(j, root, 'Input', 'Input')

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

removeAntdImportAndAddSemiImport(j, root, 'InputNumber', 'InputNumber')

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

removeAntdImportAndAddSemiImport(j, root, 'Radio', 'Radio')

return root.toSource()
}
42 changes: 42 additions & 0 deletions transforms/Upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const chalk = require('chalk')

const { removeAntdImportAndAddSemiImport } = require('./utils')
module.exports = function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)

removeAntdImportAndAddSemiImport(j, root, 'Upload', 'Upload')

// todo - 暂时没想好怎么自动处理 customRequest 的差异
console.log(
chalk.yellow(`
Upload组件 customRequest函数的参数有差异,如果有用到,记得手动更改一下
Antd
----
<Upload customRequest={({ file } => {})} />
Semi
----
<Upload customRequest={({ fileInstance: file }) => {}} />
Semi中fileInstance参数和Antd中的file等价
`)
)

// Find the Upload component and update its props
root.findJSXElements('Upload').forEach((path) => {
const { openingElement } = path.value

const actionAttribute = openingElement.attributes.find(
(attr) => attr.name.name === 'action'
)
// Semi组件<Upload /> action是必须
if (!actionAttribute) {
openingElement.attributes.push(
j.jsxAttribute(j.jsxIdentifier('action'), j.literal(''))
)
}
})

return root.toSource()
}

0 comments on commit ff609b9

Please sign in to comment.