insomnia/packages/insomnia-app/app/ui/components/html-element-wrapper.tsx
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12:00

41 lines
946 B
TypeScript

import React, { Component } from 'react';
import { autoBindMethodsForReact } from 'class-autobind-decorator';
import { AUTOBIND_CFG } from '../../common/constants';
interface Props {
el: HTMLElement;
onUnmount?: () => void;
}
/**
* This component provides an easy way to place a raw DOM node inside a React
* application. This was created to facilitate the layer between UI plugins
* and the Insomnia application.
*/
@autoBindMethodsForReact(AUTOBIND_CFG)
class HtmlElementWrapper extends Component<Props> {
_setRef(n: HTMLDivElement | null | undefined) {
if (!n) {
return;
}
// Add the element directly to the React ref
n.innerHTML = '';
n.appendChild(this.props.el);
}
componentWillUnmount() {
const { onUnmount } = this.props;
if (typeof onUnmount === 'function') {
onUnmount();
}
}
render() {
return <div ref={this._setRef} />;
}
}
export default HtmlElementWrapper;