mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-28 05:05:48 +08:00
fix: inherit default config (#38089)
* fix: inherit default config * test: add test case
This commit is contained in:
parent
ba5a7b8d1b
commit
659b89d5e6
@ -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;
|
||||
});
|
||||
});
|
||||
|
@ -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) =>
|
||||
|
Loading…
Reference in New Issue
Block a user