forked from jgrancher/react-native-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
91 lines (74 loc) · 2.12 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React from 'react';
import PropTypes from 'prop-types';
import { NativeModules, requireNativeComponent, View, ViewPropTypes } from 'react-native';
const SketchManager = NativeModules.RNSketchManager || {};
// Fallback when RN version is < 0.44
const viewPropTypes = ViewPropTypes || View.propTypes;
export default class Sketch extends React.Component {
static propTypes = {
fillColor: PropTypes.string,
imageType: PropTypes.oneOf(['jpg', 'png']),
onChange: PropTypes.func,
onClear: PropTypes.func,
strokeColor: PropTypes.string,
strokeThickness: PropTypes.number,
imageData: PropTypes.string,
style: viewPropTypes.style,
};
static defaultProps = {
fillColor: null,
imageType: 'png',
onChange: () => {},
onClear: () => {},
strokeColor: '#000000',
strokeThickness: 1,
imageData: null,
style: null
};
constructor(props) {
super(props);
// Sketch base style properties
this.style = {
flex: 1,
backgroundColor: 'transparent',
};
this.state = {
imageData: props.imageData
}
}
componentWillReceiveProps(nextProps) {
this.setState({
imageData: nextProps.imageData
})
}
onChange = (event) => {
const { imageData } = event.nativeEvent;
this.setState({ imageData });
this.props.onChange(imageData);
};
onClear = () => {
this.setState({ imageData: null });
this.props.onClear();
};
clear = () => SketchManager.clearDrawing();
save = () => {
if (!this.state.imageData) return Promise.reject('No image provided!');
return SketchManager.saveDrawing(this.state.imageData, this.props.imageType);
};
render() {
const { fillColor, strokeColor, strokeThickness, ...props } = this.props;
return (
<RNSketch
{...props}
fillColor={fillColor}
onChange={this.onChange}
onClear={this.onClear}
strokeColor={strokeColor}
strokeThickness={strokeThickness}
style={[this.style, this.props.style]}
imageData={this.state.imageData}
/>
);
}
}
const RNSketch = requireNativeComponent('RNSketch', Sketch);