mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {StyleProp, StyleSheet, TextStyle, View, ViewStyle} from 'react-native'
3
4import {choose} from 'lib/functions'
5import {useTheme} from 'lib/ThemeContext'
6import {Text} from '../text/Text'
7import {Button, ButtonType} from './Button'
8
9export function RadioButton({
10 testID,
11 type = 'default-light',
12 label,
13 isSelected,
14 style,
15 onPress,
16}: {
17 testID?: string
18 type?: ButtonType
19 label: string | JSX.Element
20 isSelected: boolean
21 style?: StyleProp<ViewStyle>
22 onPress: () => void
23}) {
24 const theme = useTheme()
25 const circleStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(type, {
26 primary: {
27 borderColor: theme.palette.primary.text,
28 },
29 secondary: {
30 borderColor: theme.palette.secondary.text,
31 },
32 inverted: {
33 borderColor: theme.palette.inverted.text,
34 },
35 'primary-outline': {
36 borderColor: theme.palette.primary.border,
37 },
38 'secondary-outline': {
39 borderColor: theme.palette.secondary.border,
40 },
41 'primary-light': {
42 borderColor: theme.palette.primary.border,
43 },
44 'secondary-light': {
45 borderColor: theme.palette.secondary.border,
46 },
47 default: {
48 borderColor: theme.palette.default.border,
49 },
50 'default-light': {
51 borderColor: theme.palette.default.borderDark,
52 },
53 })
54 const circleFillStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(
55 type,
56 {
57 primary: {
58 backgroundColor: theme.palette.primary.text,
59 },
60 secondary: {
61 backgroundColor: theme.palette.secondary.text,
62 },
63 inverted: {
64 backgroundColor: theme.palette.inverted.text,
65 },
66 'primary-outline': {
67 backgroundColor: theme.palette.primary.background,
68 },
69 'secondary-outline': {
70 backgroundColor: theme.palette.secondary.background,
71 },
72 'primary-light': {
73 backgroundColor: theme.palette.primary.background,
74 },
75 'secondary-light': {
76 backgroundColor: theme.palette.secondary.background,
77 },
78 default: {
79 backgroundColor: theme.palette.primary.background,
80 },
81 'default-light': {
82 backgroundColor: theme.palette.primary.background,
83 },
84 },
85 )
86 const labelStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(type, {
87 primary: {
88 color: theme.palette.primary.text,
89 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
90 },
91 secondary: {
92 color: theme.palette.secondary.text,
93 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
94 },
95 inverted: {
96 color: theme.palette.inverted.text,
97 fontWeight: theme.palette.inverted.isLowContrast ? '500' : undefined,
98 },
99 'primary-outline': {
100 color: theme.palette.primary.textInverted,
101 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
102 },
103 'secondary-outline': {
104 color: theme.palette.secondary.textInverted,
105 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
106 },
107 'primary-light': {
108 color: theme.palette.primary.textInverted,
109 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
110 },
111 'secondary-light': {
112 color: theme.palette.secondary.textInverted,
113 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
114 },
115 default: {
116 color: theme.palette.default.text,
117 fontWeight: theme.palette.default.isLowContrast ? '500' : undefined,
118 },
119 'default-light': {
120 color: theme.palette.default.text,
121 fontWeight: theme.palette.default.isLowContrast ? '500' : undefined,
122 },
123 })
124 return (
125 <Button testID={testID} type={type} onPress={onPress} style={style}>
126 <View style={styles.outer}>
127 <View style={[circleStyle, styles.circle]}>
128 {isSelected ? (
129 <View style={[circleFillStyle, styles.circleFill]} />
130 ) : undefined}
131 </View>
132 {typeof label === 'string' ? (
133 <Text type="button" style={[labelStyle, styles.label]}>
134 {label}
135 </Text>
136 ) : (
137 <View style={styles.label}>{label}</View>
138 )}
139 </View>
140 </Button>
141 )
142}
143
144const styles = StyleSheet.create({
145 outer: {
146 flexDirection: 'row',
147 alignItems: 'center',
148 },
149 circle: {
150 width: 26,
151 height: 26,
152 borderRadius: 15,
153 padding: 4,
154 borderWidth: 1,
155 marginRight: 10,
156 },
157 circleFill: {
158 width: 16,
159 height: 16,
160 borderRadius: 10,
161 },
162 label: {
163 flex: 1,
164 },
165})