From 91f3fbb4f31e3356a44229ebbb2f807043223052 Mon Sep 17 00:00:00 2001 From: sunhao Date: Wed, 24 Jul 2024 16:12:54 +0800 Subject: [PATCH] * core: auto check Component from global zui on create instance. --- lib/core/src/component/creator.ts | 32 ++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/core/src/component/creator.ts b/lib/core/src/component/creator.ts index bda71f281d..9c3e21b7cc 100644 --- a/lib/core/src/component/creator.ts +++ b/lib/core/src/component/creator.ts @@ -25,7 +25,10 @@ export function getComponent(name: string): ComponentClass | undefined { } export function create(name: string, element: HTMLElement, options: ComponentCreateOptions = {}) { - const TheComponentClass = getComponent(name); + let TheComponentClass = getComponent(name); + if (!TheComponentClass) { + TheComponentClass = initGlobalComponents(name); + } if (!TheComponentClass) { return null; } @@ -50,16 +53,27 @@ export function registerComponent(component: ComponentClass, name?: string) { Component.register(component, name); } -export function initGlobalComponents() { +export function initGlobalComponents(name?: string): ComponentClass | undefined { const {zui} = window as unknown as {zui: Record}; - if (zui) { - Object.keys(zui).forEach((n) => { - const TheComponentClass = zui[n]; - if (!TheComponentClass.NAME || !TheComponentClass.ZUI) { - return; - } + if (!zui) { + return; + } + name = name?.toLowerCase(); + for (const n in zui) { + const isMatch = n.toLowerCase() === name; + if (name && !isMatch) { + continue; + } + const TheComponentClass = zui[n]; + if (typeof TheComponentClass !== 'function' || !TheComponentClass.NAME || !TheComponentClass.ZUI) { + continue; + } + if (!Component.map.has(n.toLowerCase())) { registerComponent(TheComponentClass); - }); + } + if (isMatch) { + return TheComponentClass; + } } }