From 5c7809590bc8b14836d227729d5b3035dee7a76a Mon Sep 17 00:00:00 2001 From: Artur Yorsh <10753921+artyorsh@users.noreply.github.com> Date: Tue, 14 Jan 2020 14:29:21 +0300 Subject: [PATCH] fix(components): modal - backdrop behavior --- src/components/theme/modal/modal.service.tsx | 3 +- .../theme/modal/modalPanel.component.tsx | 10 +- .../theme/modal/modalPanel.spec.tsx | 5 - .../theme/modal/modalResolver.component.tsx | 102 +++---------- .../theme/modal/modalResolver.spec.tsx | 14 +- .../theme/modal/modalResolver.spec.tsx.snap | 56 +++---- src/components/ui/measure/measure.spec.ts | 7 + src/components/ui/measure/type.ts | 9 ++ src/components/ui/modal/modal.component.tsx | 143 ++++++------------ src/components/ui/modal/modal.spec.tsx | 24 --- .../overflowMenu/overflowMenu.component.tsx | 3 - .../ui/popover/popover.component.tsx | 8 +- .../ui/tooltip/tooltip.component.tsx | 3 - .../modal/modalWithBackdrop.component.tsx | 1 - 14 files changed, 120 insertions(+), 268 deletions(-) diff --git a/src/components/theme/modal/modal.service.tsx b/src/components/theme/modal/modal.service.tsx index f37d1d24d..943aa058f 100644 --- a/src/components/theme/modal/modal.service.tsx +++ b/src/components/theme/modal/modal.service.tsx @@ -34,7 +34,7 @@ import { * * const showModal = () => { * const contentElement = this.renderModalContentElement(); - * this.modalID = ModalService.show(contentElement, { allowBackdrop: true, onBackdropPress: this.hideModal }); + * this.modalID = ModalService.show(contentElement, { onBackdropPress: this.hideModal }); * }; * * const hideModal = () => { @@ -90,7 +90,6 @@ class ModalServiceType { } export interface ModalPresentingConfig { - allowBackdrop?: boolean; backdropStyle?: StyleProp; onBackdropPress?: () => void; } diff --git a/src/components/theme/modal/modalPanel.component.tsx b/src/components/theme/modal/modalPanel.component.tsx index 1891d0006..c5f9fb9ab 100644 --- a/src/components/theme/modal/modalPanel.component.tsx +++ b/src/components/theme/modal/modalPanel.component.tsx @@ -72,9 +72,7 @@ export class ModalPanel extends React.Component = this.state.components; components.delete(identifier); @@ -93,11 +91,9 @@ export class ModalPanel extends React.Component => { return ( {config.element} diff --git a/src/components/theme/modal/modalPanel.spec.tsx b/src/components/theme/modal/modalPanel.spec.tsx index da7aaffba..fbea528ea 100644 --- a/src/components/theme/modal/modalPanel.spec.tsx +++ b/src/components/theme/modal/modalPanel.spec.tsx @@ -38,7 +38,6 @@ describe('@modal-service: service checks', () => { textTestId={textId(1)} />, { - allowBackdrop: false, onBackdropPress: () => null, }, ); @@ -52,7 +51,6 @@ describe('@modal-service: service checks', () => { textTestId={textId(1)} />, { - allowBackdrop: false, onBackdropPress: () => null, }, ); @@ -64,7 +62,6 @@ describe('@modal-service: service checks', () => { textTestId={textId(2)} />, { - allowBackdrop: false, onBackdropPress: () => null, }, ); @@ -78,7 +75,6 @@ describe('@modal-service: service checks', () => { textTestId={textId(0)} />, { - allowBackdrop: true, onBackdropPress: () => null, }, ); @@ -215,7 +211,6 @@ describe('@modal panel checks', () => { this.modalId = ModalService.show( , { - allowBackdrop: true, onBackdropPress: () => null, }, ); diff --git a/src/components/theme/modal/modalResolver.component.tsx b/src/components/theme/modal/modalResolver.component.tsx index 8d54acc78..4b3895bdc 100644 --- a/src/components/theme/modal/modalResolver.component.tsx +++ b/src/components/theme/modal/modalResolver.component.tsx @@ -6,117 +6,57 @@ import React from 'react'; import { - View, - ViewProps, + StyleProp, StyleSheet, TouchableOpacity, - TouchableOpacityProps, + View, + ViewProps, + ViewStyle, } from 'react-native'; +import { ModalPresentingConfig } from './modal.service'; type ChildElement = React.ReactElement; type ChildrenProp = ChildElement | ChildElement[]; -interface ComponentProps { +export interface ModalResolverProps extends ViewProps, ModalPresentingConfig { visible: boolean; children: ChildrenProp; - allowBackdrop: boolean; + backdropStyle: StyleProp; onBackdropPress: () => void; } -export type ModalResolverProps = ViewProps & ComponentProps; - export class ModalResolver extends React.Component { static defaultProps: Partial = { visible: false, }; - private onBackdropPress = (): void => { - const { allowBackdrop, onBackdropPress } = this.props; - - if (allowBackdrop) { - onBackdropPress(); - } - }; - - private onStartShouldSetResponder = (): boolean => { - return true; - }; - - private onResponderRelease = (): void => { - return; - }; - - private onStartShouldSetResponderCapture = (): boolean => { - return false; - }; - - private renderComponentChild = (source: React.ReactElement): React.ReactElement => { + private renderChildElement = (source: ChildElement): ChildElement => { return React.cloneElement(source, { style: [source.props.style, this.props.style], }); }; - private renderComponentChildren = (source: React.ReactNode): React.ReactElement[] => { - return React.Children.map(source, this.renderComponentChild); + private renderComponentChildren = (source: ChildrenProp): ChildElement[] => { + return React.Children.map(source, this.renderChildElement); }; - private renderWithBackDrop = (component: React.ReactElement): - React.ReactElement => { - - return ( - - {component} - - ); - }; + private renderComponent = (): React.ReactElement => { + const componentChildren = this.renderComponentChildren(this.props.children); - private renderWithoutBackDrop = (component: React.ReactElement): React.ReactElement => { return ( - - - {component} + + + {componentChildren} ); }; - private renderComponent = (): React.ReactElement => { - const { children, allowBackdrop, ...derivedProps } = this.props; - const componentChildren: React.ReactElement[] = this.renderComponentChildren(children); - - const dialog: React.ReactElement = - - {componentChildren} - ; - - return allowBackdrop ? - this.renderWithBackDrop(dialog) : this.renderWithoutBackDrop(dialog); - }; - - public render(): React.ReactElement | null { - return this.props.visible ? this.renderComponent() : null; + public render(): React.ReactElement | undefined { + return this.props.visible && this.renderComponent(); } } - -const styles = StyleSheet.create({ - container: StyleSheet.absoluteFillObject, - notVisibleWrapper: { - position: 'absolute', - width: 0, - height: 0, - }, - contentWrapper: { - alignSelf: 'flex-start', - }, -}); diff --git a/src/components/theme/modal/modalResolver.spec.tsx b/src/components/theme/modal/modalResolver.spec.tsx index 19110d10b..24409d7bf 100644 --- a/src/components/theme/modal/modalResolver.spec.tsx +++ b/src/components/theme/modal/modalResolver.spec.tsx @@ -18,9 +18,7 @@ describe('@modal resolver component checks', () => { it('* modal resolver component renders properly', () => { const modal1: RenderAPI = render( - + Test1 @@ -30,9 +28,7 @@ describe('@modal resolver component checks', () => { ); const modal2: RenderAPI = render( - + Test2 @@ -51,21 +47,17 @@ describe('@modal resolver component checks', () => { it('* modal resolver component props checks', () => { const modalPassingProps = { visible: true, - allowBackdrop: false, }; const modal = ; expect(modal.props.visible).toBe(modalPassingProps.visible); - expect(modal.props.allowBackdrop).toBe(modalPassingProps.allowBackdrop); }); it('* modal resolver backdrop press calling checks', () => { const onBackdropPress = jest.fn(); const component: RenderAPI = render( - + Test1