Skip to content

Commit

Permalink
✨ feat: 在各方法中新增 prefixCls 的参数
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 21, 2023
1 parent 576465b commit 33d23b4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/containers/ThemeProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export const ThemeProvider: <T = any, S = any>(
theme={theme}
getStaticInstance={getStaticInstance}
>
<TokenContainer customToken={customToken} customStylish={customStylish}>
<TokenContainer
prefixCls={prefixCls}
customToken={customToken}
customStylish={customStylish}
>
{children}
</TokenContainer>
</AntdProvider>
Expand Down
4 changes: 3 additions & 1 deletion src/containers/ThemeProvider/TokenContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { Theme } from '@/types';

type TokenContainerProps<T, S = Record<string, string>> = Pick<
ThemeProviderProps<T, S>,
'children' | 'customToken' | 'customStylish'
'children' | 'customToken' | 'customStylish' | 'prefixCls'
>;

const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement | null = ({
children,
customToken: customTokenOrFn,
customStylish: stylishOrGetStylish,
prefixCls = 'ant',
}) => {
const themeState = useThemeMode();
const { appearance, isDarkMode } = themeState;
Expand Down Expand Up @@ -53,6 +54,7 @@ const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement |
...customToken,
stylish,
...themeState,
prefixCls,
};

return <Provider theme={theme}>{children}</Provider>;
Expand Down
5 changes: 0 additions & 5 deletions src/context/EmotionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ import { createContext } from 'react';

import { emotion, type Emotion } from '@/functions';

export interface CommonStyleUtils {
cx: Emotion['cx'];
css: Emotion['css'];
}

export const EmotionContext = createContext<Emotion>(emotion);
22 changes: 15 additions & 7 deletions src/functions/createStyles.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMemo } from 'react';

import { CommonStyleUtils } from '@/context';
import { useEmotion, useTheme } from '@/hooks';
import {
import type {
CommonStyleUtils,
FullStylish,
FullToken,
ReturnStyleToUse,
Expand All @@ -18,15 +18,17 @@ export interface CreateStylesTheme extends CommonStyleUtils {
stylish: FullStylish;
appearance: ThemeAppearance;
isDarkMode: boolean;
prefixCls: string;
}

/**
* 最终返回 styles 对象的类型定义
*/
export interface ReturnStyles<T extends StyleInputType> {
styles: ReturnStyleToUse<T>;
theme: Theme;
theme: Omit<Theme, 'prefixCls'>;
cx: Emotion['cx'];
prefixCls: string;
}

// 获取样式
Expand Down Expand Up @@ -56,14 +58,17 @@ export const createStyles =
const styles = useMemo(() => {
let tempStyles: ReturnStyleToUse<Input>;

// 函数场景
if (styleOrGetStyleFn instanceof Function) {
const { stylish, appearance, isDarkMode, ...token } = theme;
const { stylish, appearance, isDarkMode, prefixCls, ...token } = theme;

tempStyles = styleOrGetStyleFn(
{ token, stylish, appearance, cx, css, isDarkMode },
{ token, stylish, appearance, cx, css, isDarkMode, prefixCls },
props!,
) as any;
} else {
}
// 没有函数时直接就是 object 或者 string
else {
tempStyles = styleOrGetStyleFn as any;
}

Expand All @@ -82,5 +87,8 @@ export const createStyles =
return tempStyles;
}, [styleOrGetStyleFn, props, theme]);

return useMemo(() => ({ styles, cx, theme }), [styles, theme]);
return useMemo(() => {
const { prefixCls, ...res } = theme;
return { styles, cx, theme: res, prefixCls };
}, [styles, theme]);
};
5 changes: 4 additions & 1 deletion src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export const useTheme = (): Theme => {
const themeState = useThemeMode();
const defaultTheme = _useTheme();

const initTheme = useMemo(() => ({ ...antdTheme, ...themeState }), [antdTheme, themeState]);
const initTheme = useMemo<Theme>(
() => ({ ...antdTheme, ...themeState, prefixCls: 'ant' }),
[antdTheme, themeState],
);

// 如果是个空值,说明没有套 Provider,返回 antdTheme 的默认值
if (Object.keys(defaultTheme).length === 0) {
Expand Down
8 changes: 7 additions & 1 deletion src/types/theme.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ThemeConfig } from 'antd/es/config-provider/context';
import { AliasToken } from 'antd/es/theme/interface';

import { CommonStyleUtils } from '@/context';
import { Emotion } from '@/functions';
import { ThemeAppearance, ThemeMode } from './appearance';

export interface CommonStyleUtils {
cx: Emotion['cx'];
css: Emotion['css'];
}
export interface ThemeContextState {
appearance: ThemeAppearance;
themeMode: ThemeMode;
Expand Down Expand Up @@ -54,4 +58,6 @@ export interface FullToken extends AntdToken, CustomToken {}

export interface Theme extends FullToken, ThemeContextState {
stylish: FullStylish;

prefixCls: string;
}

0 comments on commit 33d23b4

Please sign in to comment.