All files / FormEngine/templates PickerField.js

20% Statements 2/10
0% Branches 0/6
0% Functions 0/4
20% Lines 2/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x                                           1x                            
import React from 'react';
import {Picker, View, Text, StyleSheet} from 'react-native';
 
const PickerField = ({fromEngine, customize}) => {
  const {errorText, value: selectedValue, onChange, dirty} = fromEngine;
  const showError = !!errorText && dirty;
  const styles = makeStyles(showError);
  const {label, options} = customize;
 
  return (
    <View>
      <Text style={styles.label}>{label}</Text>
      <Picker
        style={styles.picker}
        selectedValue={selectedValue}
        onValueChange={itemValue => onChange(itemValue)}>
        {options.map(({label: optionLabel, value}) => (
          <Picker.Item key={value} label={optionLabel} value={value} />
        ))}
      </Picker>
      {showError && <Text style={styles.errorText}>{errorText}</Text>}
    </View>
  );
};
 
const makeStyles = showError =>
  StyleSheet.create({
    label: {marginBottom: 4, fontWeight: 'bold'},
    picker: {
      borderWidth: 1,
      borderColor: 'lightgrey',
      paddingHorizontal: 8,
      borderRadius: 4,
      ...(showError && {borderColor: 'red'}),
    },
    errorText: {color: 'red', fontSize: 12},
  });
 
export default PickerField;