Browse Source

[js] Add React.Ref support to captureRef

Stanisław Chmiela 5 years ago
parent
commit
5c58b2daf4
2 changed files with 9 additions and 6 deletions
  1. 3
    3
      src/index.d.ts
  2. 6
    3
      src/index.js

+ 3
- 3
src/index.d.ts View File

9
  */
9
  */
10
 
10
 
11
 declare module 'react-native-view-shot' {
11
 declare module 'react-native-view-shot' {
12
-    import { Component, ReactInstance } from 'react'
12
+    import { Component, ReactInstance, RefObject } from 'react'
13
     import { ViewStyle } from 'react-native'
13
     import { ViewStyle } from 'react-native'
14
 
14
 
15
     export interface CaptureOptions {
15
     export interface CaptureOptions {
81
     /**
81
     /**
82
      * lower level imperative API
82
      * lower level imperative API
83
      *
83
      *
84
-     * @param {React.ReactInstance} viewRef
84
+     * @param {React.ReactInstance | RefObject} viewRef
85
      * @param {"react-native-view-shot".CaptureOptions} options
85
      * @param {"react-native-view-shot".CaptureOptions} options
86
      * @return {Promise<string>} Returns a Promise of the image URI.
86
      * @return {Promise<string>} Returns a Promise of the image URI.
87
      */
87
      */
88
-    export function captureRef(viewRef: ReactInstance, options?: CaptureOptions): Promise<string>
88
+    export function captureRef<T>(viewRef: ReactInstance | RefObject<T>, options?: CaptureOptions): Promise<string>
89
 
89
 
90
     /**
90
     /**
91
      * This method release a previously captured uri. For tmpfile it will clean them out, for other result types it
91
      * This method release a previously captured uri. For tmpfile it will clean them out, for other result types it

+ 6
- 3
src/index.js View File

3
 import { View, NativeModules, Platform, findNodeHandle } from "react-native";
3
 import { View, NativeModules, Platform, findNodeHandle } from "react-native";
4
 const { RNViewShot } = NativeModules;
4
 const { RNViewShot } = NativeModules;
5
 
5
 
6
-import type { Element, ElementRef } from 'react';
6
+import type { Element, ElementRef, ElementType, Ref } from 'react';
7
 import type { ViewStyleProp } from 'StyleSheet';
7
 import type { ViewStyleProp } from 'StyleSheet';
8
 import type { LayoutEvent } from 'CoreEventTypes';
8
 import type { LayoutEvent } from 'CoreEventTypes';
9
 
9
 
88
   return { options, errors };
88
   return { options, errors };
89
 }
89
 }
90
 
90
 
91
-export function captureRef(
92
-  view: number | ?View,
91
+export function captureRef<T: ElementType>(
92
+  view: number | ?View | Ref<T>,
93
   optionsObject?: Object
93
   optionsObject?: Object
94
 ): Promise<string> {
94
 ): Promise<string> {
95
+  if (view && typeof view === "object" && "current" in view && view.current) { // React.RefObject
96
+    view = view.current;
97
+  }
95
   if (typeof view !== "number") {
98
   if (typeof view !== "number") {
96
     const node = findNodeHandle(view);
99
     const node = findNodeHandle(view);
97
     if (!node)
100
     if (!node)