|
1 | 1 | import React from 'react';
|
2 | 2 | import {
|
3 |
| - TouchableOpacity, |
4 | 3 | View,
|
5 |
| - StyleSheet, |
| 4 | + TouchableOpacity, |
6 | 5 | TouchableOpacityProps,
|
7 | 6 | GestureResponderEvent,
|
| 7 | + StyleSheet, |
8 | 8 | } from 'react-native';
|
9 | 9 | import {
|
| 10 | + styled, |
10 | 11 | StyledComponentProps,
|
11 | 12 | StyleType,
|
12 | 13 | Interaction,
|
13 | 14 | } from '@kitten/theme';
|
| 15 | +import { |
| 16 | + Text as TextComponent, |
| 17 | + Props as TextProps, |
| 18 | +} from '../text/text.component'; |
14 | 19 |
|
15 | 20 | interface RadioProps {
|
| 21 | + text?: string; |
16 | 22 | checked?: boolean;
|
17 | 23 | status?: string;
|
18 | 24 | size?: string;
|
19 | 25 | onChange?: (selected: boolean) => void;
|
20 | 26 | }
|
21 | 27 |
|
| 28 | +const Text = styled<TextComponent, TextProps>(TextComponent); |
| 29 | + |
22 | 30 | export type Props = RadioProps & StyledComponentProps & TouchableOpacityProps;
|
23 | 31 |
|
24 | 32 | export class Radio extends React.Component<Props> {
|
@@ -46,54 +54,73 @@ export class Radio extends React.Component<Props> {
|
46 | 54 | };
|
47 | 55 |
|
48 | 56 | private getComponentStyle = (style: StyleType): StyleType => {
|
49 |
| - const { select, highlight, ...container } = style; |
| 57 | + const { text, select, highlight, ...container } = style; |
50 | 58 |
|
51 | 59 | return {
|
52 |
| - container: { |
53 |
| - ...container, |
54 |
| - ...styles.container, |
55 |
| - }, |
56 |
| - select: { |
57 |
| - ...select, |
58 |
| - ...styles.select, |
59 |
| - }, |
60 |
| - highlight: { |
61 |
| - ...highlight, |
62 |
| - ...styles.highlight, |
63 |
| - }, |
| 60 | + selectContainer: container, |
| 61 | + select: select, |
| 62 | + highlight: highlight, |
| 63 | + text: text, |
64 | 64 | };
|
65 | 65 | };
|
66 | 66 |
|
| 67 | + private renderTextElement = (style: StyleType): React.ReactElement<TextProps> => { |
| 68 | + const { text } = this.props; |
| 69 | + |
| 70 | + return ( |
| 71 | + <Text style={[style, styles.text]} key={0}>{text}</Text> |
| 72 | + ); |
| 73 | + }; |
| 74 | + |
| 75 | + private renderComponentChildren = (style: StyleType): React.ReactNode => { |
| 76 | + const { text } = this.props; |
| 77 | + |
| 78 | + return [ |
| 79 | + text ? this.renderTextElement(style.text) : undefined, |
| 80 | + ]; |
| 81 | + }; |
| 82 | + |
67 | 83 | public render(): React.ReactElement<TouchableOpacityProps> {
|
68 |
| - const { themedStyle, ...derivedProps } = this.props; |
69 |
| - const { container, select, highlight } = this.getComponentStyle(themedStyle); |
| 84 | + const { style, themedStyle, ...derivedProps } = this.props; |
| 85 | + const { selectContainer, select, highlight, ...componentStyles } = this.getComponentStyle(themedStyle); |
| 86 | + const componentChildren: React.ReactNode = this.renderComponentChildren(componentStyles); |
70 | 87 |
|
71 | 88 | return (
|
72 |
| - <TouchableOpacity |
73 |
| - {...derivedProps} |
74 |
| - activeOpacity={1.0} |
75 |
| - onPress={this.onPress} |
76 |
| - onPressIn={this.onPressIn} |
77 |
| - onPressOut={this.onPressOut}> |
78 |
| - <View style={styles.container}> |
79 |
| - <View style={highlight}/> |
80 |
| - <View style={container}> |
81 |
| - <View style={select}/> |
82 |
| - </View> |
| 89 | + <View style={[style, styles.container]}> |
| 90 | + <View style={styles.highlightContainer}> |
| 91 | + <View style={[highlight, styles.highlight]}/> |
| 92 | + <TouchableOpacity |
| 93 | + {...derivedProps} |
| 94 | + style={[selectContainer, styles.selectContainer]} |
| 95 | + activeOpacity={1.0} |
| 96 | + onPress={this.onPress} |
| 97 | + onPressIn={this.onPressIn} |
| 98 | + onPressOut={this.onPressOut}> |
| 99 | + <View style={[select, styles.select]}/> |
| 100 | + </TouchableOpacity> |
83 | 101 | </View>
|
84 |
| - </TouchableOpacity> |
| 102 | + {componentChildren} |
| 103 | + </View> |
85 | 104 | );
|
86 | 105 | }
|
87 | 106 | }
|
88 | 107 |
|
89 | 108 | const styles = StyleSheet.create({
|
90 | 109 | container: {
|
| 110 | + flexDirection: 'row', |
| 111 | + alignItems: 'center', |
| 112 | + }, |
| 113 | + highlightContainer: { |
| 114 | + justifyContent: 'center', |
| 115 | + alignItems: 'center', |
| 116 | + }, |
| 117 | + selectContainer: { |
91 | 118 | justifyContent: 'center',
|
92 | 119 | alignItems: 'center',
|
93 | 120 | },
|
94 | 121 | select: {},
|
95 | 122 | highlight: {
|
96 | 123 | position: 'absolute',
|
97 |
| - alignSelf: 'center', |
98 | 124 | },
|
| 125 | + text: {}, |
99 | 126 | });
|
0 commit comments