diff --git a/Common/Types/Color.ts b/Common/Types/Color.ts index ee2775b16b..52abcb08d4 100644 --- a/Common/Types/Color.ts +++ b/Common/Types/Color.ts @@ -19,9 +19,9 @@ export default class Color extends DatabaseProperty { this._color = v; } - public constructor(color: string) { + public constructor(color: string | Color) { super(); - this.color = color; + this.color = color.toString(); } public override toString(): string { diff --git a/Common/UI/Components/Dropdown/Dropdown.tsx b/Common/UI/Components/Dropdown/Dropdown.tsx index 6d1e4e9b6e..5abb8019d3 100644 --- a/Common/UI/Components/Dropdown/Dropdown.tsx +++ b/Common/UI/Components/Dropdown/Dropdown.tsx @@ -31,6 +31,7 @@ export interface DropdownOption { label: string; description?: string; labels?: Array; + color?: Color; } export interface ComponentProps { @@ -266,6 +267,29 @@ const Dropdown: FunctionComponent = ( ); }; + const renderOptionColorIndicator: ( + color?: Color | string, + ) => ReactElement | null = (color?: Color | string): ReactElement | null => { + const normalizedColor: string | undefined = color + ? new Color(color).toString() + : undefined; + + if (!normalizedColor) { + return null; + } + + return ( + + ); + }; + const getLabelStyle: (color?: string) => { backgroundColor: string; color: string; @@ -401,10 +425,13 @@ const Dropdown: FunctionComponent = ( if (meta.context === "value") { return ( -
- - {option.label} - +
+
+ {renderOptionColorIndicator(option.color)} + + {option.label} + +
{renderAssociatedLabels( visibleLabels, meta.context, @@ -416,9 +443,12 @@ const Dropdown: FunctionComponent = ( return (
- - {option.label} - +
+ {renderOptionColorIndicator(option.color)} + + {option.label} + +
{option.description ? ( {option.description} ) : null} diff --git a/Common/UI/Components/Forms/ModelForm.tsx b/Common/UI/Components/Forms/ModelForm.tsx index d6e637cec9..99a9a08a10 100644 --- a/Common/UI/Components/Forms/ModelForm.tsx +++ b/Common/UI/Components/Forms/ModelForm.tsx @@ -414,6 +414,16 @@ const ModelForm: ( [field.dropdownModal.valueField]: true, } as any; + let colorColumnName: string | null = null; + let shouldSelectColorColumn: boolean = false; + + colorColumnName = tempModel.getFirstColorColumn(); + + if (colorColumnName) { + select[colorColumnName] = true; + shouldSelectColorColumn = true; + } + const accessControlColumnName: string | null = tempModel.getAccessControlColumn(); @@ -452,6 +462,15 @@ const ModelForm: ( ].toString(), }; + if (colorColumnName && shouldSelectColorColumn) { + const color: Color = item.getColumnValue( + colorColumnName, + ) as Color; + if (color) { + option.color = color; + } + } + if (accessControlColumnName) { const labelsForItem: Array = ( ((item as any)[ diff --git a/Common/UI/Utils/Dropdown.ts b/Common/UI/Utils/Dropdown.ts index ea6b06a305..6b121c4dc7 100644 --- a/Common/UI/Utils/Dropdown.ts +++ b/Common/UI/Utils/Dropdown.ts @@ -1,6 +1,7 @@ import NumberUtil from "../../Utils/Number"; import { DropdownOption } from "../Components/Dropdown/Dropdown"; import BaseModel from "../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel"; +import Color from "../../Types/Color"; type Enum = Record & { [k: number]: string }; @@ -52,10 +53,25 @@ export default class DropdownUtil { valueField: string; }): Array { return data.array.map((item: TBaseModel) => { - return { + const option: DropdownOption = { label: item.getColumnValue(data.labelField) as string, value: item.getColumnValue(data.valueField) as string, }; + + const colorColumnName: string | null = + typeof item.getFirstColorColumn === "function" + ? item.getFirstColorColumn() + : null; + + if (colorColumnName) { + const color = item.getColumnValue(colorColumnName) as Color; + + if (color) { + option.color = color; + } + } + + return option; }); }