-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Batch func to support React 18.3 findDOMNode (#537)
* feat: support proxyObject * chore: findDOMNode support nativeElement
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
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
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,25 @@ | ||
/** | ||
* Proxy object if environment supported | ||
*/ | ||
export default function proxyObject< | ||
Obj extends object, | ||
ExtendObj extends object | ||
>(obj: Obj, extendProps: ExtendObj): Obj & ExtendObj { | ||
if (typeof Proxy !== 'undefined') { | ||
return new Proxy(obj, { | ||
get(target, prop) { | ||
if (extendProps[prop]) { | ||
return extendProps[prop]; | ||
} | ||
|
||
// Proxy origin property | ||
const originProp = (target as any)[prop]; | ||
return typeof originProp === 'function' | ||
? originProp.bind(target) | ||
: originProp; | ||
}, | ||
}) as Obj & ExtendObj; | ||
} | ||
|
||
return obj as Obj & ExtendObj; | ||
} |
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,15 @@ | ||
import proxyObject from '../src/proxyObject'; | ||
|
||
describe('proxyObject', () => { | ||
it('work', () => { | ||
const div = document.createElement('div'); | ||
div.innerHTML = '<a>noop</a>'; | ||
const a = div.firstChild as HTMLAnchorElement; | ||
|
||
const proxyA = proxyObject(a, { | ||
bamboo: 'little', | ||
}); | ||
|
||
expect(proxyA.bamboo).toBe('little'); | ||
}); | ||
}); |