-
Notifications
You must be signed in to change notification settings - Fork 20
Feature/#216 create input stepper component #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
92160ca
created branch y added svg file for issue #216
devrodrigolec 08b2edd
added input-stepper.tsx and improve the starting point
devrodrigolec bb94184
component rendered
devrodrigolec 6108434
propertys working
devrodrigolec 77041fc
Component now working with properties; improved state handling throug…
devrodrigolec 22097ae
small fixes
devrodrigolec 2bf54d8
inline edit in development
devrodrigolec 3be388d
inline edition working
devrodrigolec d8cfa8e
number() changed for parseInt in inputWithStepper.business
devrodrigolec dc033e5
..
devrodrigolec 2e532de
Merge branch 'dev' into feature/#216-create-input-stepper-component
devrodrigolec e93ef71
finished
devrodrigolec b8ed61a
event double click on buttons handled
devrodrigolec 9ab928e
Merge remote-tracking branch 'origin/dev' into feature/#216-create-in…
deletidev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/common/components/mock-components/front-rich-components/input-with-stepper/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './input-with-stepper'; | ||
export * from './input-with-stepper.business'; |
63 changes: 63 additions & 0 deletions
63
...s/mock-components/front-rich-components/input-with-stepper/input-with-stepper.business.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
type MustBeANumberError = 'You must enter a number'; | ||
|
||
interface handleCounterInputWithStepperHook { | ||
valueToString: string | MustBeANumberError; | ||
handleIncrement: () => void; | ||
handleDecrement: () => void; | ||
isTextANumber: boolean; | ||
} | ||
|
||
const MUST_BE_A_NUMBER: MustBeANumberError = 'You must enter a number'; | ||
|
||
export const useHandleCounterInputWithStepper = ( | ||
text: string | ||
): handleCounterInputWithStepperHook => { | ||
const [value, setValue] = React.useState<number | MustBeANumberError>(0); | ||
|
||
const textToNumber = parseInt(text); | ||
|
||
const isTextANumber: boolean = !isNaN(textToNumber); | ||
|
||
useEffect(() => { | ||
if (isTextANumber) { | ||
setValue(textToNumber); | ||
} else { | ||
setValue(MUST_BE_A_NUMBER); | ||
} | ||
}, [text]); | ||
|
||
const handleIncrement = () => { | ||
if (typeof value === 'number') { | ||
setValue(value + 1); | ||
} | ||
}; | ||
|
||
const handleDecrement = () => { | ||
if (typeof value === 'number') { | ||
if (value === 0) return; | ||
setValue(value - 1); | ||
} | ||
}; | ||
|
||
const valueToString: string = | ||
typeof value === 'string' ? value : value.toString(); | ||
|
||
return { | ||
valueToString, | ||
handleIncrement, | ||
handleDecrement, | ||
isTextANumber, | ||
}; | ||
}; | ||
|
||
export const handleButtonWidth = (restrictedWidth: number): number => { | ||
const buttonWidth = restrictedWidth * 0.3; | ||
const minButtonWidth = 30; | ||
const maxButtonWidth = 70; | ||
|
||
if (buttonWidth < minButtonWidth) return minButtonWidth; | ||
if (buttonWidth > maxButtonWidth) return maxButtonWidth; | ||
return buttonWidth; | ||
}; |
176 changes: 176 additions & 0 deletions
176
...omponents/mock-components/front-rich-components/input-with-stepper/input-with-stepper.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { forwardRef } from 'react'; | ||
import { Group, Rect, Text } from 'react-konva'; | ||
import { ShapeSizeRestrictions } from '@/core/model'; | ||
import { ShapeType } from '../../../../../core/model/index'; | ||
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes'; | ||
import { useShapeComponentSelection } from '../../../shapes/use-shape-selection.hook'; | ||
import { ShapeProps } from '../../shape.model'; | ||
import { | ||
handleButtonWidth, | ||
useHandleCounterInputWithStepper, | ||
} from './input-with-stepper.business'; | ||
import { INPUT_SHAPE } from '../../front-components/shape.const'; | ||
import { KonvaEventObject } from 'konva/lib/Node'; | ||
import { useShapeProps } from '@/common/components/shapes/use-shape-props.hook'; | ||
import { useGroupShapeProps } from '../../mock-components.utils'; | ||
|
||
const inputWithStepperSizeRestrictions: ShapeSizeRestrictions = { | ||
minWidth: 60, | ||
minHeight: 35, | ||
maxWidth: 500, | ||
maxHeight: 35, | ||
defaultWidth: 100, | ||
defaultHeight: 35, | ||
}; | ||
|
||
export const getInputWithStepperSizeRestrictions = (): ShapeSizeRestrictions => | ||
inputWithStepperSizeRestrictions; | ||
|
||
const shapeType: ShapeType = 'inputWithStepper'; | ||
|
||
export const InputWithStepperShape = forwardRef<any, ShapeProps>( | ||
(props, ref) => { | ||
const { | ||
x, | ||
y, | ||
width, | ||
height, | ||
id, | ||
text, | ||
onSelected, | ||
otherProps, | ||
...shapeProps | ||
} = props; | ||
|
||
const restrictedSize = fitSizeToShapeSizeRestrictions( | ||
inputWithStepperSizeRestrictions, | ||
width, | ||
height | ||
); | ||
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize; | ||
|
||
const { handleSelection } = useShapeComponentSelection(props, shapeType); | ||
|
||
const handleDoubleClickInButtons = (e: KonvaEventObject<MouseEvent>) => | ||
(e.cancelBubble = true); | ||
|
||
const { | ||
valueToString: value, | ||
handleIncrement, | ||
handleDecrement, | ||
isTextANumber, | ||
} = useHandleCounterInputWithStepper(text); | ||
|
||
const { stroke, strokeStyle, fill, textColor } = useShapeProps( | ||
otherProps, | ||
INPUT_SHAPE | ||
); | ||
|
||
// Reservar espacio para el stepper | ||
const buttonWidth = handleButtonWidth(restrictedWidth); | ||
const buttonHeight = restrictedHeight / 2; | ||
|
||
const commonGroupProps = useGroupShapeProps( | ||
props, | ||
restrictedSize, | ||
shapeType, | ||
ref | ||
); | ||
|
||
return ( | ||
<Group {...commonGroupProps} {...shapeProps} onClick={handleSelection}> | ||
{/* Caja del input */} | ||
<Rect | ||
x={0} | ||
y={0} | ||
width={restrictedWidth - buttonWidth} | ||
height={restrictedHeight} | ||
fill={fill} | ||
stroke={stroke} | ||
strokeWidth={2} | ||
dash={strokeStyle} | ||
/> | ||
|
||
{/* Texto del input */} | ||
<Text | ||
width={restrictedWidth - buttonWidth - 8} | ||
x={0} // Alinear a la derecha dependiendo de la cantidad de dígitos | ||
y={restrictedHeight / 2 - 6} // Centrar verticalmente | ||
text={isTextANumber ? value : ''} | ||
fontFamily={INPUT_SHAPE.DEFAULT_FONT_FAMILY} | ||
fontSize={INPUT_SHAPE.DEFAULT_FONT_SIZE + 2} | ||
fill={textColor} | ||
align="right" | ||
/> | ||
|
||
{/* Botón de incremento (flecha arriba) */} | ||
<Group | ||
x={restrictedWidth - buttonWidth} | ||
y={0} | ||
onClick={handleIncrement} | ||
onDblClick={handleDoubleClickInButtons} | ||
> | ||
<Rect | ||
x={0} | ||
y={0} | ||
width={buttonWidth} | ||
height={buttonHeight} | ||
fill={fill} | ||
stroke={stroke} | ||
strokeWidth={2} | ||
dash={strokeStyle} | ||
/> | ||
<Text | ||
x={buttonWidth / 2 - 6} | ||
y={buttonHeight / 2 - 6} | ||
text="▲" | ||
fontFamily={INPUT_SHAPE.DEFAULT_FONT_FAMILY} | ||
fontSize={INPUT_SHAPE.DEFAULT_FONT_SIZE} | ||
fill={textColor} | ||
align="center" | ||
/> | ||
</Group> | ||
|
||
{/* Botón de decremento (flecha abajo) */} | ||
<Group | ||
x={restrictedWidth - buttonWidth} | ||
y={buttonHeight} | ||
onClick={handleDecrement} | ||
onDblClick={handleDoubleClickInButtons} | ||
> | ||
<Rect | ||
x={0} | ||
y={0} | ||
width={buttonWidth} | ||
height={buttonHeight} | ||
fill={fill} | ||
stroke={stroke} | ||
strokeWidth={2} | ||
dash={strokeStyle} | ||
/> | ||
<Text | ||
x={buttonWidth / 2 - 6} | ||
y={buttonHeight / 2 - 6} | ||
text="▼" | ||
fontFamily={INPUT_SHAPE.DEFAULT_FONT_FAMILY} | ||
fontSize={INPUT_SHAPE.DEFAULT_FONT_SIZE} | ||
fill={textColor} | ||
align="center" | ||
/> | ||
</Group> | ||
{!isTextANumber && ( | ||
<Group x={0} y={40}> | ||
<Text | ||
x={0} | ||
y={0} | ||
text={value} | ||
fontFamily={INPUT_SHAPE.DEFAULT_FONT_FAMILY} | ||
fontSize={INPUT_SHAPE.DEFAULT_FONT_SIZE} | ||
fill="gray" | ||
/> | ||
</Group> | ||
)} | ||
</Group> | ||
); | ||
} | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/pods/canvas/shape-renderer/simple-rich-components/input-with-stepper.renderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { InputWithStepperShape } from '@/common/components/mock-components/front-rich-components'; | ||
import { ShapeRendererProps } from '../model'; | ||
import { ShapeModel } from '@/core/model'; | ||
|
||
export const renderInputWithStepper = ( | ||
shape: ShapeModel, | ||
shapeRenderedProps: ShapeRendererProps | ||
) => { | ||
const { handleSelected, shapeRefs, handleDragEnd, handleTransform } = | ||
shapeRenderedProps; | ||
|
||
return ( | ||
<InputWithStepperShape | ||
id={shape.id} | ||
key={shape.id} | ||
ref={shapeRefs.current[shape.id]} | ||
x={shape.x} | ||
y={shape.y} | ||
name="shape" | ||
width={shape.width} | ||
height={shape.height} | ||
draggable | ||
typeOfTransformer={shape.typeOfTransformer} | ||
onSelected={handleSelected} | ||
onDragEnd={handleDragEnd(shape.id)} | ||
onTransform={handleTransform} | ||
onTransformEnd={handleTransform} | ||
otherProps={shape.otherProps} | ||
text={shape.text} | ||
/> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of this, i prefrer do this:
if (value > 0) {
setValue(value - 1);
}