From 234b6171fc15084cbd8e54585b66e6f52727967b Mon Sep 17 00:00:00 2001 From: Harald Friessnegger Date: Fri, 27 Mar 2020 18:12:34 +0100 Subject: [PATCH] react-cropper: support ref prop created with useRef hook (#42801) * support useRef hook allows to also pass refs created with the `useRef` hook. see https://github.com/roadmanfong/react-cropper/issues/134#issuecomment-593984770 * use same cropperjs version as current react-cropper release * update interface def and tests --- types/react-cropper/index.d.ts | 9 ++-- types/react-cropper/package.json | 2 +- types/react-cropper/react-cropper-tests.tsx | 56 +++++++++++++++------ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/types/react-cropper/index.d.ts b/types/react-cropper/index.d.ts index 6f2341f492..aaa43884b9 100644 --- a/types/react-cropper/index.d.ts +++ b/types/react-cropper/index.d.ts @@ -1,17 +1,18 @@ -// Type definitions for react-cropper 0.10 +// Type definitions for react-cropper 1.3 // Project: https://github.com/roadmanfong/react-cropper, http://roadmanfong.github.io/react-cropper // Definitions by: Stepan Mikhaylyuk // Walter Barbagallo +// Harald Friessnegger // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// Minimum TypeScript Version: 3.7 import Cropper from 'cropperjs'; import * as React from 'react'; type Omit = Pick>; -export interface ReactCropperProps extends Cropper.Options, Omit, 'data' | 'ref'> { - ref?: string | ((cropper: null | ReactCropper) => any); +interface ReactCropperProps extends Cropper.Options, Omit, 'data' | 'ref'> { + ref?: React.LegacyRef; } interface ReactCropper extends Cropper {} // tslint:disable-line no-empty-interface diff --git a/types/react-cropper/package.json b/types/react-cropper/package.json index a736669a76..9a65fe66e1 100644 --- a/types/react-cropper/package.json +++ b/types/react-cropper/package.json @@ -1,6 +1,6 @@ { "private": true, "dependencies": { - "cropperjs": "^1.3.0" + "cropperjs": "^1.5.5" } } diff --git a/types/react-cropper/react-cropper-tests.tsx b/types/react-cropper/react-cropper-tests.tsx index 47b25fea0f..6e9d1f2d28 100644 --- a/types/react-cropper/react-cropper-tests.tsx +++ b/types/react-cropper/react-cropper-tests.tsx @@ -3,6 +3,9 @@ import Cropper from 'react-cropper'; // If you choose not to use import, you need to assign Cropper to default // var Cropper = require('react-cropper').default +/** + * initializes cropper with string reference + */ class Demo extends React.Component { crop() { // image in dataUrl @@ -12,30 +15,55 @@ class Demo extends React.Component { render() { return ( + crop={this.crop.bind(this)} + /> ); } } +/** + * initializes cropper with ref using useRef hook + */ +const DemoFunctionComponent: React.FunctionComponent = () => { + const cropper = React.useRef(); + + const crop = () => { + console.log(cropper.current?.getData(true)); + }; + + return ( + + ); +}; + function testCropperRef() { - const refIsWorking = { - // $ExpectError el can be null - el.getCroppedCanvas(); - if (el !== null) { - // el is a cropperjs element + const refIsWorking = ( + { + // $ExpectError el can be null el.getCroppedCanvas(); - // el is also a React.Component instance - el.props; - } - }} - />; + if (el !== null) { + // el is a cropperjs element + el.getCroppedCanvas(); + // el is also a React.Component instance + el.props; + } + }} + /> + ); const refIsOptional = ; }