Skip to content

Commit

Permalink
feat: add useConfig hook to ConfigProvider (ant-design#40215)
Browse files Browse the repository at this point in the history
* feat: add useConfig hook to ConfigProvider

* docs: format config-provide markdown

* doc: add available version

* test: add useConfig test

* docs: add debug

* chore: fix lint error

* fix: getter dead loop

* test: promote useConfig test coverage

* docs(ConfigProvider): update available version
  • Loading branch information
xliez authored and RedJue committed Apr 4, 2023
1 parent 1ac4448 commit ebc877d
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
63 changes: 63 additions & 0 deletions components/config-provider/__tests__/useConfig.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import ConfigProvider from '..';
import Form from '../../form';
import { render } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning';

describe('ConfigProvider.useConfig', () => {
it('useConfig - componentSize', () => {
let size;

const App: React.FC = () => {
const { componentSize } = ConfigProvider.useConfig();
size = componentSize;

return null;
};

render(
<ConfigProvider componentSize='small'>
<App />
</ConfigProvider>,
);

expect(size).toBe('small');
});

it('useConfig - componentDisabled', () => {
let disabled;
const App: React.FC = () => {
const { componentDisabled } = ConfigProvider.useConfig();
disabled = componentDisabled;
return null;
};

render(
<Form disabled>
<App />
</Form>,
);

expect(disabled).toBe(true);
});

it('deprecated SizeContext', () => {
resetWarned();

const App: React.FC = () => {
const { SizeContext } = ConfigProvider;
const ctx = React.useContext(SizeContext);

return <div>{ctx}</div>;
};

const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

render(<App />);

expect(errSpy).toHaveBeenCalledWith(
'Warning: [antd: ConfigProvider] ConfigProvider.SizeContext is deprecated. Please use `ConfigProvider.useConfig().componentSize` instead.',
);
errSpy.mockRestore();
});
});
7 changes: 7 additions & 0 deletions components/config-provider/demo/useConfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## zh-CN

获取父级 `Provider` 的值。如 `DisabledContextProvider``SizeContextProvider`

## en-US

Get the value of the parent `Provider`. Such as `DisabledContextProvider`, `SizeContextProvider`.
53 changes: 53 additions & 0 deletions components/config-provider/demo/useConfig.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useState } from 'react';
import { Checkbox, ConfigProvider, Divider, Form, Input, Radio, Space } from 'antd';
import type { SizeType } from 'antd/es/config-provider/SizeContext';

const ConfigDisplay = () => {
const { componentDisabled, componentSize } = ConfigProvider.useConfig();

return (
<>
<Form.Item label="componentSize value">
<Input value={componentSize} />
</Form.Item>
<Form.Item label="componentDisabled value">
<Input value={String(componentDisabled)} disabled={componentDisabled} />
</Form.Item>
</>
);
};

const App: React.FC = () => {
const [componentSize, setComponentSize] = useState<SizeType>('small');
const [disabled, setDisabled] = useState<boolean>(true);

return (
<div>
<Space>
<Radio.Group
value={componentSize}
onChange={(e) => {
setComponentSize(e.target.value);
}}
>
<Radio.Button value="small">Small</Radio.Button>
<Radio.Button value="middle">Middle</Radio.Button>
<Radio.Button value="large">Large</Radio.Button>
</Radio.Group>
<Checkbox checked={disabled} onChange={(e) => setDisabled(e.target.checked)}>
Form disabled
</Checkbox>
</Space>
<Divider />
<ConfigProvider componentSize={componentSize}>
<div className="example">
<Form disabled={disabled}>
<ConfigDisplay />
</Form>
</div>
</ConfigProvider>
</div>
);
};

export default App;
15 changes: 15 additions & 0 deletions components/config-provider/hooks/useConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useContext } from 'react';
import DisabledContext from '../DisabledContext';
import SizeContext from '../SizeContext';

