/
Radio.tsx
65 lines (58 loc) · 1.64 KB
/
Radio.tsx
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
import React from 'react';
import {TouchableWithoutFeedback, Image} from 'react-native';
import RadioProps from './RadioPropsType';
import styles from './style/index';
export default class Radio extends React.Component<RadioProps, any> {
static RadioItem: any;
constructor(props: RadioProps, context: any) {
super(props, context);
let checked;
if (props.checked !== null && props.checked !== undefined) {
checked = !!props.checked;
} else {
checked = !!props.defaultChecked;
}
this.state = {
checked: checked,
};
}
componentWillReceiveProps(nextProps: RadioProps, nextContext: any): void {
if (nextProps.checked !== null && nextProps.checked !== undefined) {
const oldChecked = this.state.checked;
if (nextProps.checked === oldChecked) {
return;
}
this.setState({
checked: !!nextProps.checked,
});
}
}
handleClick = () => {
if (this.props.disabled) {
return;
}
if (this.props.checked === null || this.props.checked === undefined) {
this.setState({
checked: true,
});
}
if (this.props.onChange) {
this.props.onChange(true);
}
};
render(): JSX.Element {
let {style, disabled} = this.props;
let checked = this.state.checked;
let imgSrc = null;
if (checked) {
if (disabled) {
imgSrc = require('./image/checked_disable.png');
} else {
imgSrc = require('./image/checked.png');
}
}
return (<TouchableWithoutFeedback onPress={this.handleClick}>
<Image source={imgSrc} style={[styles.icon, style]} />
</TouchableWithoutFeedback>);
}
}