Skip to content

Commit e513679

Browse files
3.0.0
1 parent 6a5c1a0 commit e513679

File tree

3 files changed

+185
-67
lines changed

3 files changed

+185
-67
lines changed

index.js

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
3-
import { TouchableOpacity, View, Text, StyleSheet, Image } from 'react-native'
3+
import { TouchableOpacity, View, Text, StyleSheet, Image, Platform, TouchableNativeFeedback } from 'react-native'
44

55
class Checkbox extends PureComponent {
6-
constructor (...args) {
7-
super(...args)
8-
this.state = {
9-
checked: false
10-
}
11-
12-
this.handleToggleChecked = this.handleToggleChecked.bind(this)
6+
state = {
7+
checked: this.props.checked
138
}
149

15-
componentDidMount () {
16-
this.setState({ checked: this.props.checked })
10+
static Container = Platform.OS === 'ios' ? TouchableOpacity : TouchableNativeFeedback
11+
12+
static defaultProps = {
13+
custom: false,
14+
label: 'Label',
15+
numberOfLabelLines: 1,
16+
labelBefore: false,
17+
checked: false,
18+
checkedImage: require('./checked.png'),
19+
uncheckedImage: require('./unchecked.png'),
20+
checkedComponent: null,
21+
uncheckedComponent: null
1722
}
1823

19-
componentDidUpdate (prevProps, prevState) {
20-
this.setState({ checked: this.props.checked })
24+
static propTypes = {
25+
checkedComponent: PropTypes.element,
26+
uncheckedComponent: PropTypes.element,
27+
checked: PropTypes.bool,
28+
checkboxStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object, Image.propTypes.style]),
29+
containerStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object, View.propTypes.style]),
30+
label: PropTypes.string,
31+
labelBefore: PropTypes.bool,
32+
labelStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
33+
numberOfLabelLines: PropTypes.number,
34+
onChange: PropTypes.func
2135
}
2236

2337
render () {
@@ -36,43 +50,47 @@ class Checkbox extends PureComponent {
3650
} = this.props
3751

3852
return (
39-
<TouchableOpacity
53+
<Checkbox.Container
4054
style={[styles.container, containerStyle]}
4155
onPress={this.handleToggleChecked}
4256
>
43-
{labelBefore ? (
44-
<Label
45-
labelStyle={labelStyle}
46-
numberOfLabelLines={numberOfLabelLines}
47-
label={label}
48-
/>
49-
) : null}
57+
<View style={[styles.container, containerStyle]}>
58+
{labelBefore ? (
59+
<Label
60+
labelStyle={labelStyle}
61+
numberOfLabelLines={numberOfLabelLines}
62+
label={label}
63+
/>
64+
) : null}
5065

51-
{checkedComponent && uncheckedComponent ? (
52-
checked ? (
53-
checkedComponent
66+
{checkedComponent && uncheckedComponent ? (
67+
checked ? (
68+
checkedComponent
69+
) : (
70+
uncheckedComponent
71+
)
5472
) : (
55-
uncheckedComponent
56-
)
57-
) : (
58-
<Image
59-
style={[styles.checkbox, checkboxStyle]}
60-
source={checked ? checkedImage : uncheckedImage}
61-
/>
62-
)}
73+
<Image
74+
style={[styles.checkbox, checkboxStyle]}
75+
source={checked ? checkedImage : uncheckedImage}
76+
/>
77+
)}
78+
79+
{!labelBefore && (
80+
<Label
81+
labelStyle={labelStyle}
82+
numberOfLabelLines={numberOfLabelLines}
83+
label={label}
84+
/>
85+
)}
86+
87+
</View>
6388

64-
{!labelBefore && (
65-
<Label
66-
labelStyle={labelStyle}
67-
numberOfLabelLines={numberOfLabelLines}
68-
label={label}
69-
/>
70-
)}
71-
</TouchableOpacity>
89+
</Checkbox.Container>
7290
)
7391
}
7492

