|
| 1 | +import { forwardRef } from 'react'; |
| 2 | +import { Group, Rect, Text } from 'react-konva'; |
| 3 | +import { ShapeSizeRestrictions, ShapeType } from '@/core/model'; |
| 4 | +import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 5 | +import { useShapeProps } from '../../shapes/use-shape-props.hook'; |
| 6 | +import { CHIP_SHAPE } from '../front-components/shape.const'; |
| 7 | +import { ShapeProps } from '../shape.model'; |
| 8 | +import { useGroupShapeProps } from '../mock-components.utils'; |
| 9 | + |
| 10 | +const ChipShapeSizeRestrictions: ShapeSizeRestrictions = { |
| 11 | + minWidth: 40, |
| 12 | + minHeight: 28, |
| 13 | + maxWidth: -1, |
| 14 | + maxHeight: 28, |
| 15 | + defaultWidth: 56, |
| 16 | + defaultHeight: 28, |
| 17 | +}; |
| 18 | + |
| 19 | +export const getChipShapeSizeRestrictions = (): ShapeSizeRestrictions => |
| 20 | + ChipShapeSizeRestrictions; |
| 21 | + |
| 22 | +const shapeType: ShapeType = 'chip'; |
| 23 | + |
| 24 | +export const ChipShape = forwardRef<any, ShapeProps>((props, ref) => { |
| 25 | + const { |
| 26 | + x, |
| 27 | + y, |
| 28 | + width, |
| 29 | + height, |
| 30 | + id, |
| 31 | + text, |
| 32 | + onSelected, |
| 33 | + otherProps, |
| 34 | + ...shapeProps |
| 35 | + } = props; |
| 36 | + const restrictedSize = fitSizeToShapeSizeRestrictions( |
| 37 | + ChipShapeSizeRestrictions, |
| 38 | + width, |
| 39 | + height |
| 40 | + ); |
| 41 | + |
| 42 | + const { width: restrictedWidth, height: restrictedHeigth } = restrictedSize; |
| 43 | + const { stroke, strokeStyle, fill, textColor } = useShapeProps( |
| 44 | + otherProps, |
| 45 | + CHIP_SHAPE |
| 46 | + ); |
| 47 | + |
| 48 | + const commonGroupProps = useGroupShapeProps( |
| 49 | + props, |
| 50 | + restrictedSize, |
| 51 | + shapeType, |
| 52 | + ref |
| 53 | + ); |
| 54 | + |
| 55 | + return ( |
| 56 | + <Group {...commonGroupProps} {...shapeProps}> |
| 57 | + <Rect |
| 58 | + x={0} |
| 59 | + y={0} |
| 60 | + width={restrictedWidth + CHIP_SHAPE.DEFAULT_STROKE_WIDTH * 2} |
| 61 | + height={restrictedHeigth} |
| 62 | + fill={fill} |
| 63 | + stroke={stroke} |
| 64 | + dash={strokeStyle} |
| 65 | + strokeWidth={CHIP_SHAPE.DEFAULT_STROKE_WIDTH} |
| 66 | + cornerRadius={CHIP_SHAPE.DEFAULT_CORNER_RADIUS} |
| 67 | + /> |
| 68 | + |
| 69 | + <Text |
| 70 | + x={CHIP_SHAPE.DEFAULT_PADDING} |
| 71 | + y={CHIP_SHAPE.DEFAULT_PADDING - CHIP_SHAPE.DEFAULT_STROKE_WIDTH * 2} |
| 72 | + width={ |
| 73 | + restrictedWidth - |
| 74 | + CHIP_SHAPE.DEFAULT_PADDING * 2 - |
| 75 | + CHIP_SHAPE.DEFAULT_STROKE_WIDTH |
| 76 | + } |
| 77 | + text={text} |
| 78 | + fontFamily={CHIP_SHAPE.DEFAULT_FONT_FAMILY} |
| 79 | + fontSize={CHIP_SHAPE.DEFAULT_FONT_SIZE} |
| 80 | + fill={textColor} |
| 81 | + verticalAlign="middle" |
| 82 | + align="center" |
| 83 | + ellipsis={true} |
| 84 | + wrap="none" |
| 85 | + /> |
| 86 | + </Group> |
| 87 | + ); |
| 88 | +}); |
0 commit comments