Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Percentage field color customization #692

Merged
merged 30 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8644367
Merge pull request #685 from rowyio/develop
shamsmosowi Jun 16, 2022
40e259e
Merge branch 'develop' into rc
notsidney Jun 16, 2022
ad586e7
Merge branch 'develop' into rc
shamsmosowi Jun 17, 2022
57e758d
Merge pull request #686 from rowyio/rc
shamsmosowi Jun 17, 2022
f9e5be7
feat(percentage-c11n): convert to table cell
Jun 22, 2022
4882048
feat(percentage-c11n): add logic to default configs
Jun 22, 2022
f8c4269
feat(percentage-c11n): add color picker to settings
Jun 22, 2022
976dd7f
feat(percentage-c11n): change default colors
Jun 22, 2022
9bd1a4c
feat(percentage-c11n): fix button text color
Jun 22, 2022
52964a8
feat(percentage-c11n): add labels to settings
Jun 22, 2022
d74ac13
feat(percentage-c11n): add preview section
Jun 22, 2022
8854965
feat(percentage-c11n): fix cache issues with debouncing
Jun 23, 2022
13db993
feat(percentage-c11n): add width responsiveness to color picker
Jun 23, 2022
c25f420
feat(percentage-c11n): fix responsiveness issues
Jun 25, 2022
5afd5c7
feat(percentage-c11n): add checkbox, refactor a little
Jun 25, 2022
7ca47fa
feat(percentage-c11n): convert data type to array
Jun 28, 2022
591ccb2
feat(percentage-c11n): refactor config states
Jun 29, 2022
5069a75
feat(percentage-c11n): fix defaults
Jun 29, 2022
531b996
feat(percentage-c11n): add basic cell without bg
Jun 29, 2022
aa8d086
feat(percentage-c11n): remove collapse
Jun 29, 2022
5161c7c
feat(percentage-c11n): refactor checkStates
Jun 29, 2022
8fac4b2
feat(percentage-c11n): add grid layout
Jun 29, 2022
21443cd
feat(percentage-c11n): chore conventions
Jun 30, 2022
9fd88b3
feat(percentage-c11n): add default theme color to sidedrawer
Jun 30, 2022
2c1fbca
remove redundant fragment
htuerker Jun 30, 2022
bb73673
fix text color in preview
htuerker Jun 30, 2022
18aa406
fix: change state to derived state
htuerker Jul 2, 2022
e2bfef2
fix: review suggestions
Jul 2, 2022
0bd6f63
fix: remove redundant change call
htuerker Jul 4, 2022
d015144
fix(percentage-c11n): remove redundant dependencies
Jul 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(percentage-c11n): refactor config states
  • Loading branch information
Han Tuerker committed Jun 29, 2022
commit 591ccb2c4c1d0496f3ded6a7dfb96ac4b53d3ea9
87 changes: 51 additions & 36 deletions src/components/fields/Percentage/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react";
import { useEffect, useState } from "react";

import {
Box,
Expand All @@ -15,7 +15,7 @@ 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 } from "@src/utils/color";
import { resultColorsScale, defaultColors } from "@src/utils/color";

