feat: add useConfig hook to ConfigProvider (#40215)
* 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 versionpull/40726/head
parent
e11ef765a1
commit
0d67bde239
@ -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();
|
||||
});
|
||||
});
|
@ -0,0 +1,7 @@
|
||||
## zh-CN
|
||||
|
||||
获取父级 `Provider` 的值。如 `DisabledContextProvider`、`SizeContextProvider`。
|
||||
|
||||
## en-US
|
||||
|
||||
Get the value of the parent `Provider`. Such as `DisabledContextProvider`, `SizeContextProvider`.
|
@ -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;
|
@ -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;
|
Loading…
Reference in New Issue