function useConfig() {
const componentDisabled = useContext(DisabledContext);
const componentSize = useContext(SizeContext);

return {
componentDisabled,
componentSize,
};
}

export default useConfig;
17 changes: 17 additions & 0 deletions components/config-provider/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Some components use dynamic style to support wave effect. You can config `csp` p
<code src="./demo/size.tsx">Component size</code>
<code src="./demo/theme.tsx">Theme</code>
<code src="./demo/prefixCls.tsx" debug>prefixCls</code>
<code src="./demo/useConfig.tsx" debug>useConfig</code>

## API

Expand Down Expand Up @@ -78,6 +79,22 @@ ConfigProvider.config({
});
```

### ConfigProvider.useConfig() `5.3.0+`

Available since `5.2.0`. Get the value of the parent `Provider`. Such as `DisabledContextProvider`, `SizeContextProvider`.

```jsx
const {
componentDisabled, // 5.3.0+
componentSize, // 5.3.0+
} = ConfigProvider.useConfig();
```

| 返回值 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| componentDisabled | antd component disabled state | boolean | - | 5.3.0 |
| componentSize | antd component size state | `small` \| `middle` \| `large` | - | 5.3.0 |

## FAQ

#### How to contribute a new language?
Expand Down
16 changes: 16 additions & 0 deletions components/config-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import useTheme from './hooks/useTheme';
import type { SizeType } from './SizeContext';
import SizeContext, { SizeContextProvider } from './SizeContext';
import useStyle from './style';
import useConfig from './hooks/useConfig';
import warning from '../_util/warning';

export {
type RenderEmptyHandler,
Expand Down Expand Up @@ -311,8 +313,10 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
const ConfigProvider: React.FC<ConfigProviderProps> & {
/** @private internal Usage. do not use in your production */
ConfigContext: typeof ConfigContext;
/** @deprecated Please use `ConfigProvider.useConfig().componentSize` instead */
SizeContext: typeof SizeContext;
config: typeof setGlobalConfig;
useConfig: typeof useConfig;
} = (props) => (
<LocaleReceiver>
{(_, __, legacyLocale) => (
Expand All @@ -328,6 +332,18 @@ const ConfigProvider: React.FC<ConfigProviderProps> & {
ConfigProvider.ConfigContext = ConfigContext;
ConfigProvider.SizeContext = SizeContext;
ConfigProvider.config = setGlobalConfig;
ConfigProvider.useConfig = useConfig;

Object.defineProperty(ConfigProvider, 'SizeContext', {
get: () => {
warning(
false,
'ConfigProvider',
'ConfigProvider.SizeContext is deprecated. Please use `ConfigProvider.useConfig().componentSize` instead.',
);
return SizeContext;
},
});

if (process.env.NODE_ENV !== 'production') {
ConfigProvider.displayName = 'ConfigProvider';
Expand Down
18 changes: 18 additions & 0 deletions components/config-provider/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default Demo;
<code src="./demo/size.tsx">组件尺寸</code>
<code src="./demo/theme.tsx">主题</code>
<code src="./demo/prefixCls.tsx" debug>前缀</code>
<code src="./demo/useConfig.tsx" debug>useConfig</code>

## API

Expand Down Expand Up @@ -79,6 +80,23 @@ ConfigProvider.config({
});
```

### ConfigProvider.useConfig() `5.3.0+`

`5.2.0` 版本后可用。获取父级 `Provider` 的值。如 `DisabledContextProvider``SizeContextProvider`

```jsx
const {
componentDisabled, // 5.3.0+
componentSize, // 5.3.0+
} = ConfigProvider.useConfig();
```

<!-- prettier-ignore -->
| 返回值 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| componentDisabled | antd 组件禁用状态 | boolean | - | 5.3.0 |
| componentSize | antd 组件大小状态 | `small` \| `middle` \| `large` | - | 5.3.0 |

## FAQ

#### 如何增加一个新的语言包?
Expand Down

0 comments on commit ebc877d

Please sign in to comment.