aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/react-small-apps/apps/todos/src/components/forms/Input/Input.js
blob: 86e660cdd5e97eadb1ff9e0bdce636765de34ba9 (plain)
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
function Input({
  label,
  id,
  name,
  value,
  updateValue,
  onBlurHandler,
  required,
  type = "text",
}) {
  const handleChange = (e) => {
    e.target.type === "checkbox"
      ? updateValue(e.target.checked)
      : updateValue(e.target.value);
  };

  return (
    <>
      {label && (
        <label htmlFor={id} className="form__label">
          {label}
        </label>
      )}
      <input
        type={type}
        id={id}
        name={name}
        value={type === "checkbox" ? undefined : value}
        checked={type === "checkbox" ? value : null}
        required={required ? "required" : false}
        onChange={handleChange}
        onBlur={onBlurHandler}
        className="form__field"
      />
    </>
  );
}

export default Input;