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: 更改 useConfig computed属性计算导致列表渲染卡顿问题 #1122

Merged
merged 3 commits into from
Jul 5, 2022
Merged
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
54 changes: 27 additions & 27 deletions src/config-provider/useConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { computed, inject, h } from '@vue/composition-api';
import { inject, h, ref } from '@vue/composition-api';
import { GlobalConfigProvider, defaultGlobalConfig } from './context';

// 处理正则表达式
const t = function <T> (pattern: T, ...args: any[]) {
const [data] = args;
if (typeof pattern === 'string') {
if (!data) return pattern;
const regular = /\{\s*([\w-]+)\s*\}/g;
const translated = pattern.replace(regular, (match, key) => {
if (data) {
return String(data[key]);
}
return '';
});
return translated;
}
if (typeof pattern === 'function') {
// 重要:组件的渲染必须存在参数 h,不能移除
if (!args.length) return pattern(h);
return pattern(...args);
}
return '';
};

/**
* component global config
* @param componentName
Expand All @@ -9,32 +31,10 @@ import { GlobalConfigProvider, defaultGlobalConfig } from './context';
*/
export function useConfig<T extends keyof GlobalConfigProvider>(componentName?: T) {
const injectGlobalConfig = inject<GlobalConfigProvider>('globalConfig', null);
const mergedGlobalConfig = computed(() => injectGlobalConfig || defaultGlobalConfig);
const global = computed(() => mergedGlobalConfig.value[componentName]);

const classPrefix = computed(() => mergedGlobalConfig.value.classPrefix);
const mergedGlobalConfig = injectGlobalConfig || defaultGlobalConfig;

// 处理正则表达式
const t = function <T> (pattern: T, ...args: any[]) {
const [data] = args;
if (typeof pattern === 'string') {
if (!data) return pattern;
const regular = /\{\s*([\w-]+)\s*\}/g;
const translated = pattern.replace(regular, (match, key) => {
if (data) {
return String(data[key]);
}
return '';
});
return translated;
}
if (typeof pattern === 'function') {
// 重要:组件的渲染必须存在参数 h,不能移除
if (!args.length) return pattern(h);
return pattern(...args);
}
return '';
};
const global = ref(mergedGlobalConfig[componentName]);
const classPrefix = ref(mergedGlobalConfig.classPrefix);

return {
t,
Expand All @@ -45,5 +45,5 @@ export function useConfig<T extends keyof GlobalConfigProvider>(componentName?:

export function usePrefixClass(componentName?: string) {
const { classPrefix } = useConfig('classPrefix');
return computed(() => (componentName ? `${classPrefix.value}-${componentName}` : classPrefix.value));
return ref(componentName ? `${classPrefix.value}-${componentName}` : classPrefix.value);
}