-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhorizontal-menu.tsx
More file actions
112 lines (99 loc) · 3.03 KB
/
horizontal-menu.tsx
File metadata and controls
112 lines (99 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { Group, Rect, Text } from 'react-konva';
import { ShapeSizeRestrictions, ShapeType } from '@/core/model';
import { forwardRef } from 'react';
import { ShapeProps } from '../../shape.model';
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';
import { BASIC_SHAPE } from '../../front-components/shape.const';
import { useShapeProps } from '../../../shapes/use-shape-props.hook';
import {
extractCSVHeaders,
splitCSVContentIntoRows,
} from '@/common/utils/active-element-selector.utils';
import { useGroupShapeProps } from '../../mock-components.utils';
const horizontalMenuShapeSizeRestrictions: ShapeSizeRestrictions = {
minWidth: 200,
minHeight: 25,
maxWidth: -1,
maxHeight: 100,
defaultWidth: 500,
defaultHeight: 50,
};
export const getHorizontalMenuShapeSizeRestrictions =
(): ShapeSizeRestrictions => horizontalMenuShapeSizeRestrictions;
const shapeType: ShapeType = 'horizontal-menu';
export const HorizontalMenu = forwardRef<any, ShapeProps>((props, ref) => {
const {
x,
y,
width,
height,
id,
onSelected,
text,
otherProps,
...shapeProps
} = props;
const csvData = splitCSVContentIntoRows(text);
const headers = extractCSVHeaders(csvData[0]);
const itemLabels = headers.map(header => header.text);
const numberOfItems = itemLabels.length;
const itemSpacing = 10;
const restrictedSize = fitSizeToShapeSizeRestrictions(
horizontalMenuShapeSizeRestrictions,
width,
height
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;
const totalMargins = restrictedWidth - itemSpacing * (numberOfItems + 1);
const itemWidth = totalMargins / numberOfItems;
const { stroke, strokeStyle, fill, textColor, borderRadius } = useShapeProps(
otherProps,
BASIC_SHAPE
);
const itemVerticalPadding = 4;
const activeSelected = otherProps?.activeElement ?? 0;
const commonGroupProps = useGroupShapeProps(
props,
restrictedSize,
shapeType,
ref
);
return (
<Group {...commonGroupProps} {...shapeProps}>
<Rect
x={0}
y={0}
width={restrictedWidth}
height={restrictedHeight}
stroke={stroke}
strokeWidth={2}
dash={strokeStyle}
fill={fill}
cornerRadius={borderRadius}
/>
{itemLabels.map((header, index) => (
<Group key={index}>
<Rect
x={itemSpacing * (index + 1) + itemWidth * index}
y={itemVerticalPadding}
width={itemWidth}
height={restrictedHeight - 2 * itemVerticalPadding}
fill={index === activeSelected ? 'lightblue' : fill}
/>
<Text
x={itemSpacing * (index + 1) + itemWidth * index}
y={restrictedHeight / 2 - 8}
text={header}
fontFamily="Arial"
fontSize={16}
fill={textColor}
width={itemWidth}
align="center"
wrap="none"
ellipsis={true}
/>
</Group>
))}
</Group>
);
});