UNPKG

1.41 kBJavaScriptView Raw
1import React, {
2 Component, PropTypes,
3} from 'react';
4
5import {
6 TouchableWithoutFeedback, View,
7} from 'react-native';
8
9import Checkbox from '../Checkbox';
10import Text from './stringToText';
11
12class Item extends Component {
13 // 初始化外界参数
14 constructor(props) {
15 super(props);
16 this.componentWillReceiveProps(props);
17 }
18
19 // 将变更同步到 state
20 componentWillReceiveProps = (props) => {
21 this.state = { ...props };
22 }
23
24 // 缩短 state 链路
25 set state(state) {
26 state && Object.keys(state).forEach(key => this[key] = state[key]);
27 }
28
29 picked = false
30
31 render() {
32 const { children, onChange, picked, style } = this;
33 return (
34 <TouchableWithoutFeedback onPress={() => onChange(!picked)}>
35 {Text.isText(children) ? (
36 <View style={[{ flexDirection: 'row', height: 54, alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 20, borderTopWidth: 1, borderTopColor: '#e2e2e2' }, style]}>
37 <Text>{children}</Text>
38 {picked && (
39 <Checkbox checked={picked} />
40 )}
41 </View>
42 ) : (
43 <children.type {...children.props} picked={picked} />
44 )}
45 </TouchableWithoutFeedback>
46 );
47 }
48}
49
50Item.propTypes = {
51 picked: PropTypes.bool,
52 style: View.propTypes.style,
53};
54
55Item.defaultProps = {
56 picked: false,
57 style: null,
58};
59
60export default Item;