-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
126 lines (113 loc) · 3.61 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
ReactNativeCircleCheckbox 0.1.5
https://github.com/paramoshkinandrew/ReactNativeCircleCheckbox
(c) 2016 Andrew Paramoshkin <[email protected]>
ReactNativeCircleCheckbox may be freely distributed under the MIT license.
*/
'use strict';
import * as React from 'react';
import PropTypes from 'prop-types';
import {StyleSheet, Text, TouchableOpacity, View, ViewPropTypes} from 'react-native';
export const LABEL_POSITION = {
RIGHT: 'right',
LEFT: 'left'
};
export default class CircleCheckBox extends React.Component {
static propTypes = {
checked: PropTypes.bool,
label: PropTypes.string,
outerSize: PropTypes.number,
filterSize: PropTypes.number,
innerSize: PropTypes.number,
outerColor: PropTypes.string,
filterColor: PropTypes.string,
innerColor: PropTypes.string,
onToggle: PropTypes.func.isRequired,
labelPosition: PropTypes.oneOf([LABEL_POSITION.RIGHT, LABEL_POSITION.LEFT]),
styleCheckboxContainer: ViewPropTypes.style,
styleLabel: Text.propTypes.style,
};
static defaultProps = {
checked: false,
outerSize: 26,
filterSize: 23,
innerSize: 18,
outerColor: '#FC9527',
filterColor: '#FFF',
innerColor: '#FC9527',
label: '',
labelPosition: LABEL_POSITION.RIGHT,
styleLabel: {}
};
constructor(props) {
super(props);
const outerSize = (parseInt(props.outerSize) < 10 || isNaN(parseInt(props.outerSize))) ? 10 : parseInt(props.outerSize);
const filterSize = (parseInt(props.filterSize) > outerSize + 3 || isNaN(parseInt(props.filterSize))) ? outerSize - 3 : parseInt(props.filterSize);
const innerSize = (parseInt(props.innerSize) > filterSize + 5 || isNaN(parseInt(props.innerSize))) ? filterSize - 5 : parseInt(props.innerSize);
const customStyle = StyleSheet.create({
_circleOuterStyle: {
width: outerSize,
height: outerSize,
borderRadius: outerSize / 2,
backgroundColor: props.outerColor
},
_circleFilterStyle: {
width: filterSize,
height: filterSize,
borderRadius: filterSize / 2,
backgroundColor: props.filterColor
},
_circleInnerStyle: {
width: innerSize,
height: innerSize,
borderRadius: innerSize / 2,
backgroundColor: props.innerColor
}
});
this.state = {
customStyle: customStyle
}
}
render() {
return (
<TouchableOpacity onPress={this._onToggle.bind(this)}>
<View style={[styles.checkBoxContainer, this.props.styleCheckboxContainer]}>
{this._renderLabel(LABEL_POSITION.LEFT)}
<View style={[styles.alignStyle, this.state.customStyle._circleOuterStyle]}>
<View style={[styles.alignStyle, this.state.customStyle._circleFilterStyle]}>
{this._renderInner()}
</View>
</View>
{this._renderLabel(LABEL_POSITION.RIGHT)}
</View>
</TouchableOpacity>
);
}
_onToggle() {
if (this.props.onToggle) {
this.props.onToggle(!this.props.checked);
}
}
_renderInner() {
return this.props.checked ? (<View style={this.state.customStyle._circleInnerStyle} />) : (<View />);
}
_renderLabel(position) {
return ((this.props.label.length > 0) && (position === this.props.labelPosition))
? <Text style={[styles.checkBoxLabel, this.props.styleLabel]}>{this.props.label}</Text>
: <View />
}
}
const styles = StyleSheet.create({
checkBoxContainer: {
flexDirection: 'row',
alignItems: 'center'
},
alignStyle: {
justifyContent: 'center',
alignItems: 'center'
},
checkBoxLabel: {
marginLeft: 5,
marginRight: 5
}
});