Fakultas Ilmu Komputer UI

index.tsx 1.73 KB
Newer Older
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
import React, { FC } from 'react';
import { View } from 'react-native';
import { CheckBox, Text, Input } from 'react-native-elements';
import { typographyStyles } from 'styles';

import { styles } from './styles';
import { Props } from './types';
import FormLabel from '../FormLabel';
import { checkbox } from 'constants/options';

const CheckboxGroup: FC<Props> = ({
  label,
  choices,
  value,
  onChange,
  hasOtherChoice,
  otherValue,
  setOtherValue,
}) => {
  const handlePress = (choiceValue: string | number) => {
    if (value.includes(choiceValue)) {
      onChange(value.filter((item: number | string) => item !== choiceValue));
    } else {
      onChange([...value, choiceValue]);
    }
  };

  return (
    <View>
      <FormLabel label={label} />
      <Text style={[typographyStyles.bodySmall, styles.helperText]}>
        Dapat memilih lebih dari satu
      </Text>
      {choices.map((choice) => (
        <CheckBox
          key={choice.value}
          title={choice.label}
          checked={value.includes(choice.value)}
          onPress={() => handlePress(choice.value)}
        />
      ))}
      {hasOtherChoice ? (
        <CheckBox
          title={
            <View style={styles.otherContainer}>
              <Text style={styles.textStyle}>Lainnya:</Text>
              <Input
                onChangeText={setOtherValue}
                value={otherValue}
                inputContainerStyle={styles.inputContainerStyle}
                labelStyle={styles.removed}
                errorStyle={styles.removed}
              />
            </View>
          }
          checked={value.includes(checkbox.OTHER)}
          onPress={() => handlePress(checkbox.OTHER)}
        />
      ) : null}
    </View>
  );
};

export default CheckboxGroup;