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

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 75
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 32
mnd 2
bc 2
fnc 0
dl 0
loc 75
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 { CounterComponent } from "./types";
6
7 1
import { buildBoxComponent } from "../../../utils";
8
9
/**
10
 * A flexible numeric input element
11
 *
12
 * @since 2.0.0
13
 *
14
 * @param {number} value numeric input value
15
 * @param {(newValue:number)=>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 Counter usage</caption>
25
 *
26
 * import { render } from "react-dom";
27
 * import { Counter } from '@cianciarusocataldo/modular-ui';
28
 *
29
 * render(<Counter value={3} />, document.getElementById("root"));
30
 *
31
 * @see https://cianciarusocataldo.github.io/modular-ui/components/atoms/Counter
32
 *
33
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
34
 *
35
 * @copyright 2022 Cataldo Cianciaruso
36
 */
37 1
const Counter: CounterComponent = ({
38 12
  onChange,
39 12
  value,
40 12
  placeholder,
41 12
  readOnly,
42 12
  label,
43 12
  ...commonProps
44
}) =>
45 12
  buildBoxComponent<number>({
46 12
    callBack: (value, setValue) => ({
47
      name: "modular-counterbox",
48
      Component: (
49
        <input
50
          type="number"
51
          value={value}
52
          placeholder={placeholder}
53
          className="input-box"
54
          readOnly={readOnly}
55
          onChange={(e) => {
56 4
            if (!readOnly) {
57 3
              const changedValue = e.target.value
58
                ? Number.parseInt(e.target.value)
59
                : 0;
60
61 3
              onChange && onChange(changedValue);
62 3
              setValue(changedValue);
63
            }
64
          }}
65
        />
66
      ),
67
      commonProps,
68
    }),
69
    defaultValue: value || 0,
70
    value,
71
    label,
72
  });
73
74
export default Counter;
75