Pārlūkot izejas kodu

[js] Add React.Ref support to captureRef

Stanisław Chmiela 5 gadus atpakaļ
vecāks
revīzija
5c58b2daf4
2 mainītis faili ar 9 papildinājumiem un 6 dzēšanām
  1. 3
    3
      src/index.d.ts
  2. 6
    3
      src/index.js

+ 3
- 3
src/index.d.ts Parādīt failu

@@ -9,7 +9,7 @@
9 9
  */
10 10
 
11 11
 declare module 'react-native-view-shot' {
12
-    import { Component, ReactInstance } from 'react'
12
+    import { Component, ReactInstance, RefObject } from 'react'
13 13
     import { ViewStyle } from 'react-native'
14 14
 
15 15
     export interface CaptureOptions {
@@ -81,11 +81,11 @@ declare module 'react-native-view-shot' {
81 81
     /**
82 82
      * lower level imperative API
83 83
      *
84
-     * @param {React.ReactInstance} viewRef
84
+     * @param {React.ReactInstance | RefObject} viewRef
85 85
      * @param {"react-native-view-shot".CaptureOptions} options
86 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 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 Parādīt failu

@@ -3,7 +3,7 @@ import React, { Component } from "react";
3 3
 import { View, NativeModules, Platform, findNodeHandle } from "react-native";
4 4
 const { RNViewShot } = NativeModules;
5 5
 
6
-import type { Element, ElementRef } from 'react';
6
+import type { Element, ElementRef, ElementType, Ref } from 'react';
7 7
 import type { ViewStyleProp } from 'StyleSheet';
8 8
 import type { LayoutEvent } from 'CoreEventTypes';
9 9
 
@@ -88,10 +88,13 @@ function validateOptions(
88 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 93
   optionsObject?: Object
94 94
 ): Promise<string> {
95
+  if (view && typeof view === "object" && "current" in view && view.current) { // React.RefObject
96
+    view = view.current;
97
+  }
95 98
   if (typeof view !== "number") {
96 99
     const node = findNodeHandle(view);
97 100
     if (!node)