From f9e5be792bc58989e56320c6d6543afcef19b657 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Wed, 22 Jun 2022 21:45:34 +0300 Subject: [PATCH 01/26] feat(percentage-c11n): convert to table cell --- src/components/fields/Percentage/Settings.tsx | 10 +++++++ .../{BasicCell.tsx => TableCell.tsx} | 26 ++++++++++++++++--- src/components/fields/Percentage/index.tsx | 17 +++++++++--- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/components/fields/Percentage/Settings.tsx rename src/components/fields/Percentage/{BasicCell.tsx => TableCell.tsx} (54%) diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx new file mode 100644 index 000000000..3aa6b5766 --- /dev/null +++ b/src/components/fields/Percentage/Settings.tsx @@ -0,0 +1,10 @@ +import { ISettingsProps } from "@src/components/fields/types"; + +export default function Settings({ onChange, config }: ISettingsProps) { + return ( + <> + {`Config: ${JSON.stringify(config)}`} + {Object.keys(checkStates).map((key) => { - const color = defaultColors[key]; + const index = Number(key); + const colorHex = colorsDraft[Number(key)]; return ( {colorLabels[key]} @@ -149,21 +156,25 @@ export default function Settings({ onChange, config }: ISettingsProps) { fieldSx, { width: "auto", marginRight: "0.5rem", boxShadow: "none" }, ]} - checked={checkStates[key]} - onChange={() => onCheckboxChange(key, !checkStates[key])} + checked={checkStates[index]} + onChange={() => onCheckboxChange(key, !checkStates[index])} /> setActivePicker(activePicker)} - color={color} - disabled={!checkStates[key]} + color={colorHex || "#fff"} + disabled={!checkStates[index]} > - handleOnChange(key, color)} - disabled={!checkStates[key]} - /> + {colorHex && ( + + handleColorChange(key, color) + } + disabled={!checkStates[index]} + /> + )} @@ -188,7 +199,11 @@ const Preview = ({ colors }: { colors: any }) => { width: "100%", padding: "0.5rem 0", color: theme.palette.text.primary, - backgroundColor: resultColorsScale(value, colors).toHex(), + backgroundColor: resultColorsScale( + value, + colors, + theme.palette.background.paper + ).toHex(), opacity: 0.5, }} > diff --git a/src/components/fields/Percentage/SideDrawerField.tsx b/src/components/fields/Percentage/SideDrawerField.tsx index 29673a4f7..516682a36 100644 --- a/src/components/fields/Percentage/SideDrawerField.tsx +++ b/src/components/fields/Percentage/SideDrawerField.tsx @@ -3,7 +3,6 @@ import { ISideDrawerFieldProps } from "@src/components/fields/types"; import { TextField, InputAdornment, Box } from "@mui/material"; import { resultColorsScale } from "@src/utils/color"; import { getFieldId } from "@src/components/SideDrawer/utils"; -import { configDefaults } from "@src/components/fields/Percentage"; export default function Percentage({ column, @@ -12,11 +11,7 @@ export default function Percentage({ onSubmit, disabled, }: ISideDrawerFieldProps) { - const { colors }: { colors: string[] } = { - ...configDefaults, - ...(column as any).config, - }; - + const { colors } = (column as any).config; return (
- value <= 0.5 - ? colord(colors[0]).mix(colors[1], value * 2) - : colord(colors[1]).mix(colors[2], (value - 0.5) * 2); +export const defaultColors = ["#ED4747", "#F3C900", "#1FAD5F"]; +export const resultColorsScale = ( + value: number, + colors: any = defaultColors, + defaultColor: string = "#fff" +) => { + return value <= 0.5 + ? colord(colors[0] || defaultColor).mix( + colors[1] || defaultColor, + value * 2 + ) + : colord(colors[1] || defaultColor).mix( + colors[2] || defaultColor, + (value - 0.5) * 2 + ); +}; From 5069a75e2b4e00161396e72ce458029c34c4a76c Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Wed, 29 Jun 2022 09:16:26 +0300 Subject: [PATCH 14/26] feat(percentage-c11n): fix defaults --- src/components/fields/Percentage/Settings.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index c10d2b37e..0f5aa0138 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -111,7 +111,8 @@ export default function Settings({ onChange, config }: ISettingsProps) { useEffect(() => { onChange("colors")(colorsDraft); - }, [colorsDraft, onChange]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [colorsDraft]); const onCheckboxChange = (index: string, checked: boolean) => { setColorsDraft( @@ -137,7 +138,6 @@ export default function Settings({ onChange, config }: ISettingsProps) { return ( <> {JSON.stringify(config)} - {Object.keys(checkStates).map((key) => { const index = Number(key); const colorHex = colorsDraft[Number(key)]; @@ -152,11 +152,11 @@ export default function Settings({ onChange, config }: ISettingsProps) { }} > onCheckboxChange(key, !checkStates[index])} /> - {colorHex && ( + {colorHex ? ( @@ -174,6 +174,8 @@ export default function Settings({ onChange, config }: ISettingsProps) { } disabled={!checkStates[index]} /> + ) : ( + <> )} From 531b996018a8e1c1b0dfba5473b91196ca9c09e5 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Wed, 29 Jun 2022 09:28:49 +0300 Subject: [PATCH 15/26] feat(percentage-c11n): add basic cell without bg --- .../fields/Percentage/BasicCell.tsx | 23 +++++++++++++++++++ src/components/fields/Percentage/index.tsx | 5 +++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/components/fields/Percentage/BasicCell.tsx diff --git a/src/components/fields/Percentage/BasicCell.tsx b/src/components/fields/Percentage/BasicCell.tsx new file mode 100644 index 000000000..8ee0f9a0b --- /dev/null +++ b/src/components/fields/Percentage/BasicCell.tsx @@ -0,0 +1,23 @@ +import { IBasicCellProps } from "@src/components/fields/types"; + +import { useTheme } from "@mui/material"; + +export default function Percentage({ value }: IBasicCellProps) { + const theme = useTheme(); + + const percentage = typeof value === "number" ? value : 0; + return ( + <> +
+ {Math.round(percentage * 100)}% +
+ + ); +} diff --git a/src/components/fields/Percentage/index.tsx b/src/components/fields/Percentage/index.tsx index d53e993bc..f353774fc 100644 --- a/src/components/fields/Percentage/index.tsx +++ b/src/components/fields/Percentage/index.tsx @@ -3,11 +3,14 @@ import { IFieldConfig, FieldType } from "@src/components/fields/types"; import withHeavyCell from "@src/components/fields/_withTableCell/withHeavyCell"; import { Percentage as PercentageIcon } from "@src/assets/icons"; -import BasicCell from "@src/components/fields/_BasicCell/BasicCellNull"; import TextEditor from "@src/components/Table/editors/TextEditor"; import { filterOperators } from "@src/components/fields/Number/Filter"; import BasicContextMenuActions from "@src/components/fields/_BasicCell/BasicCellContextMenuActions"; +const BasicCell = lazy( + () => import("./BasicCell" /* webpackChunkName: "BasicCell-Percentage" */) +); + const TableCell = lazy( () => import("./TableCell" /* webpackChunkName: "TableCell-Percentage" */) ); From aa8d086c2cac0c22991b6beebc372ba49f20689a Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Wed, 29 Jun 2022 16:21:57 +0300 Subject: [PATCH 16/26] feat(percentage-c11n): remove collapse --- src/components/ColorPickerInput.tsx | 1 - src/components/fields/Percentage/Settings.tsx | 155 ++++++------------ 2 files changed, 54 insertions(+), 102 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index 5b5a92d20..7ed1dd3f7 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -65,7 +65,6 @@ export default function ColorPickerInput({ sx={[ fieldSx, { - marginTop: "1rem", padding: "1rem", borderColor: "divider", transitionDuration: 0, diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index 0f5aa0138..a9bc98984 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -2,10 +2,10 @@ import { useEffect, useState } from "react"; import { Box, - ButtonBase, Checkbox, - Collapse, InputLabel, + MenuItem, + TextField, useTheme, } from "@mui/material"; import ColorPickerInput from "@src/components/ColorPickerInput"; @@ -13,88 +13,12 @@ import { ISettingsProps } from "@src/components/fields/types"; import { Color, toColor } from "react-color-palette"; import { fieldSx } from "@src/components/SideDrawer/utils"; -import { ChevronDown } from "mdi-material-ui"; -import { ReactElement } from "react-markdown/lib/react-markdown"; import { resultColorsScale, defaultColors } from "@src/utils/color"; -const ColorPickerCollapse = ({ - colorKey, - color, - active, - setActive, - disabled, - children, -}: { - colorKey: string; - color: string; - active: boolean; - setActive: (activeKey: string | null) => void; - disabled?: boolean; - children?: ReactElement; -}) => { - const toggleCollapse = () => !disabled && setActive(active ? null : colorKey); - - const toggleState = (() => { - if (disabled && active) { - setActive(null); - return false; - } - return active; - })(); - - return ( - - - theme.transitions.create("border-radius", { - delay: theme.transitions.duration.standard, - }), - "&.Mui-disabled": { color: "text.disabled" }, - }, - active && { - transitionDelay: "0s", - transitionDuration: "0s", - }, - ]} - > - `0 0 0 1px ${theme.palette.divider} inset`, - borderRadius: 0.5, - opacity: 0.5, - }} - /> -
{color}
- theme.transitions.create("transform"), - transform: active ? "rotate(180deg)" : "none", - }} - /> -
- {children} -
- ); -}; - const colorLabels: { [key: string]: string } = { 0: "No", - 1: "Yes", - 2: "Maybe", + 1: "Maybe", + 2: "Yes", }; export default function Settings({ onChange, config }: ISettingsProps) { @@ -107,7 +31,6 @@ export default function Settings({ onChange, config }: ISettingsProps) { 1: colorsDraft[1], 2: colorsDraft[2], }); - const [activePicker, setActivePicker] = useState(null); useEffect(() => { onChange("colors")(colorsDraft); @@ -143,11 +66,10 @@ export default function Settings({ onChange, config }: ISettingsProps) { const colorHex = colorsDraft[Number(key)]; return ( - {colorLabels[key]} @@ -155,29 +77,60 @@ export default function Settings({ onChange, config }: ISettingsProps) { checked={Boolean(checkStates[key])} sx={[ fieldSx, - { width: "auto", marginRight: "0.5rem", boxShadow: "none" }, + { + width: "auto", + boxShadow: "none", + backgroundColor: "inherit", + "&:hover": { + backgroundColor: "inherit", + }, + }, ]} onChange={() => onCheckboxChange(key, !checkStates[index])} /> - setActivePicker(activePicker)} - color={colorHex || "#fff"} - disabled={!checkStates[index]} + - {colorHex ? ( - - handleColorChange(key, color) - } - disabled={!checkStates[index]} - /> - ) : ( - <> + + {checkStates[key] && ( + + + `0 0 0 1px ${theme.palette.divider} inset`, + borderRadius: 0.5, + opacity: 0.5, + }} + /> + {colorHex} + + )} + + {colorHex && ( + <> + + handleColorChange(key, color) + } + disabled={!checkStates[index]} + /> + )} - + ); From 5161c7cfbdbe013e471b617450817cb7d09317b6 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Wed, 29 Jun 2022 16:31:08 +0300 Subject: [PATCH 17/26] feat(percentage-c11n): refactor checkStates --- src/components/fields/Percentage/Settings.tsx | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index a9bc98984..5417d5025 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -26,46 +26,41 @@ export default function Settings({ onChange, config }: ISettingsProps) { config.colors ? config.colors : defaultColors ); - const [checkStates, setCheckStates] = useState<{ [key: string]: boolean }>({ - 0: colorsDraft[0], - 1: colorsDraft[1], - 2: colorsDraft[2], - }); + const [checkStates, setCheckStates] = useState([ + colorsDraft[0], + colorsDraft[1], + colorsDraft[2], + ]); useEffect(() => { onChange("colors")(colorsDraft); // eslint-disable-next-line react-hooks/exhaustive-deps }, [colorsDraft]); - const onCheckboxChange = (index: string, checked: boolean) => { + const onCheckboxChange = (index: number, checked: boolean) => { setColorsDraft( colorsDraft.map((value: any, idx: number) => - Number(index) === idx - ? checked - ? value || defaultColors[idx] - : null - : value + index === idx ? (checked ? value || defaultColors[idx] : null) : value ) ); - setCheckStates({ ...checkStates, [index]: checked }); + setCheckStates( + checkStates.map((value, idx) => (index === idx ? checked : value)) + ); }; - const handleColorChange = (key: string, color: Color): void => { + const handleColorChange = (index: number, color: Color): void => { setColorsDraft( - colorsDraft.map((value, index) => - index === Number(key) ? color.hex : value - ) + colorsDraft.map((value, idx) => (index === idx ? color.hex : value)) ); }; return ( <> {JSON.stringify(config)} - {Object.keys(checkStates).map((key) => { - const index = Number(key); - const colorHex = colorsDraft[Number(key)]; + {checkStates.map((checked: boolean, index: number) => { + const colorHex = colorsDraft[index]; return ( - + onCheckboxChange(key, !checkStates[index])} + onChange={() => onCheckboxChange(index, !checked)} /> - {checkStates[key] && ( + {checked && ( - handleColorChange(key, color) + handleColorChange(index, color) } disabled={!checkStates[index]} /> From 8fac4b2a1a5372a480508ac765bf8da9ad5c30b0 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Thu, 30 Jun 2022 02:34:00 +0300 Subject: [PATCH 18/26] feat(percentage-c11n): add grid layout --- src/components/ColorPickerInput.tsx | 11 +- src/components/fields/Percentage/Settings.tsx | 174 ++++++++++-------- 2 files changed, 100 insertions(+), 85 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index 7ed1dd3f7..9815d42dd 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -5,9 +5,8 @@ import { MutableRefObject, useLayoutEffect, } from "react"; -import { Box } from "@mui/material"; +import { Box, useTheme } from "@mui/material"; -import { fieldSx } from "@src/components/SideDrawer/utils"; import { Color, ColorPicker } from "react-color-palette"; import { useDebouncedCallback } from "use-debounce"; @@ -50,7 +49,7 @@ export default function ColorPickerInput({ }: IColorPickerProps) { const [localValue, setLocalValue] = useState(value); const [width, setRef] = useResponsiveWidth(); - + const theme = useTheme(); const debouncedOnChangeComplete = useDebouncedCallback((color) => { handleOnChangeComplete(color); }, 100); @@ -63,10 +62,8 @@ export default function ColorPickerInput({ setLocalValue(color)} /> diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index 5417d5025..3a7d944ec 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -3,9 +3,11 @@ import { useEffect, useState } from "react"; import { Box, Checkbox, + Grid, InputLabel, MenuItem, TextField, + Typography, useTheme, } from "@mui/material"; import ColorPickerInput from "@src/components/ColorPickerInput"; @@ -14,6 +16,7 @@ import { ISettingsProps } from "@src/components/fields/types"; import { Color, toColor } from "react-color-palette"; import { fieldSx } from "@src/components/SideDrawer/utils"; import { resultColorsScale, defaultColors } from "@src/utils/color"; +import { GlobalStyles } from "tss-react"; const colorLabels: { [key: string]: string } = { 0: "No", @@ -56,80 +59,83 @@ export default function Settings({ onChange, config }: ISettingsProps) { return ( <> - {JSON.stringify(config)} - {checkStates.map((checked: boolean, index: number) => { - const colorHex = colorsDraft[index]; - return ( - - - onCheckboxChange(index, !checked)} - /> - + + {checkStates.map((checked: boolean, index: number) => { + const colorHex = colorsDraft[index]; + return ( + + - - {checked && ( - - - `0 0 0 1px ${theme.palette.divider} inset`, - borderRadius: 0.5, - opacity: 0.5, - }} + onCheckboxChange(index, !checked)} + /> + + + {checked && ( + + + `0 0 0 1px ${theme.palette.divider} inset`, + borderRadius: 0.5, + opacity: 0.5, + }} + /> + {colorHex} + + )} + + {colorHex && ( +
+ + handleColorChange(index, color) + } + disabled={!checkStates[index]} /> - {colorHex} - +
)} -
- {colorHex && ( - <> - - handleColorChange(index, color) - } - disabled={!checkStates[index]} - /> - - )} -
-
-
- ); - })} +
+
+ + ); + })} + ); @@ -140,24 +146,36 @@ const Preview = ({ colors }: { colors: any }) => { return ( Preview: - + {[0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1].map((value) => { return ( - {Math.floor(value * 100)}% + + {Math.floor(value * 100)}% ); })} From 21443cd05c98e30846f957913d09fe44077ef8e4 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Thu, 30 Jun 2022 04:25:02 +0300 Subject: [PATCH 19/26] feat(percentage-c11n): chore conventions --- src/components/ColorPickerInput.tsx | 2 +- src/components/fields/Percentage/Settings.tsx | 123 +++++++++--------- src/utils/color.ts | 6 +- 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index 9815d42dd..4e53c0e72 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -68,7 +68,7 @@ export default function ColorPickerInput({ "& .rcp": { border: "none", "& .rcp-saturation": { - borderRadius: "4px", + borderRadius: theme.spacing(0.5), }, "& .rcp-body": { boxSizing: "unset", diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index 3a7d944ec..2b2afe72b 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -19,9 +19,9 @@ import { resultColorsScale, defaultColors } from "@src/utils/color"; import { GlobalStyles } from "tss-react"; const colorLabels: { [key: string]: string } = { - 0: "No", - 1: "Maybe", - 2: "Yes", + 0: "Start", + 1: "Middle", + 2: "End", }; export default function Settings({ onChange, config }: ISettingsProps) { @@ -70,68 +70,69 @@ export default function Settings({ onChange, config }: ISettingsProps) { {checkStates.map((checked: boolean, index: number) => { const colorHex = colorsDraft[index]; return ( - - - + onCheckboxChange(index, !checked)} - /> - - - {checked && ( - - - `0 0 0 1px ${theme.palette.divider} inset`, - borderRadius: 0.5, - opacity: 0.5, - }} - /> - {colorHex} - - )} - - {colorHex && ( -
- - handleColorChange(index, color) - } - disabled={!checkStates[index]} + }, + ]} + onChange={() => onCheckboxChange(index, !checked)} + /> + + + {checked && ( + + + `0 0 0 1px ${theme.palette.divider} inset`, + borderRadius: 0.5, + opacity: 0.5, + }} /> -
+ {colorHex} +
)} - -
+ + {colorHex && ( +
+ + handleColorChange(index, color) + } + disabled={!checkStates[index]} + /> +
+ )} + ); })} diff --git a/src/utils/color.ts b/src/utils/color.ts index efb668923..686e27160 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -1,12 +1,13 @@ import { colord } from "colord"; export const defaultColors = ["#ED4747", "#F3C900", "#1FAD5F"]; + export const resultColorsScale = ( value: number, colors: any = defaultColors, defaultColor: string = "#fff" -) => { - return value <= 0.5 +) => + value <= 0.5 ? colord(colors[0] || defaultColor).mix( colors[1] || defaultColor, value * 2 @@ -15,4 +16,3 @@ export const resultColorsScale = ( colors[2] || defaultColor, (value - 0.5) * 2 ); -}; From 9fd88b387f69aa88711e36d7a43055dbbf02c9db Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Thu, 30 Jun 2022 05:05:08 +0300 Subject: [PATCH 20/26] feat(percentage-c11n): add default theme color to sidedrawer --- src/components/fields/Percentage/SideDrawerField.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/fields/Percentage/SideDrawerField.tsx b/src/components/fields/Percentage/SideDrawerField.tsx index 516682a36..15b354201 100644 --- a/src/components/fields/Percentage/SideDrawerField.tsx +++ b/src/components/fields/Percentage/SideDrawerField.tsx @@ -1,6 +1,6 @@ import { ISideDrawerFieldProps } from "@src/components/fields/types"; -import { TextField, InputAdornment, Box } from "@mui/material"; +import { TextField, InputAdornment, Box, useTheme } from "@mui/material"; import { resultColorsScale } from "@src/utils/color"; import { getFieldId } from "@src/components/SideDrawer/utils"; @@ -12,6 +12,7 @@ export default function Percentage({ disabled, }: ISideDrawerFieldProps) { const { colors } = (column as any).config; + const theme = useTheme(); return ( - `0 0 0 1px ${theme.palette.divider} inest`, + boxShadow: `0 0 0 1px ${theme.palette.divider} inest`, backgroundColor: typeof value === "number" - ? resultColorsScale(value, colors).toHex() + "!important" + ? resultColorsScale( + value, + colors, + theme.palette.background.paper + ).toHex() + "!important" : undefined, }} /> From 2c1fbca1e9b17bc0aa18d4c665d8b69b04aeb5dc Mon Sep 17 00:00:00 2001 From: Han Tuerker <46192266+htuerker@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:21:03 +0300 Subject: [PATCH 21/26] remove redundant fragment Co-authored-by: Sidney Alcantara --- src/components/fields/Percentage/BasicCell.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/fields/Percentage/BasicCell.tsx b/src/components/fields/Percentage/BasicCell.tsx index 8ee0f9a0b..c2dbef58e 100644 --- a/src/components/fields/Percentage/BasicCell.tsx +++ b/src/components/fields/Percentage/BasicCell.tsx @@ -7,7 +7,6 @@ export default function Percentage({ value }: IBasicCellProps) { const percentage = typeof value === "number" ? value : 0; return ( - <>
{Math.round(percentage * 100)}%
- ); } From bb73673227143923e2254b98b3f38de422aaeddb Mon Sep 17 00:00:00 2001 From: Han Tuerker <46192266+htuerker@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:26:12 +0300 Subject: [PATCH 22/26] fix text color in preview Co-authored-by: Sidney Alcantara --- src/components/fields/Percentage/Settings.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index 2b2afe72b..fddd4a84e 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -176,7 +176,9 @@ const Preview = ({ colors }: { colors: any }) => { opacity: 0.5, }} /> - {Math.floor(value * 100)}% + + {Math.floor(value * 100)}% +
); })} From 18aa406d82498592792e3e7c660bce696545ef95 Mon Sep 17 00:00:00 2001 From: Han Tuerker <46192266+htuerker@users.noreply.github.com> Date: Sat, 2 Jul 2022 03:38:44 +0300 Subject: [PATCH 23/26] fix: change state to derived state Co-authored-by: Sidney Alcantara --- src/components/fields/Percentage/Settings.tsx | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index fddd4a84e..5be2924ba 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -25,23 +25,17 @@ const colorLabels: { [key: string]: string } = { }; export default function Settings({ onChange, config }: ISettingsProps) { - const [colorsDraft, setColorsDraft] = useState( - config.colors ? config.colors : defaultColors - ); + const colors = config.colors ?? defaultColors; + // It makes sense to keep this a state const [checkStates, setCheckStates] = useState([ colorsDraft[0], colorsDraft[1], colorsDraft[2], ]); - useEffect(() => { - onChange("colors")(colorsDraft); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [colorsDraft]); - const onCheckboxChange = (index: number, checked: boolean) => { - setColorsDraft( + onChange("colors")( colorsDraft.map((value: any, idx: number) => index === idx ? (checked ? value || defaultColors[idx] : null) : value ) @@ -52,7 +46,7 @@ export default function Settings({ onChange, config }: ISettingsProps) { }; const handleColorChange = (index: number, color: Color): void => { - setColorsDraft( + onChange("colors")( colorsDraft.map((value, idx) => (index === idx ? color.hex : value)) ); }; From e2bfef27f040633be40d128d387cd6124c5bfd40 Mon Sep 17 00:00:00 2001 From: Han Tuerker Date: Sat, 2 Jul 2022 04:23:30 +0300 Subject: [PATCH 24/26] fix: review suggestions --- src/components/ColorPickerInput.tsx | 9 ++++-- src/components/fields/Percentage/Settings.tsx | 29 ++++++------------- .../fields/Percentage/SideDrawerField.tsx | 2 +- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index 4e53c0e72..ce5d40c2e 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -38,20 +38,21 @@ const useResponsiveWidth = (): [ export interface IColorPickerProps { value: Color; - handleOnChangeComplete: (color: Color) => void; + onChangeComplete: (color: Color) => void; disabled?: boolean; } export default function ColorPickerInput({ value, - handleOnChangeComplete, + onChangeComplete, disabled = false, }: IColorPickerProps) { const [localValue, setLocalValue] = useState(value); const [width, setRef] = useResponsiveWidth(); const theme = useTheme(); + const debouncedOnChangeComplete = useDebouncedCallback((color) => { - handleOnChangeComplete(color); + onChangeComplete(color); }, 100); useEffect(() => { @@ -64,6 +65,7 @@ export default function ColorPickerInput({ sx={[ { padding: theme.spacing(1.5), + paddingTop: theme.spacing(1), transitionDuration: 0, "& .rcp": { border: "none", @@ -82,6 +84,7 @@ export default function ColorPickerInput({ height={150} color={localValue} onChange={(color) => setLocalValue(color)} + onChangeComplete={onChangeComplete} />
); diff --git a/src/components/fields/Percentage/Settings.tsx b/src/components/fields/Percentage/Settings.tsx index 5be2924ba..e28dde2cc 100644 --- a/src/components/fields/Percentage/Settings.tsx +++ b/src/components/fields/Percentage/Settings.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useState } from "react"; import { Box, @@ -16,7 +16,6 @@ import { ISettingsProps } from "@src/components/fields/types"; import { Color, toColor } from "react-color-palette"; import { fieldSx } from "@src/components/SideDrawer/utils"; import { resultColorsScale, defaultColors } from "@src/utils/color"; -import { GlobalStyles } from "tss-react"; const colorLabels: { [key: string]: string } = { 0: "Start", @@ -25,18 +24,15 @@ const colorLabels: { [key: string]: string } = { }; export default function Settings({ onChange, config }: ISettingsProps) { - const colors = config.colors ?? defaultColors; + const colors: string[] = config.colors ?? defaultColors; - // It makes sense to keep this a state - const [checkStates, setCheckStates] = useState([ - colorsDraft[0], - colorsDraft[1], - colorsDraft[2], - ]); + const [checkStates, setCheckStates] = useState( + colors.map(Boolean) + ); const onCheckboxChange = (index: number, checked: boolean) => { onChange("colors")( - colorsDraft.map((value: any, idx: number) => + colors.map((value: any, idx: number) => index === idx ? (checked ? value || defaultColors[idx] : null) : value ) ); @@ -47,22 +43,15 @@ export default function Settings({ onChange, config }: ISettingsProps) { const handleColorChange = (index: number, color: Color): void => { onChange("colors")( - colorsDraft.map((value, idx) => (index === idx ? color.hex : value)) + colors.map((value, idx) => (index === idx ? color.hex : value)) ); }; return ( <> - {checkStates.map((checked: boolean, index: number) => { - const colorHex = colorsDraft[index]; + const colorHex = colors[index]; return ( + onChangeComplete={(color) => handleColorChange(index, color) } disabled={!checkStates[index]} diff --git a/src/components/fields/Percentage/SideDrawerField.tsx b/src/components/fields/Percentage/SideDrawerField.tsx index 15b354201..a6cc43662 100644 --- a/src/components/fields/Percentage/SideDrawerField.tsx +++ b/src/components/fields/Percentage/SideDrawerField.tsx @@ -35,7 +35,7 @@ export default function Percentage({ width: 20, height: 20, borderRadius: 0.5, - boxShadow: `0 0 0 1px ${theme.palette.divider} inest`, + boxShadow: `0 0 0 1px ${theme.palette.divider} inset`, backgroundColor: typeof value === "number" ? resultColorsScale( From 0bd6f638afd5eec506041ddbd907a406feebfe07 Mon Sep 17 00:00:00 2001 From: Han Tuerker <46192266+htuerker@users.noreply.github.com> Date: Mon, 4 Jul 2022 17:13:51 +0300 Subject: [PATCH 25/26] fix: remove redundant change call Co-authored-by: Sidney Alcantara --- src/components/ColorPickerInput.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index ce5d40c2e..29606234c 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -51,13 +51,6 @@ export default function ColorPickerInput({ const [width, setRef] = useResponsiveWidth(); const theme = useTheme(); - const debouncedOnChangeComplete = useDebouncedCallback((color) => { - onChangeComplete(color); - }, 100); - - useEffect(() => { - debouncedOnChangeComplete(localValue); - }, [debouncedOnChangeComplete, localValue]); return ( Date: Mon, 4 Jul 2022 17:18:57 +0300 Subject: [PATCH 26/26] fix(percentage-c11n): remove redundant dependencies --- src/components/ColorPickerInput.tsx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/components/ColorPickerInput.tsx b/src/components/ColorPickerInput.tsx index 29606234c..89ebdb212 100644 --- a/src/components/ColorPickerInput.tsx +++ b/src/components/ColorPickerInput.tsx @@ -1,14 +1,7 @@ -import { - useState, - useEffect, - useRef, - MutableRefObject, - useLayoutEffect, -} from "react"; +import { useState, useRef, MutableRefObject, useLayoutEffect } from "react"; import { Box, useTheme } from "@mui/material"; import { Color, ColorPicker } from "react-color-palette"; -import { useDebouncedCallback } from "use-debounce"; const useResponsiveWidth = (): [ width: number, @@ -51,7 +44,6 @@ export default function ColorPickerInput({ const [width, setRef] = useResponsiveWidth(); const theme = useTheme(); - return (