Created
January 9, 2020 10:24
-
-
Save shirakaba/5f0e10eabaf317acd3dace4175039252 to your computer and use it in GitHub Desktop.
nativescript-IQKeyboardManager: PreviousNextView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as console from "react-nativescript/dist/shared/Logger"; | |
import * as React from "react"; | |
import { PropsWithoutForwardedRef } from "react-nativescript/dist/shared/NativeScriptComponentTypings"; | |
import { PreviousNextView as NativeScriptPreviousNextView, EventData } from "@nstudio/nativescript-camera-plus"; | |
import { _ContentView, ContentViewComponentProps } from "react-nativescript/dist/components/ContentView"; | |
import { updateListener } from "react-nativescript/dist/client/EventHandling"; | |
import { Container, HostContext, Instance } from "react-nativescript/dist/shared/HostConfigTypes"; | |
import { register } from "react-nativescript/dist/client/ElementRegistry"; | |
const elementKey: string = "previousNextView"; | |
register( | |
elementKey, | |
( | |
props: Props, | |
rootContainerInstance: Container, | |
hostContext: HostContext, | |
) => { | |
return new NativeScriptPreviousNextView() as Instance; | |
} | |
); | |
interface Props { | |
} | |
type PreviousNextViewProps = {}; // No different from ContentView | |
export type PreviousNextViewComponentProps< | |
E extends NativeScriptPreviousNextView = NativeScriptPreviousNextView | |
> = Props & Partial<PreviousNextViewProps> & ContentViewComponentProps<E>; | |
/** | |
* A React wrapper around the PreviousNextView component. | |
*/ | |
class _PreviousNextView< | |
P extends PreviousNextViewComponentProps<E>, | |
S extends {}, | |
E extends NativeScriptPreviousNextView = NativeScriptPreviousNextView | |
> extends _ContentView<P, S, E> { | |
/** | |
* @param attach true: attach; false: detach; null: update | |
*/ | |
protected updateListeners(node: E, attach: boolean | null, nextProps?: P): void { | |
super.updateListeners(node, attach, nextProps); | |
if(attach === null){ | |
updateListener(node, NativeScriptPreviousNextView.errorEvent, this.props.onErrorEvent, nextProps.onErrorEvent); | |
updateListener(node, NativeScriptPreviousNextView.photoCapturedEvent, this.props.onPhotoCapturedEvent, nextProps.onPhotoCapturedEvent); | |
updateListener(node, NativeScriptPreviousNextView.toggleCameraEvent, this.props.onToggleCameraEvent, nextProps.onToggleCameraEvent); | |
updateListener(node, NativeScriptPreviousNextView.imagesSelectedEvent, this.props.onImagesSelectedEvent, nextProps.onImagesSelectedEvent); | |
updateListener(node, NativeScriptPreviousNextView.videoRecordingStartedEvent, this.props.onVideoRecordingStartedEvent, nextProps.onVideoRecordingStartedEvent); | |
updateListener(node, NativeScriptPreviousNextView.videoRecordingFinishedEvent, this.props.onVideoRecordingFinishedEvent, nextProps.onVideoRecordingFinishedEvent); | |
updateListener(node, NativeScriptPreviousNextView.videoRecordingReadyEvent, this.props.onVideoRecordingReadyEvent, nextProps.onVideoRecordingReadyEvent); | |
updateListener(node, NativeScriptPreviousNextView.confirmScreenShownEvent, this.props.onConfirmScreenShownEvent, nextProps.onConfirmScreenShownEvent); | |
updateListener(node, NativeScriptPreviousNextView.confirmScreenDismissedEvent, this.props.onConfirmScreenDismissedEvent, nextProps.onConfirmScreenDismissedEvent); | |
} else { | |
const method = (attach ? node.on : node.off).bind(node); | |
if(this.props.errorEvent) method(NativeScriptPreviousNextView.errorEvent, this.props.onErrorEvent); | |
if(this.props.photoCapturedEvent) method(NativeScriptPreviousNextView.photoCapturedEvent, this.props.onPhotoCapturedEvent); | |
if(this.props.toggleCameraEvent) method(NativeScriptPreviousNextView.toggleCameraEvent, this.props.onToggleCameraEvent); | |
if(this.props.imagesSelectedEvent) method(NativeScriptPreviousNextView.imagesSelectedEvent, this.props.onImagesSelectedEvent); | |
if(this.props.videoRecordingStartedEvent) method(NativeScriptPreviousNextView.videoRecordingStartedEvent, this.props.onVideoRecordingStartedEvent); | |
if(this.props.videoRecordingFinishedEvent) method(NativeScriptPreviousNextView.videoRecordingFinishedEvent, this.props.onVideoRecordingFinishedEvent); | |
if(this.props.videoRecordingReadyEvent) method(NativeScriptPreviousNextView.videoRecordingReadyEvent, this.props.onVideoRecordingReadyEvent); | |
if(this.props.confirmScreenShownEvent) method(NativeScriptPreviousNextView.confirmScreenShownEvent, this.props.onConfirmScreenShownEvent); | |
if(this.props.confirmScreenDismissedEvent) method(NativeScriptPreviousNextView.confirmScreenDismissedEvent, this.props.onConfirmScreenDismissedEvent); | |
} | |
} | |
render(): React.ReactNode { | |
const { | |
forwardedRef, | |
onErrorEvent, | |
onPhotoCapturedEvent, | |
onToggleCameraEvent, | |
onImagesSelectedEvent, | |
onVideoRecordingStartedEvent, | |
onVideoRecordingFinishedEvent, | |
onVideoRecordingReadyEvent, | |
onConfirmScreenShownEvent, | |
onConfirmScreenDismissedEvent, | |
onLoaded, | |
onUnloaded, | |
onAndroidBackPressed, | |
onShowingModally, | |
onShownModally, | |
onTap, | |
onDoubleTap, | |
onPinch, | |
onPan, | |
onSwipe, | |
onRotation, | |
onLongPress, | |
onTouch, | |
onPropertyChange, | |
children, | |
...rest | |
} = this.props; | |
return React.createElement( | |
elementKey, | |
{ | |
...rest, | |
ref: forwardedRef || this.myRef, | |
}, | |
children | |
); | |
} | |
} | |
type OwnPropsWithoutForwardedRef = PropsWithoutForwardedRef<PreviousNextViewComponentProps<NativeScriptPreviousNextView>>; | |
export const $PreviousNextView: React.ComponentType< | |
OwnPropsWithoutForwardedRef & React.ClassAttributes<NativeScriptPreviousNextView> | |
> = React.forwardRef<NativeScriptPreviousNextView, OwnPropsWithoutForwardedRef>( | |
(props: React.PropsWithChildren<OwnPropsWithoutForwardedRef>, ref: React.RefObject<NativeScriptPreviousNextView>) => { | |
const { children, ...rest } = props; | |
return React.createElement( | |
_PreviousNextView, | |
{ | |
...rest, | |
forwardedRef: ref, | |
}, | |
children | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment