Skip to content
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

fix: state accessor defaultValue support expression #977 #997

Open
wants to merge 4 commits into
base: refactor/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
72 changes: 47 additions & 25 deletions packages/vue-generator/src/generator/vue/sfc/generateAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,50 @@ export const handleTinyIconPropsHook = (schemaData, globalHooks, config) => {
})
}

// 生成 watchEffect
const genStateAccessor = (value, globalHooks) => {
if (isSetter(value?.accessor) && value.accessor.setter?.value) {
globalHooks.addStatement({
position: INSERT_POSITION.AFTER_METHODS,
value: `vue.watchEffect(wrap(${value.accessor.setter?.value}))`
})
}

if (isGetter(value?.accessor) && value.accessor.getter?.value) {
globalHooks.addStatement({
position: INSERT_POSITION.AFTER_METHODS,
value: `vue.watchEffect(wrap(${value.accessor.getter?.value}))`
})
}
}
chilingling marked this conversation as resolved.
Show resolved Hide resolved

// 针对 state 有 getter 的场景进行处理
export const handleAccessorBinding = (renderKey, value, globalHooks, config) => {
genStateAccessor(value, globalHooks)

const result = { shouldBindToState: false, res: '' }

if (typeof value.defaultValue === 'string') {
result.res = `${renderKey}"${value.defaultValue.replaceAll("'", "\\'").replaceAll(/"/g, "'")}"`
} else if (specialTypeHandler[value?.defaultValue?.type]) {
const specialVal = specialTypeHandler[value.defaultValue.type](value.defaultValue, globalHooks, config)?.value || ''

if (specialTypes.includes(value.defaultValue.type)) {
result.shouldBindToState = true
}

result.res = `${renderKey}${specialVal}`
} else {
const { res: tempRes } =
// eslint-disable-next-line no-use-before-define
transformObjType(value.defaultValue, globalHooks, config) || {}
chilingling marked this conversation as resolved.
Show resolved Hide resolved

result.res = `${renderKey}${tempRes}`
}

return result
}

export const transformObjType = (obj, globalHooks, config) => {
if (!obj || typeof obj !== 'object') {
return {
Expand Down Expand Up @@ -415,32 +459,10 @@ export const transformObjType = (obj, globalHooks, config) => {
}

if (hasAccessor(value?.accessor)) {
if (typeof value.defaultValue === 'string') {
resStr.push(`${renderKey}"${value.defaultValue.replaceAll("'", "\\'").replaceAll(/"/g, "'")}"`)
} else {
const { res: tempRes, shouldBindToState: tempShouldBindToState } =
transformObjType(value.defaultValue, globalHooks, config) || {}

resStr.push(`${renderKey}${tempRes}`)

if (tempShouldBindToState) {
shouldBindToState = true
}
}
// 递归处理其他类型
const { res } = handleAccessorBinding(renderKey, value, globalHooks, config)

if (isSetter(value?.accessor)) {
globalHooks.addStatement({
position: INSERT_POSITION.AFTER_METHODS,
value: `vue.watchEffect(wrap(${value.accessor.setter?.value ?? ''}))`
})
}

if (isGetter(value?.accessor)) {
globalHooks.addStatement({
position: INSERT_POSITION.AFTER_METHODS,
value: `vue.watchEffect(wrap(${value.accessor.getter?.value ?? ''}))`
})
}
resStr.push(res)

continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const { t, lowcodeWrap, stores } = vue.inject(I18nInjectionKey).lowcode()
const wrap = lowcodeWrap(props, { emit })
wrap({ stores })

const { utils } = wrap(function () {
return this
})()
const state = vue.reactive({
firstName: 'Opentiny',
lastName: 'TinyEngine',
Expand All @@ -26,7 +29,22 @@ const state = vue.reactive({
trueVal: true,
falseVal: false,
arrVal: [1, '2', { aaa: 'aaa' }, [3, 4], true, false],
objVal: { aaa: 'aaa', arr: [1, '2', true, false, 0], ccc: { bbb: 'bbb' }, d: 32432, e: '', f: null, g: false }
objVal: { aaa: 'aaa', arr: [1, '2', true, false, 0], ccc: { bbb: 'bbb' }, d: 32432, e: '', f: null, g: false },
IconPlusSquare: utils.IconPlusSquare(),
editConfig: {
trigger: 'click',
mode: 'cell',
showStatus: true,
activeMethod: () => {
return props.isEdit
}
},
status: vue.computed(statusData),
buttons: [
{ type: 'primary', text: '主要操作' },
{ type: 'success', text: '成功操作' },
{ type: 'danger', text: t('operation.danger') }
]
})
wrap({ state })

Expand Down Expand Up @@ -70,5 +88,25 @@ vue.watchEffect(
this.state.objVal = `${this.state.firstName} ${this.state.lastName}`
})
)
vue.watchEffect(
wrap(function () {
this.state.IconPlusSquare = `${this.state.firstName} ${this.state.lastName}`
})
)
vue.watchEffect(
wrap(function () {
this.state.editConfig = `${this.state.firstName} ${this.state.lastName}`
})
)
vue.watchEffect(
wrap(function () {
this.state.status = `${this.state.firstName} ${this.state.lastName}`
})
)
vue.watchEffect(
wrap(function () {
this.state.buttons = `${this.state.firstName} ${this.state.lastName}`
})
)
chilingling marked this conversation as resolved.
Show resolved Hide resolved
</script>
<style scoped></style>
67 changes: 67 additions & 0 deletions packages/vue-generator/test/testcases/sfc/accessor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,73 @@
"value": "function() { this.state.objVal = `${this.state.firstName} ${this.state.lastName}` }"
}
}
},
"IconPlusSquare": {
"defaultValue": {
"type": "JSResource",
"value": "this.utils.IconPlusSquare()"
},
"accessor": {
"getter": {
"type": "JSFunction",
"value": "function() { this.state.IconPlusSquare = `${this.state.firstName} ${this.state.lastName}` }"
}
}
},
"editConfig": {
"defaultValue": {
Copy link
Collaborator

@hexqi hexqi Jan 9, 2025

Choose a reason for hiding this comment

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

这属于高阶用法了,后面可以讨论下UI怎么跟随调整,官网文档也需要补充

"trigger": "click",
"mode": "cell",
"showStatus": true,
"activeMethod": {
"type": "JSFunction",
"value": "function() { return this.props.isEdit }"
}
},
"accessor": {
"getter": {
"type": "JSFunction",
"value": "function() { this.state.editConfig = `${this.state.firstName} ${this.state.lastName}` }"
}
}
},
"status": {
"defaultValue": {
"type": "JSExpression",
"value": "this.statusData",
"computed": true
Copy link
Collaborator

Choose a reason for hiding this comment

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

当前插件UI不支持computed吧,是不是只能用JSON方式手动输入进去?

},
"accessor": {
"getter": {
"type": "JSFunction",
"value": "function() { this.state.status = `${this.state.firstName} ${this.state.lastName}` }"
}
}
},
"buttons": {
"defaultValue": [
{
"type": "primary",
"text": "主要操作"
},
{
"type": "success",
"text": "成功操作"
},
{
"type": "danger",
"text": {
"type": "i18n",
"key": "operation.danger"
}
}
],
"accessor": {
"getter": {
"type": "JSFunction",
"value": "function() { this.state.buttons = `${this.state.firstName} ${this.state.lastName}` }"
}
}
}
},
"lifeCycles": {},
Expand Down
Loading