|
| 1 | +// @flow |
| 2 | + |
| 3 | +import React, { PureComponent } from 'react'; |
| 4 | +import { Easing, Keyboard } from 'react-native'; |
| 5 | +import Modal from '@bam.tech/react-native-modalbox'; |
| 6 | +import RootSiblings from '@bam.tech/react-native-root-siblings'; |
| 7 | + |
| 8 | +type _Props = { |
| 9 | + style: Object, |
| 10 | + easingAnimation: () => void, |
| 11 | + children: any, |
| 12 | +}; |
| 13 | + |
| 14 | +const renderModal = (props: _Props, open: ?boolean) => ( |
| 15 | + <Modal |
| 16 | + backdrop={false} |
| 17 | + position="bottom" |
| 18 | + moveAboveKeyboard={false} |
| 19 | + isOpen={open} |
| 20 | + style={[{ height: 216, backgroundColor: 'rgb(200, 203, 211)' }, props.style]} |
| 21 | + easing={props.easingAnimation} |
| 22 | + > |
| 23 | + {props.children} |
| 24 | + </Modal> |
| 25 | +); |
| 26 | + |
| 27 | +let keyboardModalInstance = null; |
| 28 | +let displayedKeyboardComponent = null; |
| 29 | +let keyboardModalCount = 0; |
| 30 | +let keyboardDidShowListener = null; |
| 31 | +let currentProps; |
| 32 | + |
| 33 | +const updateKeyboardModalComponent = (props: _Props, open: ?boolean) => { |
| 34 | + if (open) currentProps = props; |
| 35 | + if (keyboardModalInstance) keyboardModalInstance.update(renderModal(props, open)); |
| 36 | +}; |
| 37 | + |
| 38 | +const open = (keyboardComponent: any) => { |
| 39 | + if (displayedKeyboardComponent) displayedKeyboardComponent.displayed = false; |
| 40 | + |
| 41 | + displayedKeyboardComponent = keyboardComponent; |
| 42 | + displayedKeyboardComponent.displayed = true; |
| 43 | + |
| 44 | + updateKeyboardModalComponent(keyboardComponent.props, true); |
| 45 | +}; |
| 46 | + |
| 47 | +const keyboardDidShow = () => updateKeyboardModalComponent(currentProps, false); |
| 48 | + |
| 49 | +const createKeyboardModalComponent = (props: _Props) => { |
| 50 | + keyboardModalCount += 1; |
| 51 | + |
| 52 | + if (keyboardModalCount > 1) return; |
| 53 | + |
| 54 | + currentProps = props; |
| 55 | + |
| 56 | + keyboardModalInstance = new RootSiblings(renderModal(props)); |
| 57 | + debugger; |
| 58 | + keyboardDidShowListener = Keyboard.addListener('keyboardWillShow', keyboardDidShow); |
| 59 | +}; |
| 60 | + |
| 61 | +const removeKeyboardModalComponent = () => { |
| 62 | + keyboardModalCount -= 1; |
| 63 | + |
| 64 | + if (keyboardModalCount === 0) { |
| 65 | + if (keyboardDidShowListener) keyboardDidShowListener.remove(); |
| 66 | + if (keyboardModalInstance) keyboardModalInstance.remove(); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +export default class KeyboardModal extends PureComponent { |
| 71 | + static dismiss() { |
| 72 | + if (keyboardModalCount > 0) { |
| 73 | + updateKeyboardModalComponent(currentProps, false); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static defaultProps = { |
| 78 | + easingAnimation: Easing.ease, |
| 79 | + }; |
| 80 | + |
| 81 | + componentWillMount() { |
| 82 | + createKeyboardModalComponent(this.props); |
| 83 | + } |
| 84 | + |
| 85 | + componentWillReceiveProps(nextProps: _Props) { |
| 86 | + if (this.displayed) { |
| 87 | + updateKeyboardModalComponent(nextProps); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + componentWillUnmount() { |
| 92 | + removeKeyboardModalComponent(); |
| 93 | + } |
| 94 | + |
| 95 | + displayed: boolean = false; |
| 96 | + |
| 97 | + open() { |
| 98 | + this.displayed = true; |
| 99 | + Keyboard.dismiss(); |
| 100 | + open(this); |
| 101 | + } |
| 102 | + |
| 103 | + close() { |
| 104 | + this.displayed = false; |
| 105 | + updateKeyboardModalComponent(this.props, false); |
| 106 | + } |
| 107 | + |
| 108 | + render() { |
| 109 | + return null; |
| 110 | + } |
| 111 | +} |
0 commit comments