src/components/atoms/Input/index.tsx   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 70
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 30
mnd 2
bc 2
fnc 0
dl 0
loc 70
ccs 17
cts 17
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1 1
import "./styles.css";
2
3 1
import React from "react";
4
5
import { InputComponent } from "./types";
6
7 1
import { buildBoxComponent } from "../../../utils";
8
9
/**
10
 * A flexible text input element
11
 *
12
 * @since 1.0.0
13
 *
14
 * @param {string} value text input value
15
 * @param {(newValue:string)=>void} onChange callback triggered when numeric input changes
16
 * @param {string} label `common modular-ui prop` - component top label
17
 * @param {string} className `common modular-ui prop` - custom className (to better customize it)
18
 * @param {boolean} unstyled `common modular-ui prop` - Style/unstyle component (to better customize it)
19
 * @param {string} id `common modular-ui prop` - `data-id` parameter (for testing purpose, to easily find the component into the DOM)
20
 * @param {boolean} dark `common modular-ui prop` - Enable/disable dark mode
21
 * @param {boolean} hide `common modular-ui prop` - Hide/show component
22
 * @param {boolean} shadow `common modular-ui prop` - Enable/disable shadow behind component (to better customize it)
23
 *
24
 * @example <caption>Example Input usage</caption>
25
 * import { render } from "react-dom";
26
 * import { Input } from '@cianciarusocataldo/modular-ui';
27
 *
28
 * render(<Input value="example text" />, document.getElementById("root"));
29
 *
30
 * @see https://cianciarusocataldo.github.io/modular-ui/components/atoms/Input
31
 *
32
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
33
 *
34
 * @copyright 2022 Cataldo Cianciaruso*/
35 1
const Input: InputComponent = ({
36 17
  onChange = () => {},
37 17
  value,
38 17
  placeholder,
39 17
  readOnly,
40 17
  label,
41 17
  ...commonProps
42
}) =>
43 17
  buildBoxComponent({
44 17
    callBack: (value, setValue) => ({
45
      name: "modular-inputbox",
46
      Component: (
47
        <input
48
          type="text"
49
          value={value}
50
          placeholder={placeholder}
51
          className="input-box"
52
          readOnly={readOnly}
53
          onChange={(e) => {
54 6
            if (!readOnly) {
55 5
              const newValue = e.target.value ? e.target.value : "";
56 5
              onChange(newValue);
57 5
              setValue(newValue);
58
            }
59
          }}
60
        />
61
      ),
62
      commonProps,
63
    }),
64
    defaultValue: value || "",
65
    value,
66
    label,
67
  });
68
69
export default Input;
70