Skip to content

Commit 2742629

Browse files
author
刘欢
committed
feat: configProvider prefixCls
1 parent c55c5a3 commit 2742629

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/components/config-provider/config-provider.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, { FC, ReactNode, useContext } from 'react'
22
import { Locale } from '../../locales/base'
33
import zhCN from '../../locales/zh-CN'
44

5+
type GetPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => string
6+
57
type Config = {
68
locale: Locale
79
checkList?: {
@@ -42,13 +44,22 @@ type Config = {
4244
searchBar?: {
4345
searchIcon?: ReactNode
4446
}
47+
prefixCls?: string
48+
getPrefixCls: GetPrefixCls
4549
}
50+
export const defaultPrefixCls = 'adm'
4651

4752
export const defaultConfigRef: {
4853
current: Config
4954
} = {
5055
current: {
5156
locale: zhCN,
57+
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => {
58+
if (customizePrefixCls) {
59+
return customizePrefixCls
60+
}
61+
return suffixCls ? `${defaultPrefixCls}-${suffixCls}` : defaultPrefixCls
62+
},
5263
},
5364
}
5465

@@ -70,11 +81,21 @@ export const ConfigProvider: FC<ConfigProviderProps> = props => {
7081
const { children, ...config } = props
7182
const parentConfig = useConfig()
7283

84+
const getPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
85+
if (customizePrefixCls) {
86+
return customizePrefixCls
87+
}
88+
const mergedPrefixCls =
89+
config.prefixCls || parentConfig.prefixCls || defaultPrefixCls
90+
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls
91+
}
92+
7393
return (
7494
<ConfigContext.Provider
7595
value={{
7696
...parentConfig,
7797
...config,
98+
getPrefixCls,
7899
}}
79100
>
80101
{children}

src/components/config-provider/index.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Configure locale messages and custom icons globally.
2929
| navBar | NavBar config | `{ backIcon?: ReactNode }` | - |
3030
| noticeBar | NoticeBar config | `{ icon?: ReactNode, closeIcon?: ReactNode }` | - |
3131
| popup | Popup config | `{ closeIcon?: ReactNode }` | - |
32+
| prefixCls | Set prefix className | string | `adm` |
3233
| result | Result config | `{ successIcon?: ReactNode, errorIcon?: ReactNode, infoIcon?: ReactNode, waitingIcon?: ReactNode, warningIcon?: ReactNode }` | - |
3334
| searchBar | SearchBar config | `{ searchIcon?: ReactNode }` | - |
3435

src/components/config-provider/index.zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
| navBar | NavBar 配置 | `{ backIcon?: ReactNode }` | - |
3030
| noticeBar | NoticeBar 配置 | `{ icon?: ReactNode, closeIcon?: ReactNode }` | - |
3131
| popup | Popup 配置 | `{ closeIcon?: ReactNode }` | - |
32+
| prefixCls | 设置统一样式前缀 | string | `adm` |
3233
| result | Result 配置 | `{ successIcon?: ReactNode, errorIcon?: ReactNode, infoIcon?: ReactNode, waitingIcon?: ReactNode, warningIcon?: ReactNode }` | - |
3334
| searchBar | SearchBar 配置 | `{ searchIcon?: ReactNode }` | - |
3435

src/components/config-provider/tests/config-provider.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('ConfigProvider', () => {
118118
})
119119
})
120120

121-
test('useConfig should only has `locale`', () => {
121+
test('useConfig should only has `locale` and `getPrefixCls`', () => {
122122
let config: ReturnType<typeof useConfig>
123123

124124
const Demo = () => {
@@ -127,6 +127,6 @@ describe('ConfigProvider', () => {
127127
}
128128
render(<Demo />)
129129

130-
expect(Object.keys(config!)).toEqual(['locale'])
130+
expect(Object.keys(config!)).toEqual(['locale', 'getPrefixCls'])
131131
})
132132
})

0 commit comments

Comments
 (0)