diff --git a/src/framework/theme/component/modal/modalPanel.component.tsx b/src/framework/theme/component/modal/modalPanel.component.tsx index 4657d61d5..54a559a13 100644 --- a/src/framework/theme/component/modal/modalPanel.component.tsx +++ b/src/framework/theme/component/modal/modalPanel.component.tsx @@ -4,53 +4,63 @@ import { StyleSheet, ViewProps, } from 'react-native'; -import { ModalService } from '../../service'; +import { + ModalPresenting, + ModalService, +} from '../../service'; import { Modal } from '../../../ui/modal/modal.component'; +import { ModalComponentCloseProps } from '@kitten/theme'; export interface ModalPanelProps { children: React.ReactElement | React.ReactElement[]; } interface ModalPanelState { - dialogComponents: Map>; - backdropValues: Map; + components: Map>; + backdrops: Map; } -export class ModalPanel extends React.Component { +export class ModalPanel extends React.Component implements ModalPresenting { public state: ModalPanelState = { - dialogComponents: new Map(), - backdropValues: new Map(), + components: new Map(), + backdrops: new Map(), }; public componentDidMount(): void { - ModalService.setComponent(this); + ModalService.mount(this); } public componentWillUnmount(): void { - ModalService.setComponent(null); + ModalService.unmount(); } - public onCloseModal = (identifier: string) => { - const components: Map> = this.state.dialogComponents; + public hide = (identifier: string): void => { + const component: React.ReactElement = this.state.components + .get(identifier); + if (component) { + component.props.onRequestClose && component.props.onRequestClose(); + } + const components: Map> = this.state.components; components.delete(identifier); - const backdropValues: Map = this.state.backdropValues; - backdropValues.delete(identifier); + const backdrops: Map = this.state.backdrops; + backdrops.delete(identifier); this.setState({ - dialogComponents: components, - backdropValues: backdropValues, + components: components, + backdrops: backdrops, }); }; - public showDialog(dialogComponent: React.ReactElement, closeOnBackDrop: boolean): void { + public show(dialogComponent: React.ReactElement, closeOnBackDrop: boolean): string { const key: string = this.generateUniqueComponentKey(); - const componentsMap: Map> = this.state.dialogComponents + const componentsMap: Map> = this.state.components .set(key, dialogComponent); - const backdropsMap: Map = this.state.backdropValues.set(key, closeOnBackDrop); + const backdrops: Map = this.state.backdrops.set(key, closeOnBackDrop); this.setState({ - dialogComponents: componentsMap, - backdropValues: backdropsMap, + components: componentsMap, + backdrops: backdrops, }); + return key; } private generateUniqueComponentKey = (): string => { @@ -58,21 +68,22 @@ export class ModalPanel extends React.Component, index: number) { - const allModalKeys: string[] = Array.from(this.state.dialogComponents.keys()); + const allModalKeys: string[] = Array.from(this.state.components.keys()); const identifier: string = allModalKeys - .find(item => this.state.dialogComponents.get(item) === modal); - const closeOnBackdrop: boolean = this.state.backdropValues.get(identifier); + .find(item => this.state.components.get(item) === modal); + const closeOnBackdrop: boolean = this.state.backdrops.get(identifier); return ( {modal} @@ -80,7 +91,7 @@ export class ModalPanel extends React.Component, i: number) => this.renderModal(component, i)); } diff --git a/src/framework/theme/component/modal/modalPanel.spec.tsx b/src/framework/theme/component/modal/modalPanel.spec.tsx index 40611db6f..360565b38 100644 --- a/src/framework/theme/component/modal/modalPanel.spec.tsx +++ b/src/framework/theme/component/modal/modalPanel.spec.tsx @@ -19,7 +19,8 @@ jest.useFakeTimers(); describe('@modal panel checks', () => { const showModalTestId: string = '@modal/show'; - const hideModalTestId: string = '@modal/hide'; + const hideModalTestIdInner: string = '@modal/hide-inner'; + const hideModalTestIdOuter: string = '@modal/hide-outer'; interface HooksProps { componentDidMount?: () => void; @@ -28,19 +29,29 @@ describe('@modal panel checks', () => { class ModalPanelTest extends React.Component { - componentDidMount(): void { - this.props.componentDidMount && this.props.componentDidMount(); + private modalId: string = ''; + + public componentDidMount(): void { + if (this.props.componentDidMount) { + this.props.componentDidMount(); + } + } + + public componentWillUnmount() { + if (this.props.componentWillUnmount) { + this.props.componentWillUnmount(); + } } - componentWillUnmount() { - this.props.componentWillUnmount && this.props.componentWillUnmount(); + public showModal() { + this.modalId = ModalService.show( 1}/>, true); } - showModal() { - ModalService.showDialog(, true); + public hideModal() { + ModalService.hide(this.modalId); } - render() { + public render() { return ( @@ -51,6 +62,11 @@ describe('@modal panel checks', () => { onPress={() => this.showModal()} testID={showModalTestId} /> +