From 446e23da62bd8d32951a547918a909f353fd29a2 Mon Sep 17 00:00:00 2001 From: "LAPTOP-G2U17NSB\\duy_9" Date: Mon, 19 Aug 2024 17:40:09 +0200 Subject: [PATCH 1/2] Adding "checked" property as new "otherprop" -Checked allowed in Toggle, Checkbox and Radiobutton components -Also, cleaned up some files by removing console.log statements --- .../front-components/checkbox-shape.tsx | 60 ++++++++++++------- .../front-components/radiobutton-shape.tsx | 18 +++++- .../front-components/toggleswitch-shape.tsx | 17 ++++-- src/core/model/index.ts | 3 +- src/pods/canvas/canvas.model.ts | 19 ++++++ .../simple-component/checkbox.renderer.tsx | 1 + .../simple-component/radiobutton.renderer.tsx | 1 + .../toggleswitch.renderer.tsx | 1 + .../breadcrumb.renderer.tsx | 2 - .../heading1.renderer.tsx | 2 - .../heading2.renderer.tsx | 2 - .../heading3.renderer.tsx | 2 - .../normaltext.renderer.tsx | 2 - .../paragraph.renderer.tsx | 2 - .../smalltext.renderer.tsx | 2 - .../checked/checked.component.module.css | 32 ++++++++++ .../components/checked/checked.component.tsx | 25 ++++++++ src/pods/properties/properties.pod.tsx | 8 +++ 18 files changed, 156 insertions(+), 43 deletions(-) create mode 100644 src/pods/properties/components/checked/checked.component.module.css create mode 100644 src/pods/properties/components/checked/checked.component.tsx diff --git a/src/common/components/front-components/checkbox-shape.tsx b/src/common/components/front-components/checkbox-shape.tsx index 1714481c..7d8fbaad 100644 --- a/src/common/components/front-components/checkbox-shape.tsx +++ b/src/common/components/front-components/checkbox-shape.tsx @@ -1,5 +1,5 @@ import { ShapeSizeRestrictions } from '@/core/model'; -import { forwardRef } from 'react'; +import { forwardRef, useEffect, useState } from 'react'; import { ShapeProps } from './shape.model'; import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; import { Group, Rect, Line, Text } from 'react-konva'; @@ -21,10 +21,25 @@ const boxTickWidth = 50; const tickWidth = boxTickWidth - marginTick; export const CheckBoxShape = forwardRef( - ({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => { + ( + { x, y, width, height, id, onSelected, text, otherProps, ...shapeProps }, + ref + ) => { const { width: restrictedWidth, height: restrictedHeight } = fitSizeToShapeSizeRestrictions(checkBoxShapeRestrictions, width, height); + const [isOn, setIsOn] = useState(otherProps?.checked ?? false); + + useEffect(() => { + if (otherProps?.checked != undefined) { + setIsOn(otherProps?.checked); + } + }, [otherProps?.checked]); + + const handleClick = () => { + setIsOn(!isOn); + }; + return ( ( width={restrictedWidth} height={restrictedHeight} {...shapeProps} - onClick={() => onSelected(id, 'checkbox')} + onClick={() => { + onSelected(id, 'checkbox'); + handleClick(); + }} > ( strokeWidth={2} fill="white" /> - + {isOn && ( + + )} + radioButtonShapeRestrictions; + export const RadioButtonShape = forwardRef( - ({ x, y, width, height, id, onSelected, text, ...shapeProps }, ref) => { + ( + { x, y, width, height, id, onSelected, text, otherProps, ...shapeProps }, + ref + ) => { const { width: restrictedWidth, height: restrictedHeight } = fitSizeToShapeSizeRestrictions( radioButtonShapeRestrictions, @@ -22,7 +28,13 @@ export const RadioButtonShape = forwardRef( height ); - const [isOn, setIsOn] = useState(false); + const [isOn, setIsOn] = useState(otherProps?.checked ?? false); + + useEffect(() => { + if (otherProps?.checked != undefined) { + setIsOn(otherProps?.checked); + } + }, [otherProps?.checked]); const handleSwitch = () => { setIsOn(!isOn); diff --git a/src/common/components/front-components/toggleswitch-shape.tsx b/src/common/components/front-components/toggleswitch-shape.tsx index 3c894183..04e645b7 100644 --- a/src/common/components/front-components/toggleswitch-shape.tsx +++ b/src/common/components/front-components/toggleswitch-shape.tsx @@ -1,5 +1,5 @@ import { ShapeSizeRestrictions } from '@/core/model'; -import { forwardRef, useState } from 'react'; +import { forwardRef, useEffect, useState } from 'react'; import { ShapeProps } from './shape.model'; import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; import { Circle, Group, Rect } from 'react-konva'; @@ -17,7 +17,7 @@ export const getToggleSwitchShapeSizeRestrictions = (): ShapeSizeRestrictions => toggleSwitchShapeRestrictions; export const ToggleSwitch = forwardRef( - ({ x, y, width, height, id, onSelected, ...shapeProps }, ref) => { + ({ x, y, width, height, id, onSelected, otherProps, ...shapeProps }, ref) => { const { width: restrictedWidth, height: restrictedHeight } = fitSizeToShapeSizeRestrictions( toggleSwitchShapeRestrictions, @@ -25,14 +25,21 @@ export const ToggleSwitch = forwardRef( height ); - const [isOn, setIsOn] = useState(false); + const [isOn, setIsOn] = useState(otherProps?.checked ?? false); const handleSwitch = () => { - //TODO: Only available when shape is selected setIsOn(!isOn); }; - const circleX = isOn ? width - height / 2 : height / 2; + useEffect(() => { + if (otherProps?.checked != undefined) { + setIsOn(otherProps?.checked); + } + }, [otherProps?.checked]); + + const circleX = isOn + ? restrictedWidth - restrictedHeight / 2 + : restrictedHeight / 2; return ( { width: getLargeArrowShapeSizeRestrictions().defaultWidth, height: getLargeArrowShapeSizeRestrictions().defaultHeight, }; + case 'radiobutton': + return { + width: getRadioButtonShapeSizeRestrictions().defaultWidth, + height: getRadioButtonShapeSizeRestrictions().defaultHeight, + }; + case 'checkbox': + return { + width: getCheckboxShapeSizeRestrictions().defaultWidth, + height: getCheckboxShapeSizeRestrictions().defaultHeight, + }; default: console.warn( `** Shape ${shapeType} has not defined default size, check getDefaultSizeFromShape helper function` @@ -353,6 +365,13 @@ export const generateDefaultOtherProps = ( backgroundColor: '#d3d3d3', textColor: '#000000', }; + case 'toggleswitch': + case 'radiobutton': + case 'checkbox': + return { + checked: true, + }; + default: return undefined; } diff --git a/src/pods/canvas/shape-renderer/simple-component/checkbox.renderer.tsx b/src/pods/canvas/shape-renderer/simple-component/checkbox.renderer.tsx index 74df85ad..2b358308 100644 --- a/src/pods/canvas/shape-renderer/simple-component/checkbox.renderer.tsx +++ b/src/pods/canvas/shape-renderer/simple-component/checkbox.renderer.tsx @@ -26,6 +26,7 @@ export const renderCheckbox = ( onTransformEnd={handleTransform} isEditable={shape.allowsInlineEdition} text={shape.text} + otherProps={shape.otherProps} /> ); }; diff --git a/src/pods/canvas/shape-renderer/simple-component/radiobutton.renderer.tsx b/src/pods/canvas/shape-renderer/simple-component/radiobutton.renderer.tsx index b30966c8..635c01f6 100644 --- a/src/pods/canvas/shape-renderer/simple-component/radiobutton.renderer.tsx +++ b/src/pods/canvas/shape-renderer/simple-component/radiobutton.renderer.tsx @@ -26,6 +26,7 @@ export const renderRadioButton = ( onTransformEnd={handleTransform} isEditable={shape.allowsInlineEdition} text={shape.text} + otherProps={shape.otherProps} /> ); }; diff --git a/src/pods/canvas/shape-renderer/simple-component/toggleswitch.renderer.tsx b/src/pods/canvas/shape-renderer/simple-component/toggleswitch.renderer.tsx index dde20f9a..0dabc916 100644 --- a/src/pods/canvas/shape-renderer/simple-component/toggleswitch.renderer.tsx +++ b/src/pods/canvas/shape-renderer/simple-component/toggleswitch.renderer.tsx @@ -24,6 +24,7 @@ export const renderToggleSwitch = ( onDragEnd={handleDragEnd(shape.id)} onTransform={handleTransform} onTransformEnd={handleTransform} + otherProps={shape.otherProps} /> ); }; diff --git a/src/pods/canvas/shape-renderer/simple-rich-components/breadcrumb.renderer.tsx b/src/pods/canvas/shape-renderer/simple-rich-components/breadcrumb.renderer.tsx index dcf78f2e..d5804e24 100644 --- a/src/pods/canvas/shape-renderer/simple-rich-components/breadcrumb.renderer.tsx +++ b/src/pods/canvas/shape-renderer/simple-rich-components/breadcrumb.renderer.tsx @@ -9,8 +9,6 @@ export const renderBreadcrumb = ( const { handleSelected, shapeRefs, handleDragEnd, handleTransform } = shapeRenderedProps; - console.log(shapeRefs.current[shape.id]); - return ( void; +} + +export const Checked: React.FC = props => { + const { label, checked, onChange } = props; + + return ( +
+

{label}

+ +
+ ); +}; diff --git a/src/pods/properties/properties.pod.tsx b/src/pods/properties/properties.pod.tsx index 941d5590..b7d2fce9 100644 --- a/src/pods/properties/properties.pod.tsx +++ b/src/pods/properties/properties.pod.tsx @@ -2,6 +2,7 @@ import { useCanvasContext } from '@/core/providers'; import classes from './properties.pod.module.css'; import { ZIndexOptions } from './components/zindex/zindex-option.component'; import { ColorPicker } from './components/color-picker/color-picker.component'; +import { Checked } from './components/checked/checked.component'; export const PropertiesPod = () => { const { selectionInfo } = useCanvasContext(); @@ -45,6 +46,13 @@ export const PropertiesPod = () => { onChange={color => updateOtherPropsOnSelected('textColor', color)} /> )} + {selectedShapeData?.otherProps?.checked != undefined && ( + updateOtherPropsOnSelected('checked', checked)} + /> + )} ); }; From 9c8a7d88d78e1c74188944a03273d733908457bd Mon Sep 17 00:00:00 2001 From: "LAPTOP-G2U17NSB\\duy_9" Date: Tue, 20 Aug 2024 18:31:11 +0200 Subject: [PATCH 2/2] Using useMemo instead of useState to manage the checked state --- .../front-components/checkbox-shape.tsx | 18 +++++------------- .../front-components/radiobutton-shape.tsx | 18 +++++------------- .../front-components/toggleswitch-shape.tsx | 18 +++++------------- 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/common/components/front-components/checkbox-shape.tsx b/src/common/components/front-components/checkbox-shape.tsx index 7d8fbaad..3ab7a1d9 100644 --- a/src/common/components/front-components/checkbox-shape.tsx +++ b/src/common/components/front-components/checkbox-shape.tsx @@ -1,5 +1,5 @@ import { ShapeSizeRestrictions } from '@/core/model'; -import { forwardRef, useEffect, useState } from 'react'; +import { forwardRef, useMemo } from 'react'; import { ShapeProps } from './shape.model'; import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; import { Group, Rect, Line, Text } from 'react-konva'; @@ -28,17 +28,10 @@ export const CheckBoxShape = forwardRef( const { width: restrictedWidth, height: restrictedHeight } = fitSizeToShapeSizeRestrictions(checkBoxShapeRestrictions, width, height); - const [isOn, setIsOn] = useState(otherProps?.checked ?? false); - - useEffect(() => { - if (otherProps?.checked != undefined) { - setIsOn(otherProps?.checked); - } - }, [otherProps?.checked]); - - const handleClick = () => { - setIsOn(!isOn); - }; + const isOn = useMemo( + () => otherProps?.checked ?? true, + [otherProps?.checked] + ); return ( ( {...shapeProps} onClick={() => { onSelected(id, 'checkbox'); - handleClick(); }} > ( height ); - const [isOn, setIsOn] = useState(otherProps?.checked ?? false); - - useEffect(() => { - if (otherProps?.checked != undefined) { - setIsOn(otherProps?.checked); - } - }, [otherProps?.checked]); - - const handleSwitch = () => { - setIsOn(!isOn); - }; + const isOn = useMemo( + () => otherProps?.checked ?? true, + [otherProps?.checked] + ); const radius = restrictedHeight / 2; @@ -63,7 +56,6 @@ export const RadioButtonShape = forwardRef( {/* CĂ­rculo interior del radio button (checked) */} ( height ); - const [isOn, setIsOn] = useState(otherProps?.checked ?? false); - - const handleSwitch = () => { - setIsOn(!isOn); - }; - - useEffect(() => { - if (otherProps?.checked != undefined) { - setIsOn(otherProps?.checked); - } - }, [otherProps?.checked]); + const isOn = useMemo( + () => otherProps?.checked ?? true, + [otherProps?.checked] + ); const circleX = isOn ? restrictedWidth - restrictedHeight / 2 @@ -62,7 +55,6 @@ export const ToggleSwitch = forwardRef( fill={isOn ? 'lightgreen' : 'lightgray'} />