forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useConfig hook to ConfigProvider (ant-design#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 version
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters