Skip to content

Commit a5aa388

Browse files
committed
修复 ElSelect 模板解析失败-v-click-outside、class和@[]形式的属性不支持,并修复了 Option API components 里映射缺失导致的属性名 undefined 问题
1 parent b87a615 commit a5aa388

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

packages/vue-to-horizon/libs/horizon-vue/src/next/convert/js/handlers/optionParserHandler.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { JSErrors } from '../../../errors.js'
66
import { DATA_REACTIVE, INSTANCE } from '../../jsx/consts.js'
77
import { watchParser } from './watchHandler.js'
88
import { propsParser } from './propsHandler.js'
9-
import {getIsPinia} from '../jsUtils.js';
9+
import { getIsPinia } from '../jsUtils.js';
1010

1111
/**
1212
* 找到Object的key和value
@@ -392,8 +392,10 @@ function componentsParser(ast, reactCovert) {
392392
t.identifier('components'),
393393
t.objectExpression(
394394
ast.properties.map(node => {
395-
const name = reactCovert.sourceCodeContext.optionTypeRegistComponentMap.get(node.key.name);
396-
return t.objectProperty(t.identifier(node.key.name), t.identifier(name));
395+
const keyName = node.key.name || node.key.value;
396+
const mapped = reactCovert.sourceCodeContext.optionTypeRegistComponentMap.get(keyName);
397+
const targetName = mapped || keyName;
398+
return t.objectProperty(t.identifier(keyName), t.identifier(targetName));
397399
})
398400
)
399401
),

packages/vue-to-horizon/libs/horizon-vue/src/next/convert/jsx/handlers/expressionHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ export function createNodeByVueVariable(name, reactCovert, path) {
206206
// 处理特殊的 $ 开头的属性
207207
else if (name.startsWith('$')) {
208208
switch (name) {
209+
case '$event': {
210+
// 将模板中的 $event 识别为当前事件参数 e
211+
return t.identifier('e');
212+
}
209213
case '$emit': {
210214
// 将模板中的 $emit(...) 统一映射为 emit(...)
211215
// 若不存在 emit,则自动注入: const emit = defineEmits(props);

packages/vue-to-horizon/libs/horizon-vue/src/next/convert/jsx/stringRegexHandler.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ const convertJsxNoCloseTag = input => {
196196
return input.replaceAll(/<br>/g, '<br/>');
197197
};
198198

199+
// 支持动态事件/指令参数归一化
200+
const normalizeDynamicDirectiveArgs = input => {
201+
// @[] 动态事件到 v-on:
202+
let result = input.replace(/@\[(.*?)\]((\.[a-z0-9]+)*)(="[^"]*")?/gi, (m, name, modifiers, _, handler = '') => {
203+
const modStr = (modifiers || '').replace(/\./g, '__');
204+
return `v-on:${name}${modStr}${handler || ''}`;
205+
});
206+
// v-on:[] -> v-on:
207+
result = result.replace(/v-on:\[([^\]]+)\]/gi, (m, name) => `v-on:${name}`);
208+
// 通用 v-xxx:[arg] -> v-xxx:arg
209+
result = result.replace(/v-([a-zA-Z-]+):\[([^\]]+)\]/g, (m, dir, arg) => `v-${dir}:${arg}`);
210+
return result;
211+
};
212+
199213
// 正则表达式匹配 v-model.xxx 语法
200214
const regex = /v-model\.(\w+)/g;
201215
const removeModelModifiers = input => {
@@ -215,6 +229,7 @@ const DefaultHardCodeHandler = {
215229
JSXNotes: convertJSXNotes,
216230
DynamicBinding: convertVueBindingToReact,
217231
ShortBinding: convertVBindShorthand,
232+
DynamicArgs: normalizeDynamicDirectiveArgs,
218233
ShortOn: convertVueShortEventToReact,
219234
ShortSlot: convertVueShortSlotToReact,
220235
vShow: transformVShowSyntax,

0 commit comments

Comments
 (0)