fix: inherit default config (#38089)

* fix: inherit default config

* test: add test case
This commit is contained in:
MadCcc 2022-10-18 23:18:11 +08:00 committed by GitHub
parent ba5a7b8d1b
commit 659b89d5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 8 deletions

View File

@ -134,4 +134,26 @@ describe('ConfigProvider.Theme', () => {
),
).toBeTruthy();
});
it('hashed should be true if not changed', () => {
let hashId = 'hashId';
theme.defaultConfig.hashed = true;
const Demo = () => {
const [, , hash] = useToken();
hashId = hash;
return null;
};
render(
<ConfigProvider theme={{ components: { InputNumber: { handleWidth: 50.1234 } } }}>
<Demo />
</ConfigProvider>,
);
expect(hashId).not.toBe('');
theme.defaultConfig.hashed = false;
});
});

View File

@ -2,13 +2,14 @@ import useMemo from 'rc-util/lib/hooks/useMemo';
import shallowEqual from 'shallowequal';
import type { OverrideToken } from '../../theme/interface';
import type { ThemeConfig } from '../context';
import { defaultConfig } from '../../theme';
export default function useTheme(
theme?: ThemeConfig,
parentTheme?: ThemeConfig,
): ThemeConfig | undefined {
const themeConfig = theme || {};
const parentThemeConfig = parentTheme || {};
const parentThemeConfig: ThemeConfig = parentTheme || defaultConfig;
const mergedTheme = useMemo<ThemeConfig | undefined>(
() => {
@ -17,19 +18,19 @@ export default function useTheme(
}
// Override
const mergedOverride = {
const mergedComponents = {
...parentThemeConfig.components,
};
Object.keys(theme.components || {}).forEach((componentName: keyof OverrideToken) => {
mergedOverride[componentName] = {
...mergedOverride[componentName],
mergedComponents[componentName] = {
...mergedComponents[componentName],
...theme.components![componentName],
} as any;
});
// Base token
const merged = {
return {
...parentThemeConfig,
...themeConfig,
@ -37,10 +38,8 @@ export default function useTheme(
...parentThemeConfig.token,
...themeConfig.token,
},
override: mergedOverride,
components: mergedComponents,
};
return merged;
},
[themeConfig, parentThemeConfig],
(prev, next) =>