forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add ref * feat: table support reference * chore: rename * chore: proxy * test: add test case * chore: fix lint * docs: update desc * docs: update desc * docs: update desc * chore: clean up * chore: fix lint
- Loading branch information
Showing
12 changed files
with
139 additions
and
15 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,47 @@ | ||
// Proxy the dom ref with `{ nativeElement, otherFn }` type | ||
// ref: https://github.com/ant-design/ant-design/discussions/45242 | ||
|
||
import { useImperativeHandle, type Ref } from 'react'; | ||
|
||
function fillProxy( | ||
element: HTMLElement & { _antProxy?: Record<string, any> }, | ||
handler: Record<string, any>, | ||
) { | ||
element._antProxy = element._antProxy || {}; | ||
|
||
Object.keys(handler).forEach((key) => { | ||
if (!(key in element._antProxy!)) { | ||
const ori = (element as any)[key]; | ||
element._antProxy![key] = ori; | ||
|
||
(element as any)[key] = handler[key]; | ||
} | ||
}); | ||
|
||
return element; | ||
} | ||
|
||
export default function useProxyImperativeHandle< | ||
NativeELementType extends HTMLElement, | ||
ReturnRefType extends { nativeElement: NativeELementType }, | ||
>(ref: Ref<any> | undefined, init: () => ReturnRefType) { | ||
return useImperativeHandle(ref, () => { | ||
const refObj = init(); | ||
const { nativeElement } = refObj; | ||
|
||
if (typeof Proxy !== 'undefined') { | ||
return new Proxy(nativeElement, { | ||
get(obj: any, prop: any) { | ||
if ((refObj as any)[prop]) { | ||
return (refObj as any)[prop]; | ||
} | ||
|
||
return Reflect.get(obj, prop); | ||
}, | ||
}); | ||
} | ||
|
||
// Fallback of IE | ||
return fillProxy(nativeElement, refObj); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
|
||
import type { TableRef } from '..'; | ||
import Table from '..'; | ||
import { render } from '../../../tests/utils'; | ||
|
||
describe('Table.IE', () => { | ||
beforeAll(() => { | ||
window.Proxy = undefined as any; | ||
global.Proxy = undefined as any; | ||
}); | ||
|
||
it('support reference', () => { | ||
const tblRef = React.createRef<TableRef>(); | ||
const { container } = render(<Table ref={tblRef} />); | ||
|
||
const wrapDom = container.querySelector('.ant-table-wrapper')!; | ||
|
||
expect(tblRef.current).toBe(wrapDom); | ||
expect(tblRef.current?.nativeElement).toBe(wrapDom); | ||
}); | ||
}); |
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
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
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