mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 20:22:19 +08:00
UI Extensions: Allow React.memo()
objects as component extensions (#76098)
fix: support the sandbox with component ui extensions
This commit is contained in:
@ -272,6 +272,18 @@ describe('Plugin Extension Validators', () => {
|
|||||||
expect(isReactComponent(() => <div>Some text</div>)).toBe(true);
|
expect(isReactComponent(() => <div>Some text</div>)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return TRUE if we pass in a component wrapped with React.memo()', () => {
|
||||||
|
const Component = () => <div>Some text</div>;
|
||||||
|
const wrapped = React.memo(() => (
|
||||||
|
<div>
|
||||||
|
<Component />
|
||||||
|
</div>
|
||||||
|
));
|
||||||
|
wrapped.displayName = 'MyComponent';
|
||||||
|
|
||||||
|
expect(isReactComponent(wrapped)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return FALSE if we pass in a valid React component', () => {
|
it('should return FALSE if we pass in a valid React component', () => {
|
||||||
expect(isReactComponent('Foo bar')).toBe(false);
|
expect(isReactComponent('Foo bar')).toBe(false);
|
||||||
expect(isReactComponent(123)).toBe(false);
|
expect(isReactComponent(123)).toBe(false);
|
||||||
|
@ -129,7 +129,14 @@ export function isPromise(value: unknown): value is Promise<unknown> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isReactComponent(component: unknown): component is React.ComponentType {
|
export function isReactComponent(component: unknown): component is React.ComponentType {
|
||||||
|
const hasReactTypeProp = (obj: unknown): obj is { $$typeof: Symbol } =>
|
||||||
|
typeof obj === 'object' && obj !== null && '$$typeof' in obj;
|
||||||
|
|
||||||
|
// The sandbox wraps the plugin components with React.memo.
|
||||||
|
const isReactMemoObject = (obj: unknown): boolean =>
|
||||||
|
hasReactTypeProp(obj) && obj.$$typeof === Symbol.for('react.memo');
|
||||||
|
|
||||||
// We currently don't have any strict runtime-checking for this.
|
// We currently don't have any strict runtime-checking for this.
|
||||||
// (The main reason is that we don't want to start depending on React implementation details.)
|
// (The main reason is that we don't want to start depending on React implementation details.)
|
||||||
return typeof component === 'function';
|
return typeof component === 'function' || isReactMemoObject(component);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user