Skip to content

Commit

Permalink
支持最新版本的RN,添加getIsEmpty方法来获取最新是否有过手写
Browse files Browse the repository at this point in the history
  • Loading branch information
杨钊 committed Mar 25, 2024
1 parent 6a36114 commit 1ea5969
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ export default class Demo extends Component {
let base64Str = await this.signaturePad.getDataURL();
} catch(e) {

}

}
}

getIsEmpty = async () => {
//获取是否有手写,true/false表示有结果,undefined表示获取失败
const isEmpty = await this.signaturePad.getIsEmpty();
}
}
```
Expand Down
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import React, {Component} from 'react';
import {PropTypes} from 'prop-types';
import {
ViewPropTypes,
StyleSheet,
StyleSheet
} from 'react-native';
import {WebView} from 'react-native-webview';
import {ViewPropTypes} from 'deprecated-react-native-prop-types';


import htmlContent from './injectedHtml';
Expand Down Expand Up @@ -74,7 +74,7 @@ class SignaturePad extends Component {
// React Native app, the JS is re-injected every time a stroke is drawn.
}

componentWillReceiveProps = (nextProps) => {
UNSAFE_componentWillReceiveProps = (nextProps) => {
if (this.props.useFont && this.state.name !== nextProps.name) {
var escapedName = nextProps.name.replace(/"/, `\\"`);
this.setState({name: escapedName});
Expand Down Expand Up @@ -171,6 +171,10 @@ class SignaturePad extends Component {
this.props.onResult({base64DataUrl});
};

_bridged_getIsEmpty = ({data}) => {
this.tempIsEmptyResolve&&this.tempIsEmptyResolve(data);
};

_bridged_canvasSize = (data) => {
this.props.onChange(data);
// this.setState({base64DataUrl});
Expand All @@ -195,6 +199,9 @@ class SignaturePad extends Component {
case 'getDataURL':
this._bridged_getDataURL(data);
break;
case 'getIsEmpty':
this._bridged_getIsEmpty(data);
break;
}
}

Expand All @@ -212,6 +219,16 @@ class SignaturePad extends Component {
})
}

getIsEmpty = async () => {
return new Promise((resolve,reject)=>{
this.tempIsEmptyResolve=resolve;
this._webview.postMessage(JSON.stringify({ action: 'getIsEmpty' }));
setTimeout(()=>{
resolve(undefined);
},2000);
})
}

render = () => {
return (
<WebView
Expand Down
6 changes: 5 additions & 1 deletion injectedJavaScript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var showSignaturePad = function (signaturePadCanvas, bodyWidth, bodyHeight) {
});
signaturePad.minWidth = ${penMinWidth || 1};
signaturePad.maxWidth = ${penMaxWidth || 4};
if ("${dataURL}") {
if (${dataURL}) {
signaturePad.fromDataURL("${dataURL}");
}
${Platform.OS==='ios'?'window':'document'}.addEventListener('message', function (event) {
Expand All @@ -47,6 +47,10 @@ var showSignaturePad = function (signaturePadCanvas, bodyWidth, bodyHeight) {
var data = signaturePad.toDataURL();
executeNativeFunction('getDataURL',{ base64DataUrl: data, type:'getDataURL' });
return;
} else if(action === 'getIsEmpty') {
var data = signaturePad.isEmpty();
executeNativeFunction('getIsEmpty', { data: data, type:'getIsEmpty' });
return;
}
});
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yz1311/react-native-signature-pad",
"version": "0.1.5",
"version": "0.1.6",
"description": "React Native wrapper around szimek's Canvas based Signature Pad",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,7 +32,8 @@
"peerDependencies": {
"react": "^16.8.4",
"react-native": "^0.58.6",
"react-native-webview": "^5.3.0"
"react-native-webview": "^5.3.0",
"deprecated-react-native-prop-types": "*"
},
"dependencies": {
"prop-types": "^15.6.0"
Expand Down

0 comments on commit 1ea5969

Please sign in to comment.