about summary refs log tree commit diff
path: root/src/view/com/util/forms/RadioGroup.tsx
blob: e2a26dc495bf609bcf6163512591585ae901b1fd (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
import {useState} from 'react'
import {View} from 'react-native'

import {s} from '#/lib/styles'
import {ButtonType} from './Button'
import {RadioButton} from './RadioButton'

export interface RadioGroupItem {
  label: string | JSX.Element
  key: string
}

export function RadioGroup({
  testID,
  type,
  items,
  initialSelection = '',
  onSelect,
}: {
  testID?: string
  type?: ButtonType
  items: RadioGroupItem[]
  initialSelection?: string
  onSelect: (key: string) => void
}) {
  const [selection, setSelection] = useState<string>(initialSelection)
  const onSelectInner = (key: string) => {
    setSelection(key)
    onSelect(key)
  }
  return (
    <View>
      {items.map((item, i) => (
        <RadioButton
          key={item.key}
          testID={testID ? `${testID}-${item.key}` : undefined}
          style={i !== 0 ? s.mt2 : undefined}
          type={type}
          label={item.label}
          isSelected={item.key === selection}
          onPress={() => onSelectInner(item.key)}
        />
      ))}
    </View>
  )
}