import * as React from 'react'; import { Modal, ModalProps } from 'react-bootstrap'; import { Observable } from 'rxjs'; import { Command } from '../../../WebRx'; import { BaseView, BaseViewProps } from '../../React'; import { ModalDialogViewModel } from './ModalDialogViewModel'; export type BootstrapModalProps = Omit2< ModalProps, React.HTMLProps, { onHide: () => void } >; export interface ModalDialogProps extends BootstrapModalProps { modalTitle?: {}; modalBody?: {}; modalFooter?: {}; canClose?: boolean; acceptCommand?: Command | ((ctx: {}) => Command | undefined); acceptCommandParameter?: any; } export interface ModalDialogViewProps extends BaseViewProps>, ModalDialogProps {} export class ModalDialogView extends BaseView< ModalDialogViewProps, ModalDialogViewModel > { public static displayName = 'ModalDialogView'; static defaultProps: Partial = { canClose: false, modalFooter: (view: ModalDialogView) => view.props.children, }; constructor(props: any) { super(props); this.handleKeyDown = this.handleKeyDown.bind(this); } updateOn(viewModel: Readonly>) { return [viewModel.isVisible.changed]; } render() { const { className, props, rest } = this.restProps(x => { const { modalTitle, modalBody, modalFooter, canClose, acceptCommand, acceptCommandParameter, } = x; return { modalTitle, modalBody, modalFooter, canClose, acceptCommand, acceptCommandParameter, }; }); return this.wxr.renderConditional(this.viewModel.isVisible, () => ( x.hide)} {...this.trimProps(rest)} > {this.renderHeader()} {this.renderBody()} {this.renderFooter()} )); } private renderHeader() { const titleContent = this.props.modalTitle && ( {this.props.modalTitle instanceof Function ? this.props.modalTitle(this) : this.props.modalTitle} ); return ( titleContent && this.props.canClose && ( {titleContent} ) ); } private renderBody() { return ( this.props.modalBody && ( {this.props.modalBody instanceof Function ? this.props.modalBody(this) : this.props.modalBody} ) ); } private renderFooter() { if (!this.props.modalFooter) { return false; } const footer = this.props.modalFooter instanceof Function ? this.props.modalFooter(this) : this.props.modalFooter; return footer && {footer}; } private handleKeyDown(e: React.KeyboardEvent) { if (this.props.acceptCommand && e.keyCode === 13) { const ctx = this.viewModel.context.value; const cmd = this.props.acceptCommand instanceof Function ? this.props.acceptCommand(ctx) : this.props.acceptCommand; const param = this.props.acceptCommandParameter instanceof Function ? this.props.acceptCommandParameter(ctx) : this.props.acceptCommandParameter; if (cmd && cmd.canExecuteFor(param)) { return cmd.execute(param); } } return undefined; } }