75-
handleToggleChecked () {
93+
handleToggleChecked = () => {
7694
const { label } = this.props
7795
const checked = !this.state.checked
7896

@@ -108,29 +126,4 @@ var styles = StyleSheet.create({
108126
}
109127
})
110128

111-
Checkbox.defaultProps = {
112-
custom: false,
113-
label: 'Label',
114-
numberOfLabelLines: 1,
115-
labelBefore: false,
116-
checked: false,
117-
checkedImage: require('./checked.png'),
118-
uncheckedImage: require('./unchecked.png'),
119-
checkedComponent: null,
120-
uncheckedComponent: null
121-
}
122-
123-
Checkbox.propTypes = {
124-
checkedComponent: PropTypes.element,
125-
uncheckedComponent: PropTypes.element,
126-
checked: PropTypes.bool,
127-
checkboxStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
128-
containerStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
129-
label: PropTypes.string,
130-
labelBefore: PropTypes.bool,
131-
labelStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
132-
numberOfLabelLines: PropTypes.number,
133-
onChange: PropTypes.func
134-
}
135-
136129
export default Checkbox

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
"prop-types": "^15.6.1"
2626
},
2727
"devDependencies": {
28+
"babel-eslint": "^8.2.3",
2829
"standard": "^11.0.1"
30+
},
31+
"peerDependencies": {
32+
"react": "*",
33+
"react-native": "*"
34+
},
35+
"standard": {
36+
"parser": "babel-eslint"
2937
}
3038
}

yarn.lock

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,82 @@
22
# yarn lockfile v1
33

44

5+
"@babel/code-frame@7.0.0-beta.44":
6+
version "7.0.0-beta.44"
7+
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz#2a02643368de80916162be70865c97774f3adbd9"
8+
dependencies:
9+
"@babel/highlight" "7.0.0-beta.44"
10+
11+
"@babel/generator@7.0.0-beta.44":
12+
version "7.0.0-beta.44"
13+
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.44.tgz#c7e67b9b5284afcf69b309b50d7d37f3e5033d42"
14+
dependencies:
15+
"@babel/types" "7.0.0-beta.44"
16+
jsesc "^2.5.1"
17+
lodash "^4.2.0"
18+
source-map "^0.5.0"
19+
trim-right "^1.0.1"
20+
21+
"@babel/helper-function-name@7.0.0-beta.44":
22+
version "7.0.0-beta.44"
23+
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz#e18552aaae2231100a6e485e03854bc3532d44dd"
24+
dependencies:
25+
"@babel/helper-get-function-arity" "7.0.0-beta.44"
26+
"@babel/template" "7.0.0-beta.44"
27+
"@babel/types" "7.0.0-beta.44"
28+
29+
"@babel/helper-get-function-arity@7.0.0-beta.44":
30+
version "7.0.0-beta.44"
31+
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz#d03ca6dd2b9f7b0b1e6b32c56c72836140db3a15"
32+
dependencies:
33+
"@babel/types" "7.0.0-beta.44"
34+
35+
"@babel/helper-split-export-declaration@7.0.0-beta.44":
36+
version "7.0.0-beta.44"
37+
resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz#c0b351735e0fbcb3822c8ad8db4e583b05ebd9dc"
38+
dependencies:
39+
"@babel/types" "7.0.0-beta.44"
40+
41+
"@babel/highlight@7.0.0-beta.44":
42+
version "7.0.0-beta.44"
43+
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.44.tgz#18c94ce543916a80553edcdcf681890b200747d5"
44+
dependencies:
45+
chalk "^2.0.0"
46+
esutils "^2.0.2"
47+
js-tokens "^3.0.0"
48+
49+
"@babel/template@7.0.0-beta.44":
50+
version "7.0.0-beta.44"
51+
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f"
52+
dependencies:
53+
"@babel/code-frame" "7.0.0-beta.44"
54+
"@babel/types" "7.0.0-beta.44"
55+
babylon "7.0.0-beta.44"
56+
lodash "^4.2.0"
57+
58+
"@babel/traverse@7.0.0-beta.44":
59+
version "7.0.0-beta.44"
60+
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.44.tgz#a970a2c45477ad18017e2e465a0606feee0d2966"
61+
dependencies:
62+
"@babel/code-frame" "7.0.0-beta.44"
63+
"@babel/generator" "7.0.0-beta.44"
64+
"@babel/helper-function-name" "7.0.0-beta.44"
65+
"@babel/helper-split-export-declaration" "7.0.0-beta.44"
66+
"@babel/types" "7.0.0-beta.44"
67+
babylon "7.0.0-beta.44"
68+
debug "^3.1.0"
69+
globals "^11.1.0"
70+
invariant "^2.2.0"
71+
lodash "^4.2.0"
72+
73+
"@babel/types@7.0.0-beta.44":
74+
version "7.0.0-beta.44"
75+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.44.tgz#6b1b164591f77dec0a0342aca995f2d046b3a757"
76+
dependencies:
77+
esutils "^2.0.2"
78+
lodash "^4.2.0"
79+
to-fast-properties "^2.0.0"
80+
581
acorn-jsx@^3.0.0:
682
version "3.0.1"
783
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-3.0.1.tgz#afdf9488fb1ecefc8348f6fb22f464e32a58b36b"
@@ -99,6 +175,21 @@ babel-code-frame@^6.22.0:
99175
esutils "^2.0.2"
100176
js-tokens "^3.0.0"
101177