const ColorPickerCollapse = ({
colorKey,
Expand Down Expand Up @@ -54,7 +54,6 @@ const ColorPickerCollapse = ({
{
justifyContent: "flex-start",
"&&": { pl: 0.75, pr: 0.5 },
color: color,
transition: (theme) =>
theme.transitions.create("border-radius", {
delay: theme.transitions.duration.standard,
Expand Down Expand Up @@ -92,48 +91,56 @@ const ColorPickerCollapse = ({
);
};

const defaultColors: { [key: string]: string } = {
0: "#ED4747",
1: "#F3C900",
2: "#1FAD5F",
};

const colorLabels: { [key: string]: string } = {
0: "No",
1: "Yes",
2: "Maybe",
};

export default function Settings({ onChange, config }: ISettingsProps) {
const { current: colorsDraft } = useRef<any>([]);
const [colorsDraft, setColorsDraft] = useState<any[]>(
config.colors ? config.colors : defaultColors
);

const [checkStates, setCheckStates] = useState<{ [key: string]: boolean }>({
0: false,
1: false,
2: false,
0: colorsDraft[0],
1: colorsDraft[1],
2: colorsDraft[2],
});
const [activePicker, setActivePicker] = useState<string | null>(null);

const onCheckboxChange = (key: string, checked: boolean) => {
if (checked) {
colorsDraft[key] = defaultColors[key];
onChange("colors")(colorsDraft);
} else {
colorsDraft[key] = null;
onChange("colors")(colorsDraft);
}
setCheckStates({ ...checkStates, [key]: checked });
useEffect(() => {
onChange("colors")(colorsDraft);
}, [colorsDraft, onChange]);

const onCheckboxChange = (index: string, checked: boolean) => {
setColorsDraft(
colorsDraft.map((value: any, idx: number) =>
Number(index) === idx
? checked
? value || defaultColors[idx]
: null
: value
)
);
setCheckStates({ ...checkStates, [index]: checked });
};

const handleOnChange = (key: string, color: Color): void => {
colorsDraft[key] = color.hex;
onChange("colors")(colorsDraft);
const handleColorChange = (key: string, color: Color): void => {
setColorsDraft(
colorsDraft.map((value, index) =>
index === Number(key) ? color.hex : value
)
);
};

return (
<>
{JSON.stringify(config)}
<button onClick={() => onChange("colors")([])}>reset</button>
{Object.keys(checkStates).map((key) => {
const color = defaultColors[key];
const index = Number(key);
const colorHex = colorsDraft[Number(key)];
return (
<Box key={key} sx={{ display: "flex", flexDirection: "column" }}>
{colorLabels[key]}
Expand All @@ -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])}
/>
<ColorPickerCollapse
colorKey={key}
active={activePicker === key}
setActive={(activePicker) => setActivePicker(activePicker)}
color={color}
disabled={!checkStates[key]}
color={colorHex || "#fff"}
disabled={!checkStates[index]}
htuerker marked this conversation as resolved.
Show resolved Hide resolved
>
<ColorPickerInput
value={toColor("hex", color)}
handleOnChangeComplete={(color) => handleOnChange(key, color)}
disabled={!checkStates[key]}
/>
{colorHex && (
<ColorPickerInput
value={toColor("hex", colorHex)}
handleOnChangeComplete={(color) =>
handleColorChange(key, color)
}
disabled={!checkStates[index]}
/>
)}
</ColorPickerCollapse>
</Box>
</Box>
Expand All @@ -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,
}}
>
Expand Down
7 changes: 1 addition & 6 deletions src/components/fields/Percentage/SideDrawerField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
<TextField
variant="filled"
Expand Down
12 changes: 6 additions & 6 deletions src/components/fields/Percentage/TableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { IHeavyCellProps } from "@src/components/fields/types";

import { useTheme } from "@mui/material";
import { resultColorsScale } from "@src/utils/color";
import { configDefaults } from "@src/components/fields/Percentage";

export default function Percentage({ column, value }: IHeavyCellProps) {
const theme = useTheme();
const { colors }: { colors: string[] } = {
...configDefaults,
...(column as any).config,
};
const { colors } = (column as any).config;

const percentage = typeof value === "number" ? value : 0;
return (
<>
<div
style={{
backgroundColor: resultColorsScale(percentage, colors).toHex(),
backgroundColor: resultColorsScale(
percentage,
colors,
theme.palette.background.paper
).toHex(),
position: "absolute",
top: 0,
right: 0,
Expand Down
22 changes: 16 additions & 6 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { colord } from "colord";

const defaultColors = ["#ED4747", "#F3C900", "#1FAD5F"];

export const resultColorsScale = (value: number, colors: any = defaultColors) =>
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
);
};