feat(react): Initial implementations of controller required elements. (#16817)

This commit is contained in:
Josh Thomas
2018-12-19 10:27:11 -06:00
committed by GitHub
parent 45b2e5c577
commit e30c5f1756
15 changed files with 257 additions and 141 deletions

View File

@ -0,0 +1,50 @@
import React from 'react';
import { attachEventProps } from './utils'
import { ensureElementInBody, dashToPascalCase } from './utils';
export function createControllerComponent<T, E extends HTMLElement, C extends HTMLElement>(tagName: string, controllerTagName: string) {
const displayName = dashToPascalCase(tagName);
type IonicReactInternalProps = {
forwardedRef?: React.RefObject<E>;
children?: React.ReactNode;
show: boolean
}
return class ReactControllerComponent extends React.Component<T & IonicReactInternalProps> {
element: E;
controllerElement: C;
constructor(props: T & IonicReactInternalProps) {
super(props);
}
static get displayName() {
return displayName;
}
async componentDidMount() {
this.controllerElement = ensureElementInBody<C>(controllerTagName);
await (this.controllerElement as any).componentOnReady();
}
async componentDidUpdate(prevProps: T & IonicReactInternalProps) {
if (prevProps.show !== this.props.show && this.props.show === true) {
const { children, show, ...cProps} = this.props as any;
this.element = await (this.controllerElement as any).create(cProps);
await (this.element as any).present();
attachEventProps(this.element, cProps);
}
if (prevProps.show !== this.props.show && this.props.show === false) {
return await (this.element as any).dismiss();
}
}
render(): null {
return null;
}
}
}