Skip to content

Commit

Permalink
feat(image): allow to use icons in trees
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Nov 25, 2024
1 parent 3d9985d commit 0fc70ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
48 changes: 30 additions & 18 deletions src/widgets/base/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useRef } from "react";
import React, { useRef, useMemo } from "react";
import { Row, Space } from "antd";
import Field from "@/common/Field";
import { Image as ImageOoui } from "@gisce/ooui";
Expand All @@ -12,20 +12,39 @@ import {
import { toBase64, getMimeType } from "@/helpers/filesHelper";
import iconMapper from "@/helpers/iconMapper";
import { useLocale } from "@gisce/react-formiga-components";
import isBase64 from "validator/lib/isBase64";

type Props = {
ooui: ImageOoui;
};

export const ImageRender = ({
ooui,
value,
}: {
ooui: ImageOoui;
value?: string;
}) => {
if (value) {
const Icon: React.ElementType = iconMapper(value) as any;
if (Icon) {
return <Icon />;
} else {
return (
<img
src={`data:image/*;base64,${value}`}
style={{ maxWidth: "100px" }}
/>
);
}
}
};

export const Image = (props: Props) => {
const { ooui } = props;
const { required, id } = ooui;

const Icon: React.ElementType = iconMapper(id) as any;

if (Icon) {
return <Icon />;
if (iconMapper(id)) {
return <ImageRender ooui={ooui} value={id} />;
}

return (
Expand All @@ -47,13 +66,6 @@ export const ImageInput = (props: ImageInputProps) => {
const inputFile = useRef(null);
const { t } = useLocale();

if (value) {
const Icon: React.ElementType = iconMapper(value) as any;
if (Icon) {
return <Icon height={50} />;
}
}

const triggerChange = (changedValue?: string) => {
onChange?.(changedValue);
};
Expand Down Expand Up @@ -83,11 +95,11 @@ export const ImageInput = (props: ImageInputProps) => {
return (
<>
<Row gutter={8} wrap={false} justify="center">
{value && isBase64(value) && (
<img
src={`data:image/*;base64,${value}`}
style={{ maxWidth: "100px" }}
/>
{useMemo(
() => (
<ImageRender ooui={ooui} value={value} />
),
[value, ooui],
)}
<input
type="file"
Expand Down
11 changes: 2 additions & 9 deletions src/widgets/views/Tree/treeComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DatePickerConfig } from "@/common/DatePicker";
import ConnectionProvider from "@/ConnectionProvider";
import { colorFromString } from "@/helpers/formHelper";
import { EmailTagsRender } from "@/widgets/custom/EmailTags";
import { ImageRender } from "@/widgets/base/Image";

export const BooleanComponent = ({
value,
Expand Down Expand Up @@ -113,15 +114,7 @@ export const NumberComponent = ({ value }: { value: number }): ReactElement => {
};

export const ImageComponent = ({ value }: { value: string }): ReactElement => {
return useMemo(
() => (
<img
src={`data:image/*;base64,${value}`}
style={{ maxWidth: "50px", padding: "5px" }}
/>
),
[value],
);
return useMemo(() => <ImageRender value={value} />, [value]);
};

export const TagComponent = ({
Expand Down

0 comments on commit 0fc70ab

Please sign in to comment.