about summary refs log tree commit diff
path: root/src/components/RadioGroup.tsx
blob: 010f65bc314e77d1f174387568ce256582a38ee6 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
import React from 'react'
import {View, ViewProps} from 'react-native'

import {atoms as a, useTheme} from '#/alf'
import {Button} from './Button'
import {Text} from './Typography'

export function RadioGroup<T extends string | number>({
  value,
  onSelect,
  items,
  ...props
}: ViewProps & {
  value: T
  onSelect: (value: T) => void
  items: Array<{label: string; value: T}>
}) {
  return (
    <View {...props}>
      {items.map(item => (
        <Button
          label={item.label}
          key={item.value}
          variant="ghost"
          color="secondary"
          size="small"
          onPress={() => onSelect(item.value)}
          style={[a.justify_between, a.px_sm]}>
          <Text style={a.text_md}>{item.label}</Text>
          <RadioIcon selected={value === item.value} />
        </Button>
      ))}
    </View>
  )
}

function RadioIcon({selected}: {selected: boolean}) {
  const t = useTheme()
  return (
    <View
      style={[
        {
          width: 30,
          height: 30,
          borderWidth: 2,
          borderColor: selected
            ? t.palette.primary_500
            : t.palette.contrast_200,
        },
        selected
          ? {
              backgroundColor:
                t.name === 'light'
                  ? t.palette.primary_100
                  : t.palette.primary_900,
            }
          : t.atoms.bg,
        a.align_center,
        a.justify_center,
        a.rounded_full,
      ]}>
      {selected && (
        <View
          style={[
            {
              width: 18,
              height: 18,
              backgroundColor: t.palette.primary_500,
            },
            a.rounded_full,
          ]}
        />
      )}
    </View>
  )
}