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

export interface RadioGroupItem {
  label: string
  key: string
}

export function RadioGroup({
  items,
  onSelect,
}: {
  items: RadioGroupItem[]
  onSelect: (key: string) => void
}) {
  const [selection, setSelection] = useState<string>('')
  const onSelectInner = (key: string) => {
    setSelection(key)
    onSelect(key)
  }
  return (
    <View>
      {items.map(item => (
        <RadioButton
          key={item.key}
          label={item.label}
          isSelected={item.key === selection}
          onPress={() => onSelectInner(item.key)}
        />
      ))}
    </View>
  )
}