@topconsultnpm/sdkui-react-beta 6.7.99 → 6.7.100

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.
@@ -8,28 +8,47 @@ import TMLayoutContainer, { TMLayoutItem } from '../base/TMLayout';
8
8
  import TMVilViewer from '../base/TMVilViewer';
9
9
  import TMTooltip from '../base/TMTooltip';
10
10
  import { DeviceType, useDeviceType } from '../base/TMDeviceProvider';
11
+ // Define the TMTextArea component
11
12
  const TMTextArea = (props) => {
13
+ // Extract properties from the props object
12
14
  const { label = '', value = '', width = '100%', height = 'auto', autoFocus = false, validationItems = [], disabled = false, isModifiedWhen = false, fontSize = FontSize.defaultFontSize, elementStyle = {}, icon = null, labelPosition = 'left', readOnly = false, onValueChanged, onBlur, placeHolder, formulaItems = [], buttons = [], maxHeight = 'auto', rows, maxLength } = props;
15
+ // Reference to the textarea DOM element
13
16
  const inputRef = useRef(null);
17
+ // Stores the textarea is focused
14
18
  const [isFocused, setIsFocused] = useState(false);
19
+ // Stores the current value of the textarea
15
20
  const [currentValue, setCurrentValue] = useState(value);
21
+ // Stores the items for the context menu
16
22
  const [formulaMenuItems, setFormulaMenuItems] = useState([]);
23
+ // Stores the calculated number of rows for the textarea
17
24
  const [calculatedRows, setCalculatedRows] = useState(rows ?? 1);
25
+ // Manages the state for the custom context menu
18
26
  const { clicked, setClicked, points, setPoints } = useContextMenu();
19
27
  const deviceType = useDeviceType();
28
+ // Attach a `ResizeObserver` to the textarea to monitor changes in width: dynamically updates rows based on textarea content and width
29
+ useEffect(() => {
30
+ const observer = new ResizeObserver(() => { if (currentValue) {
31
+ updateRows();
32
+ } });
33
+ if (inputRef.current) {
34
+ observer.observe(inputRef.current);
35
+ }
36
+ return () => { if (inputRef.current) {
37
+ observer.unobserve(inputRef.current);
38
+ } };
39
+ }, [currentValue]);
40
+ // Update current value whenever the `value` prop changes, synchronize the internal state with the `value` prop
20
41
  useEffect(() => {
21
42
  setCurrentValue(value);
22
43
  }, [value]);
44
+ // Recalculate the number of rows whenever the `currentValue` changes
23
45
  useEffect(() => {
24
46
  if (currentValue) {
25
47
  const newRowCount = calculateRows(currentValue);
26
48
  setCalculatedRows(newRowCount);
27
49
  }
28
50
  }, [currentValue]);
29
- const calculateRows = (content) => {
30
- const lines = content.split('\n').length;
31
- return Math.min(lines, 10);
32
- };
51
+ // Populate the formula menu items whenever the `formulaItems` prop changes
33
52
  useEffect(() => {
34
53
  if (formulaItems && formulaItems.length > 0) {
35
54
  let menuItems = [];
@@ -39,6 +58,24 @@ const TMTextArea = (props) => {
39
58
  setFormulaMenuItems(menuItems);
40
59
  }
41
60
  }, [formulaItems]);
61
+ // Adjust the rows dynamically
62
+ const updateRows = () => {
63
+ const newRowCount = calculateRows(currentValue);
64
+ setCalculatedRows(newRowCount);
65
+ };
66
+ // Calculate the number of rows required to display the content
67
+ const calculateRows = (content) => {
68
+ if (!inputRef.current)
69
+ return rows ?? 1;
70
+ const textareaWidth = inputRef.current.offsetWidth;
71
+ const charWidth = 8;
72
+ const maxCharsPerLine = Math.floor(textareaWidth / charWidth);
73
+ const lines = content.split('\n').reduce((acc, line) => {
74
+ return acc + Math.ceil(line.length / maxCharsPerLine);
75
+ }, 0);
76
+ return Math.min(lines, 10);
77
+ };
78
+ // Inserts text at the cursor position
42
79
  const insertText = (textToInsert) => {
43
80
  if (inputRef?.current == null)
44
81
  return;
@@ -56,6 +93,7 @@ const TMTextArea = (props) => {
56
93
  TMExceptionBoxManager.show({ exception: e });
57
94
  }
58
95
  };
96
+ // Renders the textarea
59
97
  const renderTextArea = () => {
60
98
  return _jsxs(_Fragment, { children: [_jsx(StyledTextareaEditor, { ref: inputRef, autoFocus: autoFocus, readOnly: readOnly, disabled: disabled, value: currentValue, placeholder: placeHolder, rows: calculatedRows, maxLength: maxLength, onFocus: () => setIsFocused(true), onBlur: (e) => { setIsFocused(false); if (currentValue != value)
61
99
  onBlur?.(currentValue); }, onChange: (e) => { setCurrentValue(e.target.value); onValueChanged?.(e); }, onContextMenu: (e) => {
@@ -74,6 +112,7 @@ const TMTextArea = (props) => {
74
112
  return (_jsx(StyledEditorButtonIcon, { onClick: buttonItem.onClick, "$index": index, "$transform": '-26px', children: _jsx(TMTooltip, { content: buttonItem.text, children: buttonItem.icon }) }, index));
75
113
  }), formulaItems.length > 0 && clicked && (_jsx(TMContextMenu, { menuData: formulaMenuItems, top: points.y, left: points.x, onMenuItemClick: (formula) => insertText(formula) })), _jsx(TMVilViewer, { vil: validationItems })] });
76
114
  };
115
+ // Layout for the textarea with a left-aligned label
77
116
  const renderedLeftLabelTextArea = () => {
78
117
  return (_jsxs(TMLayoutContainer, { direction: 'horizontal', children: [icon && _jsx(TMLayoutItem, { width: '20px', children: _jsx(StyledEditorIcon, { "$disabled": disabled, "$vil": validationItems, "$isModified": isModifiedWhen, children: icon }) }), _jsx(TMLayoutItem, { children: _jsxs(StyledEditorContainer, { "$width": width, children: [label && _jsx(StyledEditorLabel, { "$isFocused": isFocused, "$labelPosition": labelPosition, "$disabled": disabled, children: label ?? '' }), renderTextArea()] }) })] }));
79
118
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@topconsultnpm/sdkui-react-beta",
3
- "version": "6.7.99",
3
+ "version": "6.7.100",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",