-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add nativerenderer component * Use native renderer in app and editor frame
- Loading branch information
Showing
6 changed files
with
305 additions
and
41 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
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,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export * from './native_renderer'; |
187 changes: 187 additions & 0 deletions
187
x-pack/plugins/lens/public/native_renderer/native_renderer.test.tsx
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,187 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import { NativeRenderer } from './native_renderer'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
function renderAndTriggerHooks(element: JSX.Element, mountpoint: Element) { | ||
// act takes care of triggering state hooks | ||
act(() => { | ||
render(element, mountpoint); | ||
}); | ||
} | ||
|
||
describe('native_renderer', () => { | ||
let mountpoint: Element; | ||
|
||
beforeEach(() => { | ||
mountpoint = document.createElement('div'); | ||
}); | ||
|
||
afterEach(() => { | ||
mountpoint.remove(); | ||
}); | ||
|
||
it('should render element in container', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
const containerElement = mountpoint.firstElementChild; | ||
expect(renderSpy).toHaveBeenCalledWith(containerElement, testProps); | ||
}); | ||
|
||
it('should not render again if props do not change', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not render again if props do not change shallowly', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={{ ...testProps }} />, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not render again for unchanged callback functions', () => { | ||
const renderSpy = jest.fn(); | ||
const testCallback = () => {}; | ||
const testState = { a: 'abc' }; | ||
|
||
render( | ||
<NativeRenderer | ||
render={renderSpy} | ||
nativeProps={{ state: testState, setState: testCallback }} | ||
/>, | ||
mountpoint | ||
); | ||
render( | ||
<NativeRenderer | ||
render={renderSpy} | ||
nativeProps={{ state: testState, setState: testCallback }} | ||
/>, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should render again once if props change', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={{ a: 'def' }} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={{ a: 'def' }} />, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(2); | ||
const containerElement = mountpoint.firstElementChild; | ||
expect(renderSpy).lastCalledWith(containerElement, { a: 'def' }); | ||
}); | ||
|
||
it('should render again once if props is just a string', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = 'abc'; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks(<NativeRenderer render={renderSpy} nativeProps="def" />, mountpoint); | ||
renderAndTriggerHooks(<NativeRenderer render={renderSpy} nativeProps="def" />, mountpoint); | ||
expect(renderSpy).toHaveBeenCalledTimes(2); | ||
const containerElement = mountpoint.firstElementChild; | ||
expect(renderSpy).lastCalledWith(containerElement, 'def'); | ||
}); | ||
|
||
it('should render again if props are extended', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={{ a: 'abc', b: 'def' }} />, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(2); | ||
const containerElement = mountpoint.firstElementChild; | ||
expect(renderSpy).lastCalledWith(containerElement, { a: 'abc', b: 'def' }); | ||
}); | ||
|
||
it('should render again if props are limited', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc', b: 'def' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={{ a: 'abc' }} />, | ||
mountpoint | ||
); | ||
expect(renderSpy).toHaveBeenCalledTimes(2); | ||
const containerElement = mountpoint.firstElementChild; | ||
expect(renderSpy).lastCalledWith(containerElement, { a: 'abc' }); | ||
}); | ||
|
||
it('should render a div as container', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
const containerElement: Element = mountpoint.firstElementChild!; | ||
expect(containerElement.nodeName).toBe('DIV'); | ||
}); | ||
|
||
it('should render a specified element as container', () => { | ||
const renderSpy = jest.fn(); | ||
const testProps = { a: 'abc' }; | ||
|
||
renderAndTriggerHooks( | ||
<NativeRenderer render={renderSpy} tag="span" nativeProps={testProps} />, | ||
mountpoint | ||
); | ||
const containerElement: Element = mountpoint.firstElementChild!; | ||
expect(containerElement.nodeName).toBe('SPAN'); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
x-pack/plugins/lens/public/native_renderer/native_renderer.tsx
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,80 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useEffect, useRef } from 'react'; | ||
|
||
export interface NativeRendererProps<T> { | ||
render: (domElement: Element, props: T) => void; | ||
nativeProps: T; | ||
tag?: string; | ||
children?: never; | ||
} | ||
|
||
function is(x: unknown, y: unknown) { | ||
return (x === y && (x !== 0 || 1 / (x as number) === 1 / (y as number))) || (x !== x && y !== y); | ||
} | ||
|
||
function isShallowDifferent<T>(objA: T, objB: T): boolean { | ||
if (is(objA, objB)) { | ||
return false; | ||
} | ||
|
||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return true; | ||
} | ||
|
||
const keysA = Object.keys(objA) as Array<keyof T>; | ||
const keysB = Object.keys(objB) as Array<keyof T>; | ||
|
||
if (keysA.length !== keysB.length) { | ||
return true; | ||
} | ||
|
||
for (let i = 0; i < keysA.length; i++) { | ||
if (!window.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* A component which takes care of providing a mountpoint for a generic render | ||
* function which takes an html element and an optional props object. | ||
* It also takes care of calling render again if the props object changes. | ||
* By default the mountpoint element will be a div, this can be changed with the | ||
* `tag` prop. | ||
* | ||
* @param props | ||
*/ | ||
export function NativeRenderer<T>({ render, nativeProps, tag }: NativeRendererProps<T>) { | ||
const elementRef = useRef<Element | null>(null); | ||
const propsRef = useRef<T | null>(null); | ||
|
||
function renderAndUpdate(element: Element) { | ||
elementRef.current = element; | ||
propsRef.current = nativeProps; | ||
render(element, nativeProps); | ||
} | ||
|
||
useEffect( | ||
() => { | ||
if (elementRef.current && isShallowDifferent(propsRef.current, nativeProps)) { | ||
renderAndUpdate(elementRef.current); | ||
} | ||
}, | ||
[nativeProps] | ||
); | ||
|
||
return React.createElement(tag || 'div', { | ||
ref: element => { | ||
if (element && element !== elementRef.current) { | ||
renderAndUpdate(element); | ||
} | ||
}, | ||
}); | ||
} |