Skip to content

Commit

Permalink
[js] Add React.Ref support to captureRef
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed May 8, 2019
1 parent 4a01cbf commit 5c58b2d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

declare module 'react-native-view-shot' {
import { Component, ReactInstance } from 'react'
import { Component, ReactInstance, RefObject } from 'react'
import { ViewStyle } from 'react-native'

export interface CaptureOptions {
Expand Down Expand Up @@ -81,11 +81,11 @@ declare module 'react-native-view-shot' {
/**
* lower level imperative API
*
* @param {React.ReactInstance} viewRef
* @param {React.ReactInstance | RefObject} viewRef
* @param {"react-native-view-shot".CaptureOptions} options
* @return {Promise<string>} Returns a Promise of the image URI.
*/
export function captureRef(viewRef: ReactInstance, options?: CaptureOptions): Promise<string>
export function captureRef<T>(viewRef: ReactInstance | RefObject<T>, options?: CaptureOptions): Promise<string>

/**
* This method release a previously captured uri. For tmpfile it will clean them out, for other result types it
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { Component } from "react";
import { View, NativeModules, Platform, findNodeHandle } from "react-native";
const { RNViewShot } = NativeModules;

import type { Element, ElementRef } from 'react';
import type { Element, ElementRef, ElementType, Ref } from 'react';
import type { ViewStyleProp } from 'StyleSheet';
import type { LayoutEvent } from 'CoreEventTypes';

Expand Down Expand Up @@ -88,10 +88,13 @@ function validateOptions(
return { options, errors };
}

export function captureRef(
view: number | ?View,
export function captureRef<T: ElementType>(
view: number | ?View | Ref<T>,
optionsObject?: Object
): Promise<string> {
if (view && typeof view === "object" && "current" in view && view.current) { // React.RefObject
view = view.current;
}
if (typeof view !== "number") {
const node = findNodeHandle(view);
if (!node)
Expand Down

0 comments on commit 5c58b2d

Please sign in to comment.