about summary refs log tree commit diff
path: root/src/view/com/util/Selector.tsx
blob: ef7c65d59fcd6029defa49a5d60b49533478e236 (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
import React, {useState} from 'react'
import {
  StyleProp,
  StyleSheet,
  Text,
  TouchableWithoutFeedback,
  View,
  ViewStyle,
} from 'react-native'
import {colors} from '../../lib/styles'

export interface SelectorItem {
  label: string
}

export function Selector({
  style,
  items,
  onSelect,
}: {
  style?: StyleProp<ViewStyle>
  items: SelectorItem[]
  onSelect?: (index: number) => void
}) {
  const [selectedIndex, setSelectedIndex] = useState<number>(0)
  const onPressItem = (index: number) => {
    setSelectedIndex(index)
    onSelect?.(index)
  }

  return (
    <View style={[styles.outer, style]}>
      {items.map((item, i) => {
        const selected = i === selectedIndex
        return (
          <TouchableWithoutFeedback key={i} onPress={() => onPressItem(i)}>
            <View style={selected ? styles.itemSelected : styles.item}>
              <Text style={selected ? styles.labelSelected : styles.label}>
                {item.label}
              </Text>
            </View>
          </TouchableWithoutFeedback>
        )
      })}
    </View>
  )
}

const styles = StyleSheet.create({
  outer: {
    flexDirection: 'row',
    paddingHorizontal: 14,
  },
  item: {
    paddingBottom: 12,
    marginRight: 20,
  },
  label: {
    fontWeight: '600',
    fontSize: 16,
    color: colors.gray5,
  },
  itemSelected: {
    paddingBottom: 8,
    marginRight: 20,
    borderBottomWidth: 4,
    borderBottomColor: colors.purple3,
  },
  labelSelected: {
    fontWeight: '600',
    fontSize: 16,
  },
})