mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-29 05:29:37 +08:00
* feat: modal.update() supports functional updating (#27162) * docs: add version tip for modal.update() api
This commit is contained in:
parent
7e782daec0
commit
ab4aeca60a
@ -244,7 +244,7 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('could be update', () => {
|
||||
it('could be update by new config', () => {
|
||||
jest.useFakeTimers();
|
||||
['info', 'success', 'warning', 'error'].forEach(type => {
|
||||
const instance = Modal[type]({
|
||||
@ -273,6 +273,49 @@ describe('Modal.confirm triggers callbacks correctly', () => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('could be update by call function', () => {
|
||||
jest.useFakeTimers();
|
||||
['info', 'success', 'warning', 'error'].forEach(type => {
|
||||
const instance = Modal[type]({
|
||||
title: 'title',
|
||||
okButtonProps: {
|
||||
loading: true,
|
||||
style: {
|
||||
color: 'red',
|
||||
},
|
||||
},
|
||||
});
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
||||
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('title');
|
||||
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].classList).toContain(
|
||||
'ant-btn-loading',
|
||||
);
|
||||
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].style.color).toBe('red');
|
||||
instance.update(prevConfig => ({
|
||||
...prevConfig,
|
||||
okButtonProps: {
|
||||
...prevConfig.okButtonProps,
|
||||
loading: false,
|
||||
},
|
||||
}));
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
expect($$(`.ant-modal-confirm-${type}`)).toHaveLength(1);
|
||||
expect($$('.ant-modal-confirm-title')[0].innerHTML).toBe('title');
|
||||
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].classList).not.toContain(
|
||||
'ant-btn-loading',
|
||||
);
|
||||
expect($$('.ant-modal-confirm-btns .ant-btn-primary')[0].style.color).toBe('red');
|
||||
instance.destroy();
|
||||
jest.runAllTimers();
|
||||
});
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('could be destroy', () => {
|
||||
jest.useFakeTimers();
|
||||
['info', 'success', 'warning', 'error'].forEach(type => {
|
||||
|
@ -14,11 +14,13 @@ function getRootPrefixCls() {
|
||||
return defaultRootPrefixCls;
|
||||
}
|
||||
|
||||
type ConfigUpdate = ModalFuncProps | ((prevConfig: ModalFuncProps) => ModalFuncProps);
|
||||
|
||||
export type ModalFunc = (
|
||||
props: ModalFuncProps,
|
||||
) => {
|
||||
destroy: () => void;
|
||||
update: (newConfig: ModalFuncProps) => void;
|
||||
update: (configUpdate: ConfigUpdate) => void;
|
||||
};
|
||||
|
||||
export interface ModalStaticFunctions {
|
||||
@ -84,11 +86,15 @@ export default function confirm(config: ModalFuncProps) {
|
||||
render(currentConfig);
|
||||
}
|
||||
|
||||
function update(newConfig: ModalFuncProps) {
|
||||
currentConfig = {
|
||||
...currentConfig,
|
||||
...newConfig,
|
||||
};
|
||||
function update(configUpdate: ConfigUpdate) {
|
||||
if (typeof configUpdate === 'function') {
|
||||
currentConfig = configUpdate(currentConfig);
|
||||
} else {
|
||||
currentConfig = {
|
||||
...currentConfig,
|
||||
...configUpdate,
|
||||
};
|
||||
}
|
||||
render(currentConfig);
|
||||
}
|
||||
|
||||
|
@ -97,6 +97,12 @@ modal.update({
|
||||
content: 'Updated content',
|
||||
});
|
||||
|
||||
// on 4.8.0 or above, you can pass a function to update modal
|
||||
modal.update(prevConfig => ({
|
||||
...prevConfig,
|
||||
title: `${prevConfig.title} (New)`,
|
||||
}));
|
||||
|
||||
modal.destroy();
|
||||
```
|
||||
|
||||
|
@ -100,6 +100,12 @@ modal.update({
|
||||
content: '修改的内容',
|
||||
});
|
||||
|
||||
// 在 4.8.0 或更高版本中,可以通过传入函数的方式更新弹窗
|
||||
modal.update(prevConfig => ({
|
||||
...prevConfig,
|
||||
title: `${prevConfig.title}(新)`,
|
||||
}));
|
||||
|
||||
modal.destroy();
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user