Skip to content

Commit 5e6109e

Browse files
committed
Add example sources of 'react-native-signature-capture'.
1 parent 2330555 commit 5e6109e

File tree

4 files changed

+151
-96
lines changed

4 files changed

+151
-96
lines changed

Example/index.android.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,7 @@
1-
/**
2-
* Sample React Native App
3-
* https://github.com/facebook/react-native
4-
* @flow
5-
*/
6-
7-
import React, { Component } from 'react';
81
import {
92
AppRegistry,
10-
StyleSheet,
11-
Text,
12-
View
133
} from 'react-native';
144

15-
class SignatureCaptureExample extends Component {
16-
render() {
17-
return (
18-
<View style={styles.container}>
19-
<Text style={styles.welcome}>
20-
Welcome to React Native!
21-
</Text>
22-
<Text style={styles.instructions}>
23-
To get started, edit index.android.js
24-
</Text>
25-
<Text style={styles.instructions}>
26-
Press Cmd+R to reload,{'\n'}
27-
Cmd+D or shake for dev menu
28-
</Text>
29-
</View>
30-
);
31-
}
32-
}
33-
34-
const styles = StyleSheet.create({
35-
container: {
36-
flex: 1,
37-
justifyContent: 'center',
38-
alignItems: 'center',
39-
backgroundColor: '#F5FCFF',
40-
},
41-
welcome: {
42-
fontSize: 20,
43-
textAlign: 'center',
44-
margin: 10,
45-
},
46-
instructions: {
47-
textAlign: 'center',
48-
color: '#333333',
49-
marginBottom: 5,
50-
},
51-
});
5+
import ExampleApp from './src/ExampleApp';
526

53-
AppRegistry.registerComponent('SignatureCaptureExample', () => SignatureCaptureExample);
7+
AppRegistry.registerComponent('SignatureCaptureExample', () => ExampleApp);

Example/index.ios.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,7 @@
1-
/**
2-
* Sample React Native App
3-
* https://github.com/facebook/react-native
4-
* @flow
5-
*/
6-
7-
import React, { Component } from 'react';
81
import {
92
AppRegistry,
10-
StyleSheet,
11-
Text,
12-
View
133
} from 'react-native';
144

15-
class SignatureCaptureExample extends Component {
16-
render() {
17-
return (
18-
<View style={styles.container}>
19-
<Text style={styles.welcome}>
20-
Welcome to React Native!
21-
</Text>
22-
<Text style={styles.instructions}>
23-
To get started, edit index.ios.js
24-
</Text>
25-
<Text style={styles.instructions}>
26-
Press Cmd+R to reload,{'\n'}
27-
Cmd+D or shake for dev menu
28-
</Text>
29-
</View>
30-
);
31-
}
32-
}
33-
34-
const styles = StyleSheet.create({
35-
container: {
36-
flex: 1,
37-
justifyContent: 'center',
38-
alignItems: 'center',
39-
backgroundColor: '#F5FCFF',
40-
},
41-
welcome: {
42-
fontSize: 20,
43-
textAlign: 'center',
44-
margin: 10,
45-
},
46-
instructions: {
47-
textAlign: 'center',
48-
color: '#333333',
49-
marginBottom: 5,
50-
},
51-
});
5+
import ExampleApp from './src/ExampleApp';
526

53-
AppRegistry.registerComponent('SignatureCaptureExample', () => SignatureCaptureExample);
7+
AppRegistry.registerComponent('SignatureCaptureExample', () => ExampleApp);

Example/src/ExampleApp.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React, { Component } from 'react';
2+
3+
import {
4+
View, StyleSheet, Text, Image, TouchableOpacity
5+
} from 'react-native';
6+
7+
import SignatureView from './SignatureView';
8+
9+
const flexCenter = {
10+
flex: 1,
11+
justifyContent: 'center',
12+
alignItems: 'center',
13+
};
14+
15+
class ExampleApp extends Component {
16+
17+
constructor(props) {
18+
super(props);
19+
20+
this.state = {
21+
data: null
22+
};
23+
}
24+
25+
render() {
26+
const {data} = this.state;
27+
return (
28+
<View style={flexCenter}>
29+
<TouchableOpacity onPress={this._showSignatureView.bind(this)}>
30+
<View style={[flexCenter, {padding: 10}]}>
31+
<Text style={{fontSize: 18, fontWeight: 'bold'}}>
32+
{data ? 'This is a your signature.' : 'Click here.'}
33+
</Text>
34+
<View style={{paddingBottom: 10}} />
35+
{data &&
36+
<View style={{backgroundColor: 'white'}}>
37+
<Image
38+
resizeMode={'contain'}
39+
style={{width: 300, height: 300}}
40+
source={{uri: data}}
41+
/>
42+
</View>
43+
}
44+
</View>
45+
</TouchableOpacity>
46+
<SignatureView
47+
ref={r => this._signatureView = r}
48+
rotateClockwise={!!true}
49+
onSave={this._onSave.bind(this)}
50+
/>
51+
</View>
52+
);
53+
}
54+
55+
_showSignatureView() {
56+
this._signatureView.show(true);
57+
}
58+
59+
_onSave(result) {
60+
const base64String = `data:image/png;base64,${result.encoded}`;
61+
this.setState({data: base64String});
62+
63+
this._signatureView.show(false);
64+
}
65+
}
66+
67+
export default ExampleApp;

Example/src/SignatureView.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, {
2+
Component, PropTypes
3+
} from 'react';
4+
5+
import ReactNative, {
6+
View, Text, Modal, Platform, Alert
7+
} from 'react-native';
8+
9+
import SignatureCapture from 'react-native-signature-capture';
10+
11+
const toolbarHeight = Platform.select({
12+
android: 0,
13+
ios: 22
14+
});
15+
16+
const modalViewStyle = {
17+
paddingTop: toolbarHeight,
18+
flex: 1
19+
};
20+
21+
class SignatureView extends Component {
22+
23+
static propTypes = {
24+
onSave: PropTypes.func
25+
}
26+
27+
constructor(props) {
28+
super(props);
29+
30+
this.state = {
31+
visible: false
32+
};
33+
}
34+
35+
show(display) {
36+
this.setState({visible: display});
37+
}
38+
39+
render() {
40+
const {visible} = this.state;
41+
42+
return (
43+
<Modal transparent={false} visible={visible} onRequestClose={this._onRequreClose.bind(this)}>
44+
<View style={modalViewStyle}>
45+
<View style={{padding: 10, flexDirection: 'row'}}>
46+
<Text onPress={this._onPressClose.bind(this)}>{' x '}</Text>
47+
<View style={{flex: 1, alignItems: 'center'}}>
48+
<Text style={{fontSize: 14}}>Please write your signature.</Text>
49+
</View>
50+
</View>
51+
<SignatureCapture
52+
onDragEvent={this._onDragEvent.bind(this)}
53+
onSaveEvent={this._onSaveEvent.bind(this)}
54+
/>
55+
</View>
56+
</Modal>
57+
);
58+
}
59+
60+
_onPressClose() {
61+
this.show(false);
62+
}
63+
64+
_onRequreClose() {
65+
this.show(false);
66+
}
67+
68+
_onDragEvent() {
69+
// This callback will be called when the user enters signature
70+
console.log("dragged");
71+
}
72+
73+
_onSaveEvent(result) {
74+
//result.encoded - for the base64 encoded png
75+
//result.pathName - for the file path name
76+
this.props.onSave && this.props.onSave(result);
77+
}
78+
}
79+
80+
export default SignatureView;

0 commit comments

Comments
 (0)