|
| 1 | +import { useState, forwardRef } from 'react'; |
| 2 | +import { Group, Rect, Text } from 'react-konva'; |
| 3 | +import { ShapeConfig } from 'konva/lib/Shape'; |
| 4 | +import { ShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 5 | + |
| 6 | +interface InputWithStepperProps extends ShapeConfig { |
| 7 | + id: string; |
| 8 | + x: number; |
| 9 | + y: number; |
| 10 | + width: number; |
| 11 | + height: number; |
| 12 | + initialValue?: number; |
| 13 | +} |
| 14 | + |
| 15 | +// Size restrictions (igual patrón que file-tree) |
| 16 | +export const inputStepperShapeRestrictions: ShapeSizeRestrictions = { |
| 17 | + minWidth: 80, |
| 18 | + minHeight: 24, |
| 19 | + maxWidth: -1, |
| 20 | + maxHeight: -1, |
| 21 | + defaultWidth: 120, |
| 22 | + defaultHeight: 32, |
| 23 | +}; |
| 24 | + |
| 25 | +export const getInputStepperShapeSizeRestrictions = (): ShapeSizeRestrictions => |
| 26 | + inputStepperShapeRestrictions; |
| 27 | + |
| 28 | +export const InputWithStepper = forwardRef<any, InputWithStepperProps>( |
| 29 | + ({ x, y, width, height, initialValue = 0, id, ...shapeProps }, ref) => { |
| 30 | + const [value, setValue] = useState(initialValue); |
| 31 | + |
| 32 | + const handleIncrement = () => { |
| 33 | + setValue(value + 1); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleDecrement = () => { |
| 37 | + setValue(value - 1); |
| 38 | + }; |
| 39 | + |
| 40 | + const inputWidth = width - 30; // Reservar espacio para el stepper |
| 41 | + const buttonHeight = height / 2; |
| 42 | + |
| 43 | + return ( |
| 44 | + <Group x={x} y={y} ref={ref} {...shapeProps}> |
| 45 | + {/* Caja del input */} |
| 46 | + <Rect |
| 47 | + x={0} |
| 48 | + y={0} |
| 49 | + width={inputWidth / 2} // Reducir ancho a la mitad |
| 50 | + height={height} |
| 51 | + fill="white" |
| 52 | + stroke="black" |
| 53 | + strokeWidth={2} |
| 54 | + cornerRadius={0} // Sin bordes redondeados |
| 55 | + /> |
| 56 | + |
| 57 | + {/* Texto del input */} |
| 58 | + <Text |
| 59 | + x={inputWidth / 2 - 10} // Alinear a la derecha |
| 60 | + y={height / 2 - 8} // Centrar verticalmente |
| 61 | + text={value.toString()} |
| 62 | + fontFamily="Arial" |
| 63 | + fontSize={16} |
| 64 | + fill="black" |
| 65 | + align="right" |
| 66 | + /> |
| 67 | + |
| 68 | + {/* Botón de incremento (flecha arriba) */} |
| 69 | + <Group x={inputWidth / 2} y={0} onClick={handleIncrement}> |
| 70 | + <Rect |
| 71 | + x={0} |
| 72 | + y={0} |
| 73 | + width={30} |
| 74 | + height={buttonHeight} |
| 75 | + fill="lightgray" |
| 76 | + stroke="black" |
| 77 | + strokeWidth={2} |
| 78 | + /> |
| 79 | + <Text |
| 80 | + x={10} |
| 81 | + y={buttonHeight / 2 - 8} |
| 82 | + text="▲" |
| 83 | + fontFamily="Arial" |
| 84 | + fontSize={14} |
| 85 | + fill="black" |
| 86 | + /> |
| 87 | + </Group> |
| 88 | + |
| 89 | + {/* Botón de decremento (flecha abajo) */} |
| 90 | + <Group x={inputWidth / 2} y={buttonHeight} onClick={handleDecrement}> |
| 91 | + <Rect |
| 92 | + x={0} |
| 93 | + y={0} |
| 94 | + width={30} |
| 95 | + height={buttonHeight} |
| 96 | + fill="lightgray" |
| 97 | + stroke="black" |
| 98 | + strokeWidth={2} |
| 99 | + /> |
| 100 | + <Text |
| 101 | + x={10} |
| 102 | + y={buttonHeight / 2 - 8} |
| 103 | + text="▼" |
| 104 | + fontFamily="Arial" |
| 105 | + fontSize={14} |
| 106 | + fill="black" |
| 107 | + /> |
| 108 | + </Group> |
| 109 | + </Group> |
| 110 | + ); |
| 111 | + } |
| 112 | +); |
| 113 | + |
| 114 | +export default InputWithStepper; |
0 commit comments