All files / FormEngine/templates TextField.js

75% Statements 9/12
53.85% Branches 7/13
50% Functions 2/4
75% Lines 9/12

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 41 42 43 44 45 46 47 48 49 50      1x 48x 48x   48x 48x 48x   48x                                       1x 48x                                  
import React from 'react';
import {View, Text, TextInput, StyleSheet} from 'react-native';
 
const TextField = ({fromEngine, customize}) => {
  const {touched, errorText, onChange, onBlur, ...rest} = fromEngine;
  const {label, editable = true, ...restOptions} = customize;
 
  const showError = touched && errorText;
  const [isFocusing, setIsFocusing] = React.useState(false);
  const styles = makeStyles({showError, isFocusing, editable});
 
  return (
    <View style={styles.container}>
      <Text style={styles.label}>{label}</Text>
      <TextInput
        style={styles.textInput}
        onChangeText={onChange}
        onFocus={() => setIsFocusing(true)}
        onBlur={() => {
          setIsFocusing(false);
          onBlur();
        }}
        editable={editable}
        {...rest}
        {...restOptions}
      />
      {showError && <Text style={styles.errorText}>{errorText}</Text>}
    </View>
  );
};
 
const makeStyles = ({showError, isFocusing, editable}) =>
  StyleSheet.create({
    container: {
      ...(!editable && {opacity: 0.5}),
    },
    label: {marginBottom: 4, fontWeight: 'bold'},
    textInput: {
      borderWidth: 1,
      borderColor: isFocusing ? '#007bff' : 'lightgrey',
      padding: 8,
      borderRadius: 4,
      ...(showError && {borderColor: 'red'}),
      ...(!editable && {backgroundColor: '#EEEEEE'}),
    },
    errorText: {color: 'red', fontSize: 12},
  });
 
export default TextField;