crane-cloud/frontend

View on GitHub
src/components/InputText/index.js

Summary

Maintainability
A
0 mins
Test Coverage
D
60%
import React, { useState, useEffect } from "react";
import "./InputText.css";

const InputText = (props) => {
  const { name, value, placeholder, onChange, required } = props;
  const [inputBackground, setBackground] = useState("InitialBackground");
  const changeBackground = () => {
    if (value) {
      setBackground("WhiteBackground");
    } else {
      setBackground("InitialBackground");
    }
  };

  useEffect(() => changeBackground(), [value]); // eslint-disable-line

  return (
    <input
      className={`InputText ${inputBackground}`}
      placeholder={`${placeholder}${required ? " *" : ""}`}
      name={name}
      value={value}
      onChange={(e) => {
        onChange(e);
      }}
      {...props}
    />
  );
};

export default InputText;