提交 a6834452 编写于 作者: fxy060608's avatar fxy060608

fix(nvue): v-model with input,textarea (question/143547)

上级 66fc6f50
......@@ -8,8 +8,8 @@ const _hoisted_1 = [\\"modelValue\\"]
export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock(\\"u-input\\", {
modelValue: _ctx.text,
\\"onUpdate:modelValue\\": _cache[0] || (_cache[0] = $event => ((_ctx.text) = $event))
}, null, 8 /* PROPS */, _hoisted_1))
onInput: _cache[0] || (_cache[0] = $event => ((_ctx.text) = $event.detail.value))
}, null, 40 /* PROPS, HYDRATE_EVENTS */, _hoisted_1))
}"
`;
......@@ -45,8 +45,8 @@ const _hoisted_1 = [\\"modelValue\\"]
export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock(\\"u-textarea\\", {
modelValue: _ctx.text,
\\"onUpdate:modelValue\\": _cache[0] || (_cache[0] = $event => ((_ctx.text) = $event))
}, null, 8 /* PROPS */, _hoisted_1))
onInput: _cache[0] || (_cache[0] = $event => ((_ctx.text) = $event.detail.value))
}, null, 40 /* PROPS, HYDRATE_EVENTS */, _hoisted_1))
}"
`;
......
import {
transformModel as baseTransform,
DirectiveTransform,
isStaticExp,
NodeTypes,
} from '@vue/compiler-core'
// 所有的 v-model 均走自定义组件的实现逻辑,包括 input,textarea
const tags = ['u-input', 'u-textarea']
export const transformModel: DirectiveTransform = (dir, node, context) => {
return baseTransform(dir, node, context)
const result = baseTransform(dir, node, context)
// 将 u-input,u-textarea 的 onUpdate:modelValue 事件转换为 onInput
if (tags.includes(node.tag) && result.props.length >= 2) {
const key = result.props[1].key
let value = result.props[1].value
if (value.type === NodeTypes.JS_CACHE_EXPRESSION) {
value = value.value
}
if (
isStaticExp(key) &&
key.content === 'onUpdate:modelValue' &&
value.type === NodeTypes.COMPOUND_EXPRESSION
) {
key.content = 'onInput'
// 替换 $event 为 $event.detail.value
value.children = value.children.map((child) => {
if (typeof child === 'string') {
return child.replace(/=\s\$event/g, `= $event.detail.value`)
}
return child
})
}
}
return result
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册