src/utils/index.tsx   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 114
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
eloc 59
mnd 1
bc 1
fnc 0
dl 0
loc 114
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
ccs 24
cts 24
cp 1
rs 10
1 1
import "../styles/animations.css";
2
3 1
import "../styles/shared-styles.css";
4
5 1
import classNames from "classnames";
6 1
import React from "react";
7
8
import { BoxComponent, BuilderProps } from "./global";
9
10
/**
11
 * Build a standardized component, providing shared functionalities and props, to optimize the process.
12
 *
13
 * @param name component name (will be its id)
14
 * @param Component component to render
15
 * @param commonProps shared common props (like `className`, `hide` or `dark`)
16
 * @param additionalProps additional props applied on rendered component
17
 * @param wrapper component external wrapper (like `button`, `a` or `p`, if not set will be `div`)
18
 *
19
 * @returns built component, ready to be rendered
20
 *
21
 * @see https://cianciarusocataldo.github.io/modular-ui/
22
 *
23
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
24
 *
25
 * @copyright 2022 Cataldo Cianciaruso
26
 */
27 1
export const buildComponent = ({
28 237
  name,
29 237
  Component,
30 237
  commonProps,
31 237
  additionalProps,
32 237
  wrapper,
33
}: BuilderProps) => {
34 237
  const SelectedWrapper = wrapper || "div";
35
36 237
  return (
37
    <SelectedWrapper
38
      data-id={commonProps.id}
39
      id={name}
40
      className={classNames(commonProps.className, {
41
        dark: commonProps.dark,
42
        "component-hidden": commonProps.hide,
43
        shadowed: commonProps.shadow,
44
        styled: !commonProps.unstyled,
45
      })}
46
      style={commonProps.style}
47
      {...additionalProps}
48
    >
49
      {Component}
50
    </SelectedWrapper>
51
  );
52
};
53
54
/**
55
 * Build a standardized component, decorating it with some functionalitis (like top label or dynamic value update).
56
 *
57
 * @param value actual component value
58
 * @param defaultValue default value (used when no value is set)
59
 * @param {string} label component top label
60
 * @param callback a function that returns an Object containing Component Builder props, properly formatted
61
 *
62
 * @returns built component, ready to be rendered, enhanced with some functionalities (like the top label)
63
 *
64
 * @see https://cianciarusocataldo.github.io/modular-ui/docs/#/guide?id=box-components
65
 * @see https://cianciarusocataldo.github.io/modular-ui/
66
 *
67
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
68
 *
69
 * @copyright 2022 Cataldo Cianciaruso
70
 */
71 1
export const buildBoxComponent = <T extends any>({
72 101
  value: actualValue,
73 101
  defaultValue,
74 101
  label,
75 101
  callBack,
76
}: BoxComponent<T> & {
77
  defaultValue?: T;
78
  callBack: (value: T, setValue: (newValue: T) => void) => BuilderProps;
79
}) => {
80 101
  const startValue = actualValue || defaultValue;
81
82 101
  const [value, setValue] = React.useState<T>(startValue);
83
84 101
  React.useEffect(() => {
85 42
    if (actualValue !== undefined && actualValue !== null) {
86 28
      setValue(actualValue);
87
    }
88
  }, [actualValue]);
89
90 101
  let { commonProps, additionalProps, ...props } = callBack(value, setValue);
91
92 101
  return buildComponent({
93
    ...props,
94
    commonProps: { ...commonProps, shadow: false, className: undefined },
95
    Component: (
96
      <div className="box-component">
97
        {label && <div className="box-label">{label}</div>}
98
        <div
99
          className={classNames(
100
            commonProps.className,
101
            {
102
              shadowed: commonProps.shadow,
103
            },
104
            "container"
105
          )}
106
          {...additionalProps}
107
        >
108
          {props.Component}
109
        </div>
110
      </div>
111
    ),
112
  });
113
};
114