Skip to content

Commit

Permalink
fix(form): πŸ› DrawerForm cannot get form instance by formRef (ant-desi…
Browse files Browse the repository at this point in the history
…gn#4421)

* fix: πŸ› DrawerForm cannot get form instance by formRef

* fix: πŸ› remove unused variable

* fix: πŸ› add test case for formRef

Co-authored-by: dengqing <qing.deng@goldenpig.com.cn>
  • Loading branch information
Dunqing and dengqing committed Jan 9, 2022
1 parent 15ab36f commit 179ac82
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const waitTime = (time: number = 100) => {

export default () => {
const formRef = useRef<ProFormInstance>();

return (
<DrawerForm<{
name: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/form/src/layouts/DrawerForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ function DrawerForm<T = Record<string, any>>({
[],
);

useImperativeHandle(rest.formRef, () => formRef.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
useImperativeHandle(rest.formRef, () => formRef.current, [shouldRenderFormItems]);

const formDom = (
<div onClick={(e) => e.stopPropagation()}>
Expand Down
28 changes: 28 additions & 0 deletions tests/form/drawerForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,32 @@ describe('DrawerForm', () => {
expect(html.baseElement.querySelector('form')).toBeFalsy();
html.unmount();
});

it('πŸ“¦ drawerForm get formRef when destroyOnClose', async () => {
const ref = React.createRef<any>();

const html = mount(
<DrawerForm
formRef={ref}
drawerProps={{
destroyOnClose: true,
}}
trigger={
<Button id="new" type="primary">
ζ–°ε»Ί
</Button>
}
>
<ProFormText name="name" />
</DrawerForm>,
);

waitForComponentToPaint(html, 200);
expect(ref.current).toBeFalsy();
act(() => {
html.find('button#new').simulate('click');
});
waitForComponentToPaint(html, 200);
expect(ref.current).toBeTruthy();
});
});

0 comments on commit 179ac82

Please sign in to comment.