Skip to content

Commit

Permalink
🐛 fix: 修正在添加 ThemeProvider 后 customToken 设定会丢失的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Feb 19, 2023
1 parent c51f422 commit 936e43b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/factories/createThemeProvider/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let darkThemeMatch: MediaQueryList;
const matchThemeMode = (mode: ThemeAppearance) =>
matchMedia && matchMedia(`(prefers-color-scheme: ${mode})`);

// 将适配
const ThemeObserver: FC<{
themeMode: ThemeMode;
setAppearance: (value: ThemeAppearance) => void;
Expand Down
3 changes: 2 additions & 1 deletion src/factories/createThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface CreateThemeProviderOptions {
styledConfig?: StyledConfig;
CustomThemeContext: Context<any>;
prefixCls?: string;
customToken: ThemeProviderProps<any>['customToken'];
}

export const createThemeProvider = (
Expand All @@ -24,7 +25,7 @@ export const createThemeProvider = (
({
children,

customToken,
customToken = option.customToken,
customStylish,

theme,
Expand Down
5 changes: 3 additions & 2 deletions src/functions/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface CreateOptions<T> {
/**
* 默认的自定义 Token
*/
defaultCustomToken?: T;
customToken?: T;
hashPriority?: HashPriority;

ThemeProvider?: Omit<ThemeProviderProps<T>, 'children'>;
Expand All @@ -55,7 +55,7 @@ export const createInstance = <T = any>(options: CreateOptions<T>) => {
// ******* 下面这些都和主题相关,如果做了任何改动,都需要排查一遍 ************* //

const CustomThemeContext = createContext<T>(
(options.defaultCustomToken ? options.defaultCustomToken : {}) as T,
(options.customToken ? options.customToken : {}) as T,
);

const styledUseTheme = options.styled?.useTheme as () => Theme;
Expand All @@ -79,6 +79,7 @@ export const createInstance = <T = any>(options: CreateOptions<T>) => {
styledConfig: options.styled,
CustomThemeContext,
prefixCls: options.prefixCls,
customToken: options.customToken,
});

// ******** 上面这些都和主题相关,如果做了任何改动,都需要排查一遍 ************ //
Expand Down
10 changes: 6 additions & 4 deletions tests/functions/createInstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('createInstance', () => {
key: 'xxx',
prefixCls: 'test',
speedy: true,
defaultCustomToken: {
customToken: {
x: '123',
},
});
Expand All @@ -26,14 +26,16 @@ describe('createInstance', () => {

expect(result.current.x).toEqual('123');
});
it('包裹 ThemeProvider 时 x 也存在', () => {
const { result } = renderHook(instance.useTheme, { wrapper: instance.ThemeProvider });

expect(result.current.x).toEqual('123');
});

it('创建实例时可以不填 key', () => {
const instance = createInstance({
prefixCls: 'test',
speedy: true,
defaultCustomToken: {
x: '123',
},
});
expect(instance.styleManager.sheet.key).toEqual('ant-css');
});
Expand Down

0 comments on commit 936e43b

Please sign in to comment.