@trackunit/react-components 0.0.130 → 0.0.131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ /// <reference types="react" />
2
+ import { LevelColors } from "./ValueBarTypes";
3
+ import { CommonProps } from "../../common";
4
+ export interface ValueBarProps extends CommonProps {
5
+ /**
6
+ * Value to display.
7
+ */
8
+ value: number;
9
+ /**
10
+ * Minimum value visible on the bar.
11
+ */
12
+ min?: number;
13
+ /**
14
+ * Maximum value visible on the bar.
15
+ */
16
+ max?: number;
17
+ /**
18
+ * Value unit. Visible only together with value if showValue parameter is set to true.
19
+ */
20
+ unit?: string;
21
+ /**
22
+ * Size of the tooltip
23
+ */
24
+ size?: "small" | "large";
25
+ /**
26
+ * Custom level colors. If this param is not provided then default values are used.
27
+ */
28
+ levelColors?: LevelColors;
29
+ /**
30
+ * Custom value color. If this param is not provided then default value is used.
31
+ */
32
+ valueColor?: string;
33
+ /**
34
+ * Show value with unit (if provided) on the bar in large version or after the bar in the small version.
35
+ */
36
+ showValue?: boolean;
37
+ }
38
+ /**
39
+ * ValueBar component is used to display value on a colorful bar within provided range.
40
+ *
41
+ * @param {ValueBarProps} props - The props for the ValueBar component
42
+ * @returns {JSX.Element} ValueBar component
43
+ */
44
+ export declare const ValueBar: ({ value, min, max, unit, size, levelColors, valueColor, showValue, className, dataTestId }: ValueBarProps) => JSX.Element;
@@ -0,0 +1,42 @@
1
+ import { LevelColors } from "./ValueBarTypes";
2
+ /**
3
+ * Helper function to get normalized score in range 0-1 used by the ValueBar
4
+ *
5
+ * @param {number} value - value for which score should be returned
6
+ * @param {number} min - min range value
7
+ * @param {number} max - max range value
8
+ * @returns {number} normalized score
9
+ */
10
+ export declare const getScore: (value: number, min: number, max: number) => number;
11
+ /**
12
+ * Helper function to get default color used by the ValueBar
13
+ *
14
+ * @param {number} score - score number for which color should be returned
15
+ * @returns {string} color value
16
+ */
17
+ export declare const getDefaultFillColor: (score: number) => string;
18
+ /**
19
+ * Helper function to get normalized color
20
+ *
21
+ * @param {number} levelColor - color, ex --color-green-600
22
+ * @returns {string} normalized color value
23
+ */
24
+ export declare const getNormalizedLevelColor: (levelColor: string) => string;
25
+ /**
26
+ * Helper function to get custom color used by the ValueBar
27
+ *
28
+ * @param {number} score - score number for which color should be returned
29
+ * @param {LevelColors} levelColors - custom colors and levels definitions
30
+ * @returns {string} color value
31
+ */
32
+ export declare const getFillColor: (score: number, levelColors: LevelColors) => string;
33
+ /**
34
+ * Helper function to get color used by the ValueBar
35
+ *
36
+ * @param {number} value - value for which color should be returned
37
+ * @param {number} min - min range value
38
+ * @param {number} max - max range value
39
+ * @param {LevelColors} levelColors - level colors used to coloring the bar
40
+ * @returns {JSX.Element} ValueBar component
41
+ */
42
+ export declare const getValueBarColorByValue: (value: number, min: number, max: number, levelColors: LevelColors) => string;
@@ -0,0 +1,9 @@
1
+ export interface LevelColor {
2
+ level?: number;
3
+ color: string;
4
+ }
5
+ export interface LevelColors {
6
+ low?: LevelColor;
7
+ medium?: LevelColor;
8
+ high?: LevelColor;
9
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./ValueBar";
2
+ export * from "./ValueBarTypes";
3
+ export { getValueBarColorByValue } from "./ValueBarHelper";
@@ -39,3 +39,4 @@ export * from "./Timeline/Timeline";
39
39
  export * from "./Tip";
40
40
  export * from "./Toggle";
41
41
  export * from "./Tooltip";
42
+ export * from "./ValueBar";
package/index.js CHANGED
@@ -19,6 +19,7 @@ import ReactAsyncCreatableSelect from 'react-select/async-creatable';
19
19
  import ReactCreatableSelect from 'react-select/creatable';
20
20
  import ReactAsyncSelect from 'react-select/async';
21
21
  import { format, formatDistance } from 'date-fns';
22
+ import { getColorValue } from '@trackunit/ui-design-tokens';
22
23
 
23
24
  const docs = {
24
25
  source: {
@@ -400,7 +401,7 @@ const Icon$1 = ({
400
401
  className: iconSizeClassName
401
402
  })
402
403
  }, {
403
- children: jsx(Root$3, Object.assign({
404
+ children: jsx(Root$4, Object.assign({
404
405
  "$color": color,
405
406
  onClick: onClick,
406
407
  "data-testid": dataTestId,
@@ -419,7 +420,7 @@ const Icon$1 = ({
419
420
  const LoadingDot = tw.span`
420
421
  tu-loading-dot
421
422
  `;
422
- const Root$3 = tw.span`
423
+ const Root$4 = tw.span`
423
424
  tu-icon
424
425
  ${({
425
426
  $color
@@ -581,7 +582,7 @@ const CustomIcon = tw(Icon$1)`
581
582
  * @returns {JSX.Element} Text component
582
583
  */
583
584
 
584
- const Text = ({
585
+ const Text$1 = ({
585
586
  children,
586
587
  type = "p",
587
588
  size = "medium",
@@ -727,7 +728,7 @@ const Alert = ({
727
728
  };
728
729
 
729
730
  const body = inline ? jsxs("div", {
730
- children: [title && jsx(Text, Object.assign({
731
+ children: [title && jsx(Text$1, Object.assign({
731
732
  weight: "bold"
732
733
  }, {
733
734
  children: title !== null && title !== void 0 ? title : children
@@ -738,7 +739,7 @@ const Alert = ({
738
739
  children: children
739
740
  }))]
740
741
  }) : jsxs(Fragment, {
741
- children: [title && jsx(Text, Object.assign({
742
+ children: [title && jsx(Text$1, Object.assign({
742
743
  weight: "bold"
743
744
  }, {
744
745
  children: title !== null && title !== void 0 ? title : children
@@ -856,7 +857,7 @@ const ContentDetails = tw.div`
856
857
  const ContentBody = tw.div`
857
858
  tu-alert__content-body
858
859
  `;
859
- const Description$2 = tw(Text)`
860
+ const Description$2 = tw(Text$1)`
860
861
  ${p => p.$hasTitle ? tws`tu-alert__description` : null}
861
862
  `;
862
863
  const RightAligned = tw.div`
@@ -1444,7 +1445,7 @@ const Chart = ({
1444
1445
  children: showLoading ? jsxs(SpinnerContainer, {
1445
1446
  children: [jsx(Spinner, {
1446
1447
  centered: true
1447
- }), (loadingOption === null || loadingOption === void 0 ? void 0 : loadingOption.text) && jsx(Text, Object.assign({
1448
+ }), (loadingOption === null || loadingOption === void 0 ? void 0 : loadingOption.text) && jsx(Text$1, Object.assign({
1448
1449
  align: "center",
1449
1450
  subtle: true
1450
1451
  }, {
@@ -1492,7 +1493,7 @@ const Label$3 = ({
1492
1493
  disabled,
1493
1494
  isInvalid
1494
1495
  }) => {
1495
- return jsx(Root$2, Object.assign({
1496
+ return jsx(Root$3, Object.assign({
1496
1497
  id: id || "",
1497
1498
  htmlFor: htmlFor || "",
1498
1499
  className: className,
@@ -1503,7 +1504,7 @@ const Label$3 = ({
1503
1504
  children: children
1504
1505
  }));
1505
1506
  };
1506
- const Root$2 = tw.label`
1507
+ const Root$3 = tw.label`
1507
1508
  tu-label
1508
1509
  ${({
1509
1510
  $disabled
@@ -1704,7 +1705,7 @@ const Collapse = ({
1704
1705
  onClick: handleClick
1705
1706
  }, {
1706
1707
  children: jsxs(LabelContainer$2, {
1707
- children: [jsx(Text, Object.assign({
1708
+ children: [jsx(Text$1, Object.assign({
1708
1709
  weight: "bold"
1709
1710
  }, {
1710
1711
  children: label
@@ -2220,7 +2221,7 @@ const PopoverListItem = ({
2220
2221
  })) : content
2221
2222
  }));
2222
2223
  };
2223
- const StyledText$2 = tw(Text)`
2224
+ const StyledText$2 = tw(Text$1)`
2224
2225
  tu-popover-list-item__text
2225
2226
  ${p => p.$selected && tws`tu-popover-list-item__text--selected`}
2226
2227
  `;
@@ -2267,7 +2268,7 @@ const Container$6 = tw.div`
2267
2268
  tu-popover-title-container
2268
2269
  ${p => p.$divider && `tu-popover-title-container--divider`}
2269
2270
  `;
2270
- const StyledText$1 = tw(Text)`
2271
+ const StyledText$1 = tw(Text$1)`
2271
2272
  tu-popover-title-text
2272
2273
  `;
2273
2274
 
@@ -2323,7 +2324,7 @@ const Tooltip = ({
2323
2324
  children: jsx(Content, Object.assign({
2324
2325
  "$mode": mode
2325
2326
  }, {
2326
- children: jsx(Text, Object.assign({
2327
+ children: jsx(Text$1, Object.assign({
2327
2328
  inverted: mode === "dark",
2328
2329
  type: contentTextType
2329
2330
  }, {
@@ -2721,7 +2722,7 @@ const LegendItem = ({
2721
2722
  dataTestId
2722
2723
  }) => {
2723
2724
  const handleOnClick = onClick && !disabled ? () => onClick() : undefined;
2724
- return jsxs(Root$1, Object.assign({
2725
+ return jsxs(Root$2, Object.assign({
2725
2726
  "data-testid": dataTestId,
2726
2727
  disabled: disabled,
2727
2728
  className: className,
@@ -2737,7 +2738,7 @@ const LegendItem = ({
2737
2738
  })]
2738
2739
  }));
2739
2740
  };
2740
- const Root$1 = tw.div`
2741
+ const Root$2 = tw.div`
2741
2742
  inline-flex
2742
2743
  items-center
2743
2744
  transition-all
@@ -3580,7 +3581,7 @@ const Container$1 = tw.label`
3580
3581
  $isDisabled
3581
3582
  }) => $isDisabled ? tws`tu-select--disabled` : null}
3582
3583
  `;
3583
- const InformativeText = tw(Text)`
3584
+ const InformativeText = tw(Text$1)`
3584
3585
  tu-informative-text
3585
3586
  ${p => p.$hasError ? `tu-informative-text--error` : null}
3586
3587
  `;
@@ -4543,11 +4544,11 @@ const TextLengthIndicator = ({
4543
4544
  length,
4544
4545
  maxLength
4545
4546
  }) => {
4546
- return jsxs(Root, {
4547
+ return jsxs(Root$1, {
4547
4548
  children: [length, "/", maxLength]
4548
4549
  });
4549
4550
  };
4550
- const Root = tw.span`
4551
+ const Root$1 = tw.span`
4551
4552
  font-mono
4552
4553
  `;
4553
4554
 
@@ -4839,6 +4840,166 @@ ${({
4839
4840
  }) => $disabled && tws`tu-toggle__description--disabled`}
4840
4841
  `;
4841
4842
 
4843
+ /**
4844
+ * Helper function to get normalized score in range 0-1 used by the ValueBar
4845
+ *
4846
+ * @param {number} value - value for which score should be returned
4847
+ * @param {number} min - min range value
4848
+ * @param {number} max - max range value
4849
+ * @returns {number} normalized score
4850
+ */
4851
+
4852
+ const getScore = (value, min, max) => {
4853
+ if (value <= min) {
4854
+ return 0.01; // always render at least some small fragment
4855
+ }
4856
+
4857
+ if (value >= max) {
4858
+ return 1;
4859
+ }
4860
+
4861
+ return (value - min) / (max - min);
4862
+ };
4863
+ /**
4864
+ * Helper function to get default color used by the ValueBar
4865
+ *
4866
+ * @param {number} score - score number for which color should be returned
4867
+ * @returns {string} color value
4868
+ */
4869
+
4870
+ const getDefaultFillColor = score => {
4871
+ if (score < 0.3) {
4872
+ return getColorValue("--color-red-500");
4873
+ }
4874
+
4875
+ if (score >= 0.6) {
4876
+ return getColorValue("--color-green-600");
4877
+ }
4878
+
4879
+ return getColorValue("--color-amber-300");
4880
+ };
4881
+ /**
4882
+ * Helper function to get normalized color
4883
+ *
4884
+ * @param {number} levelColor - color, ex --color-green-600
4885
+ * @returns {string} normalized color value
4886
+ */
4887
+
4888
+ const getNormalizedLevelColor = levelColor => {
4889
+ if (levelColor.startsWith("--color")) {
4890
+ return getColorValue(levelColor);
4891
+ }
4892
+
4893
+ return levelColor;
4894
+ };
4895
+ /**
4896
+ * Helper function to get custom color used by the ValueBar
4897
+ *
4898
+ * @param {number} score - score number for which color should be returned
4899
+ * @param {LevelColors} levelColors - custom colors and levels definitions
4900
+ * @returns {string} color value
4901
+ */
4902
+
4903
+ const getFillColor = (score, levelColors) => {
4904
+ var _a, _b;
4905
+
4906
+ if (levelColors.low && score < (levelColors.low.level || 0)) {
4907
+ return getNormalizedLevelColor(levelColors.low.color);
4908
+ }
4909
+
4910
+ if (levelColors.high && score >= (levelColors.high.level || 0)) {
4911
+ return getNormalizedLevelColor(levelColors.high.color);
4912
+ }
4913
+
4914
+ return getNormalizedLevelColor((_b = (_a = levelColors.medium) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b : "--color-amber-300");
4915
+ };
4916
+ /**
4917
+ * Helper function to get color used by the ValueBar
4918
+ *
4919
+ * @param {number} value - value for which color should be returned
4920
+ * @param {number} min - min range value
4921
+ * @param {number} max - max range value
4922
+ * @param {LevelColors} levelColors - level colors used to coloring the bar
4923
+ * @returns {JSX.Element} ValueBar component
4924
+ */
4925
+
4926
+ const getValueBarColorByValue = (value, min, max, levelColors) => {
4927
+ const score = getScore(value, min, max);
4928
+ return getFillColor(score, levelColors);
4929
+ };
4930
+
4931
+ /**
4932
+ * ValueBar component is used to display value on a colorful bar within provided range.
4933
+ *
4934
+ * @param {ValueBarProps} props - The props for the ValueBar component
4935
+ * @returns {JSX.Element} ValueBar component
4936
+ */
4937
+
4938
+ const ValueBar = ({
4939
+ value,
4940
+ min = 0,
4941
+ max = 100,
4942
+ unit,
4943
+ size = "small",
4944
+ levelColors,
4945
+ valueColor,
4946
+ showValue,
4947
+ className,
4948
+ dataTestId
4949
+ }) => {
4950
+ const score = getScore(value, min, max);
4951
+ const barFillColor = levelColors ? getFillColor(score, levelColors) : getDefaultFillColor(score);
4952
+ const barWidth = showValue && size === "small" ? 90 : 100;
4953
+ return jsx(Root, Object.assign({
4954
+ className: className,
4955
+ "data-testid": dataTestId,
4956
+ "$size": size
4957
+ }, {
4958
+ children: jsxs("g", {
4959
+ children: [jsx(Background, {
4960
+ width: `${barWidth}%`,
4961
+ rx: "5"
4962
+ }), jsx(Bar, {
4963
+ width: `${score * barWidth}%`,
4964
+ fill: barFillColor,
4965
+ rx: "5"
4966
+ }), showValue && size === "large" && jsxs(Text, Object.assign({
4967
+ "$size": size,
4968
+ x: "12",
4969
+ y: "23",
4970
+ fill: valueColor !== null && valueColor !== void 0 ? valueColor : "white"
4971
+ }, {
4972
+ children: [Number(value.toFixed(1)), unit || ""]
4973
+ })), showValue && size === "small" && jsxs(Text, Object.assign({
4974
+ "$size": size,
4975
+ x: `${barWidth + 2}%`,
4976
+ y: "11",
4977
+ fill: valueColor !== null && valueColor !== void 0 ? valueColor : getColorValue("--color-neutral-400")
4978
+ }, {
4979
+ children: [Number(value.toFixed(1)), unit || ""]
4980
+ }))]
4981
+ })
4982
+ }));
4983
+ };
4984
+ const Root = tw.svg`
4985
+ tu-value-bar
4986
+ ${({
4987
+ $size
4988
+ }) => $size === "small" ? tws`tu-value-bar--small` : tws`tu-value-bar--large`}
4989
+ `;
4990
+ const Text = tw.text`
4991
+ tu-value-bar__text
4992
+ ${({
4993
+ $size
4994
+ }) => $size === "small" ? tws`tu-value-bar__text--small` : tws`tu-value-bar__text--large`}
4995
+ `;
4996
+ const Background = tw.rect`
4997
+ tu-value-bar__background
4998
+ `;
4999
+ const Bar = tw.rect`
5000
+ tu-value-bar__bar
5001
+ `;
5002
+
4842
5003
  /**
4843
5004
  * The useDebounce hook works like useState, but adds a delay where previous values will be ignored.
4844
5005
  *
@@ -5202,4 +5363,4 @@ const Layout = tw.div`
5202
5363
  tu-layout-TwoColumnLayout
5203
5364
  `;
5204
5365
 
5205
- export { ActionButton, Alert, AnimatedPopover, Badge, BaseInput, Button, Card, CardBody, CardFooter, CardHeader, Chart, Checkbox, Collapse, CopyableText, CreatableSelect, DateField, DateInput, DayPicker, DayPickerPopover, DayRangePicker, DensityContainer, FilterListMapLayout, FormGroup, FullWidthRepeatingLayout, GridArea, Heading, Icon$1 as Icon, IconButton, Indicator$1 as Indicator, IntrinsicallyResponsiveGrid, Label$3 as Label, LegendItem, MenuButton, MenuItem, MenuList, MinTopFullContentLayout, Modal, ModalBackdrop, MultiSelectMenuItem, NumberField, NumberInput, PageHeading, PopoverLifecycle, PopoverList, PopoverListItem, PopoverTitle, Prompt, ROLE_CARD, RadioGroup, RadioItem, Select, SingleSelectMenuItem, SkeletonLines, Spacer, Spinner, StarButton, Tag, Text, TextArea, TextAreaField, TextField, TextInput, Timeline, TimelineElement, Tip, TitleNavContentLayout, Toggle, Tooltip, TwoColumnLayout, activityColors, colors, criticalityColors, docs, generalColors, getOrderedOptions, intentColors, isMultiValue, useContainerProps, useCustomComponents, useDebounce, useLocalStorage, useModal, usePopover, usePrompt, useTimeout };
5366
+ export { ActionButton, Alert, AnimatedPopover, Badge, BaseInput, Button, Card, CardBody, CardFooter, CardHeader, Chart, Checkbox, Collapse, CopyableText, CreatableSelect, DateField, DateInput, DayPicker, DayPickerPopover, DayRangePicker, DensityContainer, FilterListMapLayout, FormGroup, FullWidthRepeatingLayout, GridArea, Heading, Icon$1 as Icon, IconButton, Indicator$1 as Indicator, IntrinsicallyResponsiveGrid, Label$3 as Label, LegendItem, MenuButton, MenuItem, MenuList, MinTopFullContentLayout, Modal, ModalBackdrop, MultiSelectMenuItem, NumberField, NumberInput, PageHeading, PopoverLifecycle, PopoverList, PopoverListItem, PopoverTitle, Prompt, ROLE_CARD, RadioGroup, RadioItem, Select, SingleSelectMenuItem, SkeletonLines, Spacer, Spinner, StarButton, Tag, Text$1 as Text, TextArea, TextAreaField, TextField, TextInput, Timeline, TimelineElement, Tip, TitleNavContentLayout, Toggle, Tooltip, TwoColumnLayout, ValueBar, activityColors, colors, criticalityColors, docs, generalColors, getOrderedOptions, getValueBarColorByValue, intentColors, isMultiValue, useContainerProps, useCustomComponents, useDebounce, useLocalStorage, useModal, usePopover, usePrompt, useTimeout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-components",
3
- "version": "0.0.130",
3
+ "version": "0.0.131",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {