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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag: use colorField to define the tag color #874

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@ant-design/colors": "^7.2.0",
"@ant-design/plots": "^1.0.9",
"@gisce/fiber-diagram": "2.1.1",
"@gisce/ooui": "2.28.0",
"@gisce/ooui": "2.29.0",
"@gisce/react-formiga-components": "1.9.2",
"@gisce/react-formiga-table": "1.9.1",
"@monaco-editor/react": "^4.4.5",
Expand Down
48 changes: 43 additions & 5 deletions src/widgets/custom/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useContext, useState, useEffect, useCallback } from "react";
import Field from "@/common/Field";
import { WidgetProps } from "@/types";
import { Tag as AntdTag, TagProps } from "antd";
Expand All @@ -8,6 +8,9 @@ import {
colorFromBoolean,
getTextAndBackgroundColors,
} from "@/helpers/formHelper";
import { useNetworkRequest } from "@/hooks/useNetworkRequest";
import ConnectionProvider from "@/ConnectionProvider";
import { FormContext, FormContextType } from "@/context/FormContext";
import { useLocale } from "@gisce/react-formiga-components";

function capitalizeFirstLetter(text: string): string {
Expand All @@ -25,7 +28,13 @@ export const Tag = (props: WidgetProps) => {

export const TagInput = (props: any) => {
const { t } = useLocale();
const [readObjects, cancelReadObjectsRequest] = useNetworkRequest(
ConnectionProvider.getHandler().readObjects,
);
const { getFieldValue } = (useContext(FormContext) as FormContextType) || {};
const { ooui, value } = props;
const [color, setColor] = useState<string>("default");

let formattedValue = value;
let colorMethod: any = colorFromString;
let colorValue = value;
Expand All @@ -41,13 +50,42 @@ export const TagInput = (props: any) => {
colorMethod = colorFromBoolean;
}

const updateColor = useCallback(async () => {
if (ooui.colorField) {
if (ooui.fieldType === "many2one" && ooui.raw_props?.relation) {
const response = await readObjects({
model: ooui.raw_props.relation,
ids: [value[0]],
fieldsToRetrieve: [ooui.colorField],
});
const retrievedColor = response[0]?.[ooui.colorField];

if (retrievedColor) {
const sanitizedColor = retrievedColor.includes("#")
? retrievedColor
: `#${retrievedColor}`;
setColor(sanitizedColor);
}
} else {
setColor(getFieldValue(ooui.colorField));
}
} else {
setColor(
ooui.colors === "auto"
? colorMethod(colorValue)
: ooui.colors[colorValue] || colorMethod(colorValue),
);
}
}, [ooui?.colorField]);

useEffect(() => {
updateColor().catch((err) => console.error(err));
}, [ooui?.colorField]);

if (!formattedValue) {
return null;
}
const color =
ooui.colors === "auto"
? colorMethod(colorValue)
: ooui.colors[colorValue] || colorMethod(colorValue);

return <CustomTag color={color}>{formattedValue}</CustomTag>;
};

Expand Down