Skip to content

Commit

Permalink
Merge pull request #277 from gisce/tag-widget
Browse files Browse the repository at this point in the history
Add Tag widget for form and tree
  • Loading branch information
mguellsegarra authored Apr 4, 2023
2 parents b275d70 + e4793bd commit 3bd10b1
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 21 deletions.
18 changes: 9 additions & 9 deletions npm-shrinkwrap.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/react-ooui",
"version": "1.1.31",
"version": "1.1.32",
"files": [
"dist",
"src",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@ant-design/plots": "^1.0.9",
"@gisce/ooui": "^0.17.12",
"@gisce/ooui": "^0.17.13",
"@gisce/react-formiga-table": "^0.5.0",
"@monaco-editor/react": "^4.4.5",
"@tabler/icons-react": "^2.11.0",
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/treeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getTableColumns = (

if (component) {
render = (item: any) => {
return component(item, key, tree.fields[key], context);
return component(item, key, column, context);
};
}

Expand Down Expand Up @@ -68,7 +68,7 @@ const getTableItems = (treeOoui: TreeOoui, results: Array<any>): Array<any> => {
parsedItem[key] = item[key];
} else if (widget instanceof Selection) {
const selection = widget;
parsedItem[key] = selection.selectionValues.get(item[key]);
parsedItem[key] = item[key];
} else if (widget instanceof Many2one) {
parsedItem[key] = item[key] &&
Array.isArray(item[key]) &&
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { Image } from "@/widgets/base/Image";
import showConfirmDialog from "@/ui/ConfirmDialog";
import Dashboard from "@/widgets/views/Dashboard/Dashboard";
import { Tags } from "@/widgets/custom/Tags";
import { Tag } from "@/widgets/custom/Tag";
import { MultiCheckbox } from "./widgets/custom/MultiCheckbox";
import { Radio } from "@/widgets/custom/Radio";
import { Switch } from "@/widgets/custom/Switch";
Expand Down Expand Up @@ -144,6 +145,7 @@ export {
DashboardGridItem,
DashboardGrid,
Tags,
Tag,
MultiCheckbox,
Radio,
Switch,
Expand Down
57 changes: 57 additions & 0 deletions src/stories/custom/Tag.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from "react";

import { Tag as TagOoui } from "@gisce/ooui";
import "antd/dist/antd.css";
import LocaleProvider from "../../context/LocaleContext";
import { TagInput } from "../../widgets/custom/Tag";

export default {
title: "Components/Widgets/Custom/Tag",
};

export const Default = (): React.ReactElement => {
const ooui = new TagOoui({
name: "status",
string: "Status",
field: "status",
selection: [['draft', 'Draft'], ['open', 'Open']],
widget_props: '{"colors": {"draft": "blue", "open": "green"}}'
});

return (
<LocaleProvider lang="en_US">
<TagInput value="draft" ooui={ooui}/>
</LocaleProvider>
);
};

export const WithoutColor = (): React.ReactElement => {
const ooui = new TagOoui({
name: "status",
string: "Status",
field: "status",
selection: [['draft', 'Draft'], ['open', 'Open']],
});

return (
<LocaleProvider lang="en_US">
<TagInput value="draft" ooui={ooui}/>
</LocaleProvider>
);
};

export const WithColorAuto = (): React.ReactElement => {
const ooui = new TagOoui({
name: "status",
string: "Status",
field: "status",
selection: [['draft', 'Draft'], ['open', 'Open']],
widget_props: '{"colors": "auto"}'
});

return (
<LocaleProvider lang="en_US">
<TagInput value="draft" ooui={ooui}/>
</LocaleProvider>
);
};
10 changes: 5 additions & 5 deletions src/stories/views/Tree.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Default.args = {
lang: false,
name: "AGRI ENERGIA ELECTRICA, S.A.",
ref: "0112",
title: false,
title: "corp",
numeric: 30.1,
progressbar: 100,
},
Expand All @@ -41,7 +41,7 @@ Default.args = {
lang: false,
name: "Agrolait",
ref: false,
title: false,
title: "ltd",
numeric: 30.1,
progressbar: 70.434331,
},
Expand Down Expand Up @@ -85,14 +85,14 @@ Default.args = {
lang: false,
name: "Aragón",
ref: "02",
title: false,
title: "corp",
numeric: 30.1,
progressbar: 20.01,
},
],
treeView: {
arch:
'<tree string="Partners">\n<field name="numeric" sum="Numeric"/><field name="name"/>\n <field name="title"/>\n <field name="ref"/>\n <field name="city" select="2"/>\n <field name="country" select="2"/>\n <field name="lang"/>\n <field name="progressbar" string="Progress" /> </tree>',
`<tree string="Partners">\n<field name="numeric" sum="Numeric"/><field name="name"/>\n <field name="title" widget="tag" widget_props="{'colors': {'corp': 'red', 'ltd': 'green'}}"/>\n <field name="ref"/>\n <field name="city" select="2"/>\n <field name="country" select="2"/>\n <field name="lang"/>\n <field name="progressbar" string="Progress" /> </tree>`,
fields: {
progressbar: {
string: "Progress bar",
Expand Down Expand Up @@ -149,7 +149,7 @@ Default.args = {
},
title: {
selection: [
["Corp.", "Corp."],
["corp", "Corp."],
["ltd", "Ltd"],
["", ""],
],
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/WidgetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Radio,
Switch,
Steps,
Tag,
CodeEditor,
} from "@/index";
import { Image } from "./base/Image";
Expand Down Expand Up @@ -99,6 +100,8 @@ const getWidgetType = (type: string) => {
return Indicator;
case "tags":
return Tags;
case "tag":
return Tag;
case "multicheckbox":
return MultiCheckbox;
case "radio":
Expand Down
24 changes: 24 additions & 0 deletions src/widgets/custom/Tag.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Field from "@/common/Field";
import { WidgetProps } from "@/types";
import { Tag as AntdTag } from 'antd';
import { colorFromString } from "@/helpers/formHelper";


export const Tag = (props: WidgetProps) => {
return (
<Field {...props}>
<TagInput {...props}/>
</Field>
);
};

export const TagInput = (props) => {
const {ooui, value} = props;
if (!value) {
return null
}
const color = ooui.colors === "auto" ? colorFromString(value) : ooui.colors[value]
return (
<AntdTag color={color}>{ooui.selectionValues.get(value)}</AntdTag>
);
}
32 changes: 29 additions & 3 deletions src/widgets/views/Tree.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useEffect, useRef, useState } from "react";
import { Pagination, Checkbox, Space, Row, Col, Spin } from "antd";
import { Pagination, Checkbox, Space, Row, Col, Spin, Tag } from "antd";
import { getTree, getTableColumns, getTableItems } from "@/helpers/treeHelper";
import { Tree as TreeOoui } from "@gisce/ooui";

Expand Down Expand Up @@ -92,17 +92,41 @@ const imageComponent = (value: string): React.ReactElement => {
);
};

const TagComponent = (
value: any,
key: string,
ooui: any,
context: any
): React.ReactElement => {
return (
<>
{value ? <Tag color={ooui.colors[value]}>{ooui.selectionValues.get(value)}</Tag> : null}
</>
);
};

const SelectionComponent = (
value: any,
key: string,
ooui: any,
context: any
): React.ReactElement => {
return (
<>{ooui.selectionValues.get(value)}</>
);
};

const referenceComponent = (
value: any,
key: string,
fieldDefinitions: any,
ooui: any,
context: any
): React.ReactElement => {
return (
<>
<ReferenceTree
value={value}
selectionValues={new Map(fieldDefinitions?.selection)}
selectionValues={ooui.selectionValues}
context={context}
/>
</>
Expand Down Expand Up @@ -164,6 +188,8 @@ function Tree(props: Props): React.ReactElement {
integer: numberComponent,
float: numberComponent,
reference: referenceComponent,
tag: TagComponent,
selection: SelectionComponent,
},
context
);
Expand Down

0 comments on commit 3bd10b1

Please sign in to comment.