Skip to content

feat: 扩展通过 name/prop 来设置 id 的写法 #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/util/transform-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function transformContent(content) {
item.items = transformContent(item.items)
} else {
removeDollarInKey(item)
setItemId(item)
extractRulesFromComponent(item)
// 有些旧写法是 checkboxGroup & radioGroup
item.type = _kebabcase(item.type)
Expand All @@ -19,13 +20,19 @@ export default function transformContent(content) {
})
}

// 兼容旧写法:$id、$name
function removeDollarInKey(item) {
Object.keys(item)
.filter(k => k.startsWith('$'))
.filter(k => !(k.slice(1) in item))
.filter(k => k.startsWith('$') && !(k.slice(1) in item))
.forEach(k => ((item[k.slice(1)] = item[k]), delete item[k]))
}

function setItemId(item) {
if (item.id) return
// name 是符合表单项直觉的命名; prop 是为了与 element 的 table 的 columns 匹配
item.id = item.name || item.prop
Copy link
Contributor

@yolofit yolofit Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里取值优先级建议对调一下,优先prop > name, 避免用户已设置content item name,导致出错

}

export function extractRulesFromComponent(item) {
if (item.overrideRules) return
const {component} = item
Expand Down
7 changes: 7 additions & 0 deletions test/transform-content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ const expectContent = [
},
]

const hasId = [{id: 'id'}]
const hasName = [{name: 'id'}]
const hasProp = [{prop: 'id'}]

test('transform content', () => {
expect(transformContent(oldContent)).toEqual(expectContent)

expect(transformContent(hasName)[0].id).toBe(hasId[0].id)
expect(transformContent(hasProp)[0].id).toBe(hasId[0].id)
})