|
| 1 | +import { forwardRef } from 'react'; |
| 2 | +import { Group, Rect, Text } from 'react-konva'; |
| 3 | +import { ShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 4 | +import { useGroupShapeProps } from '../mock-components.utils'; |
| 5 | +import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 6 | +import { ShapeType } from '@/core/model'; |
| 7 | +import { ShapeProps } from '../shape.model'; |
| 8 | +import { useShapeProps } from '../../shapes/use-shape-props.hook'; |
| 9 | +import { INPUT_SHAPE } from '../front-components/shape.const'; |
| 10 | + |
| 11 | +// Size restrictions (igual patrón que file-tree) |
| 12 | +export const inputStepperShapeRestrictions: ShapeSizeRestrictions = { |
| 13 | + minWidth: 80, |
| 14 | + minHeight: 24, |
| 15 | + maxWidth: -1, |
| 16 | + maxHeight: -1, |
| 17 | + defaultWidth: 120, |
| 18 | + defaultHeight: 32, |
| 19 | +}; |
| 20 | + |
| 21 | +export const getInputStepperShapeSizeRestrictions = (): ShapeSizeRestrictions => |
| 22 | + inputStepperShapeRestrictions; |
| 23 | + |
| 24 | +export const InputWithStepper = forwardRef<any, ShapeProps>((props, ref) => { |
| 25 | + const { x, y, width, height, text, onSelect, otherProps, id, ...shapeProps } = |
| 26 | + props; |
| 27 | + |
| 28 | + const inputWidth = width - 30; // Reservar espacio para el stepper |
| 29 | + const buttonHeight = height / 2; |
| 30 | + |
| 31 | + const restrictedSize = fitSizeToShapeSizeRestrictions( |
| 32 | + inputStepperShapeRestrictions, |
| 33 | + width, |
| 34 | + height |
| 35 | + ); |
| 36 | + |
| 37 | + const shapeType: ShapeType = 'input-stepper'; |
| 38 | + |
| 39 | + const commonGroupProps = useGroupShapeProps( |
| 40 | + props, |
| 41 | + restrictedSize, |
| 42 | + shapeType, |
| 43 | + ref |
| 44 | + ); |
| 45 | + const { stroke, textColor, strokeStyle, fill, strokeWidth } = useShapeProps( |
| 46 | + otherProps, |
| 47 | + INPUT_SHAPE |
| 48 | + ); |
| 49 | + |
| 50 | + const { width: restrictedWidth } = restrictedSize; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Group {...commonGroupProps} {...shapeProps}> |
| 54 | + {/* Caja del input */} |
| 55 | + <Rect |
| 56 | + x={0} |
| 57 | + y={0} |
| 58 | + width={restrictedWidth} |
| 59 | + height={height} |
| 60 | + fill={fill} |
| 61 | + stroke={stroke} |
| 62 | + strokeWidth={strokeWidth} |
| 63 | + dash={strokeStyle} |
| 64 | + cornerRadius={0} // Sin bordes redondeados |
| 65 | + /> |
| 66 | + |
| 67 | + {/* Texto del input */} |
| 68 | + <Text |
| 69 | + x={0} // Alinear a la derecha |
| 70 | + y={height / 2 - 8} // Centrar verticalmente |
| 71 | + width={restrictedWidth} |
| 72 | + text={text} |
| 73 | + fontFamily="Arial" |
| 74 | + fontSize={16} |
| 75 | + fill={textColor} |
| 76 | + align="center" |
| 77 | + wrap="none" |
| 78 | + /> |
| 79 | + |
| 80 | + {/* Botón de incremento (flecha arriba) */} |
| 81 | + <Group x={inputWidth} y={0}> |
| 82 | + <Rect |
| 83 | + x={0} |
| 84 | + y={0} |
| 85 | + width={30} |
| 86 | + height={buttonHeight} |
| 87 | + fill="lightgrey" |
| 88 | + stroke={stroke} |
| 89 | + strokeWidth={strokeWidth} |
| 90 | + dash={strokeStyle} |
| 91 | + /> |
| 92 | + <Text |
| 93 | + x={10} |
| 94 | + y={buttonHeight / 2 - 8} |
| 95 | + text="▲" |
| 96 | + fontFamily="Arial" |
| 97 | + fontSize={14} |
| 98 | + fill={textColor} |
| 99 | + /> |
| 100 | + </Group> |
| 101 | + |
| 102 | + {/* Botón de decremento (flecha abajo) */} |
| 103 | + <Group x={inputWidth} y={buttonHeight}> |
| 104 | + <Rect |
| 105 | + x={0} |
| 106 | + y={0} |
| 107 | + width={30} |
| 108 | + height={buttonHeight} |
| 109 | + fill="lightgrey" |
| 110 | + stroke={stroke} |
| 111 | + strokeWidth={strokeWidth} |
| 112 | + dash={strokeStyle} |
| 113 | + /> |
| 114 | + <Text |
| 115 | + x={10} |
| 116 | + y={buttonHeight / 2 - 8} |
| 117 | + text="▼" |
| 118 | + fontFamily="Arial" |
| 119 | + fontSize={14} |
| 120 | + fill={textColor} |
| 121 | + /> |
| 122 | + </Group> |
| 123 | + </Group> |
| 124 | + ); |
| 125 | +}); |
| 126 | + |
| 127 | +export default InputWithStepper; |
0 commit comments