178+
babel-eslint@^8.2.3:
179+
version "8.2.3"
180+
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.3.tgz#1a2e6681cc9bc4473c32899e59915e19cd6733cf"
181+
dependencies:
182+
"@babel/code-frame" "7.0.0-beta.44"
183+
"@babel/traverse" "7.0.0-beta.44"
184+
"@babel/types" "7.0.0-beta.44"
185+
babylon "7.0.0-beta.44"
186+
eslint-scope "~3.7.1"
187+
eslint-visitor-keys "^1.0.0"
188+
189+
babylon@7.0.0-beta.44:
190+
version "7.0.0-beta.44"
191+
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.44.tgz#89159e15e6e30c5096e22d738d8c0af8a0e8ca1d"
192+
102193
balanced-match@^0.4.1:
103194
version "0.4.2"
104195
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"
@@ -377,7 +468,7 @@ eslint-plugin-standard@~3.0.1:
377468
version "3.0.1"
378469
resolved "https://registry.yarnpkg.com/eslint-plugin-standard/-/eslint-plugin-standard-3.0.1.tgz#34d0c915b45edc6f010393c7eef3823b08565cf2"
379470

380-
eslint-scope@^3.7.1:
471+
eslint-scope@^3.7.1, eslint-scope@~3.7.1:
381472
version "3.7.1"
382473
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
383474
dependencies:
@@ -579,7 +670,7 @@ glob@^7.1.2:
579670
once "^1.3.0"
580671
path-is-absolute "^1.0.0"
581672

582-
globals@^11.0.1:
673+
globals@^11.0.1, globals@^11.1.0:
583674
version "11.5.0"
584675
resolved "https://registry.yarnpkg.com/globals/-/globals-11.5.0.tgz#6bc840de6771173b191f13d3a9c94d441ee92642"
585676

@@ -668,6 +759,12 @@ inquirer@^3.0.6:
668759
strip-ansi "^4.0.0"
669760
through "^2.3.6"
670761

762+
invariant@^2.2.0:
763+
version "2.2.4"
764+
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
765+
dependencies:
766+
loose-envify "^1.0.0"
767+
671768
is-arrayish@^0.2.1:
672769
version "0.2.1"
673770
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
@@ -760,6 +857,10 @@ jschardet@^1.4.2:
760857
version "1.5.1"
761858
resolved "https://registry.yarnpkg.com/jschardet/-/jschardet-1.5.1.tgz#c519f629f86b3a5bedba58a88d311309eec097f9"
762859

860+
jsesc@^2.5.1:
861+
version "2.5.1"
862+
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.1.tgz#e421a2a8e20d6b0819df28908f782526b96dd1fe"
863+
763864
json-parse-better-errors@^1.0.1:
764865
version "1.0.2"
765866
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
@@ -824,6 +925,10 @@ lodash@^4.17.4, lodash@^4.3.0:
824925
version "4.17.4"
825926
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
826927

928+
lodash@^4.2.0:
929+
version "4.17.10"
930+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
931+
827932
loose-envify@^1.0.0, loose-envify@^1.3.1:
828933
version "1.3.1"
829934
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.1.tgz#d1a8ad33fa9ce0e713d65fdd0ac8b748d478c848"
@@ -1163,6 +1268,10 @@ slice-ansi@1.0.0:
11631268
dependencies:
11641269
is-fullwidth-code-point "^2.0.0"
11651270

1271+
source-map@^0.5.0:
1272+
version "0.5.7"
1273+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
1274+
11661275
spdx-correct@^3.0.0:
11671276
version "3.0.0"
11681277
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.0.0.tgz#05a5b4d7153a195bc92c3c425b69f3b2a9524c82"
@@ -1278,6 +1387,14 @@ tmp@^0.0.33:
12781387
dependencies:
12791388
os-tmpdir "~1.0.2"
12801389

1390+
to-fast-properties@^2.0.0:
1391+
version "2.0.0"
1392+
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
1393+
1394+
trim-right@^1.0.1:
1395+
version "1.0.1"
1396+
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
1397+
12811398
tryit@^1.0.1:
12821399
version "1.0.3"
12831400
resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb"

0 commit comments

Comments
 (0)