@gisce/react-ooui 2.0.0-alpha.54 → 2.0.0-alpha.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/helpers/treeHelper.d.ts.map +1 -1
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/react-ooui.es.js +3248 -3144
  5. package/dist/react-ooui.es.js.map +1 -1
  6. package/dist/ui/TitleHeader.d.ts.map +1 -1
  7. package/dist/widgets/base/one2many/One2manyInput.d.ts.map +1 -1
  8. package/dist/widgets/modals/SearchModal.d.ts.map +1 -1
  9. package/dist/widgets/views/Dashboard/DashboardTree.d.ts.map +1 -1
  10. package/dist/widgets/views/SearchTree.d.ts.map +1 -1
  11. package/dist/widgets/views/{Tree.d.ts → Tree/Tree.d.ts} +6 -5
  12. package/dist/widgets/views/Tree/Tree.d.ts.map +1 -0
  13. package/dist/widgets/views/Tree/index.d.ts +2 -0
  14. package/dist/widgets/views/Tree/index.d.ts.map +1 -0
  15. package/dist/widgets/views/Tree/treeComponents.d.ts +131 -0
  16. package/dist/widgets/views/Tree/treeComponents.d.ts.map +1 -0
  17. package/package.json +2 -2
  18. package/src/helpers/{treeHelper.ts → treeHelper.tsx} +25 -4
  19. package/src/index.ts +1 -1
  20. package/src/ui/TitleHeader.tsx +10 -1
  21. package/src/widgets/base/one2many/One2manyInput.tsx +5 -4
  22. package/src/widgets/modals/SearchModal.tsx +2 -4
  23. package/src/widgets/views/Dashboard/DashboardTree.tsx +3 -1
  24. package/src/widgets/views/Graph/GraphDefaults.ts +1 -1
  25. package/src/widgets/views/SearchTree.tsx +21 -11
  26. package/src/widgets/views/Tree/Tree.tsx +295 -0
  27. package/src/widgets/views/Tree/index.ts +1 -0
  28. package/src/widgets/views/Tree/treeComponents.tsx +276 -0
  29. package/dist/widgets/views/Tree.d.ts.map +0 -1
  30. package/src/widgets/views/Tree.tsx +0 -450
@@ -0,0 +1,295 @@
1
+ import { memo, useContext, useEffect, useMemo, useRef, useState } from "react";
2
+ import { Pagination as AntPagination, Row, Col, Spin, Badge } from "antd";
3
+ import {
4
+ getTree,
5
+ getTableColumns,
6
+ getTableItems,
7
+ hasActualValues,
8
+ } from "@/helpers/treeHelper";
9
+ import { Tree as TreeOoui } from "@gisce/ooui";
10
+ import { TreeView } from "@/types";
11
+ import { LocaleContext, LocaleContextType } from "@/context/LocaleContext";
12
+ import { calculateColumnsWidth } from "@/helpers/dynamicColumnsHelper";
13
+ import { Table as GisceTable } from "@gisce/react-formiga-table";
14
+ import {
15
+ PlusSquareOutlined,
16
+ MinusSquareOutlined,
17
+ LoadingOutlined,
18
+ } from "@ant-design/icons";
19
+ import {
20
+ ActionViewContext,
21
+ ActionViewContextType,
22
+ } from "@/context/ActionViewContext";
23
+ import { SelectAllRecordsRow } from "@/common/SelectAllRecordsRow";
24
+ import { COLUMN_COMPONENTS } from "./treeComponents";
25
+ import ErrorBoundary from "antd/es/alert/ErrorBoundary";
26
+
27
+ type Props = {
28
+ total?: number;
29
+ limit: number;
30
+ page?: number;
31
+ loading: boolean;
32
+ treeView: TreeView;
33
+ results: any[];
34
+ showPagination?: boolean;
35
+ onRequestPageChange?: (page: number, pageSize?: number) => void;
36
+ onRowClicked?: (record: any) => void;
37
+ onRowSelectionChange?: (selectedRowKeys: any[]) => void;
38
+ selectedRowKeys?: number[];
39
+ scrollY?: number;
40
+ colorsForResults?: { [key: number]: string };
41
+ statusForResults?: { [key: number]: string };
42
+ onChangeSort?: (results: any) => void;
43
+ sorter?: any;
44
+ onFetchChildrenForRecord?: (item: any) => Promise<any[]>;
45
+ childField?: string;
46
+ rootTree?: boolean;
47
+ context?: any;
48
+ readonly?: boolean;
49
+ onSelectAllRecords?: () => Promise<void>;
50
+ };
51
+ export const Tree = memo((props: Props) => {
52
+ const {
53
+ page = 1,
54
+ limit,
55
+ total,
56
+ treeView,
57
+ results,
58
+ onRequestPageChange,
59
+ loading,
60
+ onRowClicked,
61
+ showPagination = true,
62
+ selectedRowKeys = [],
63
+ onRowSelectionChange,
64
+ scrollY,
65
+ colorsForResults = {},
66
+ statusForResults = {},
67
+ onChangeSort,
68
+ sorter,
69
+ onFetchChildrenForRecord,
70
+ childField,
71
+ rootTree = false,
72
+ context,
73
+ readonly,
74
+ onSelectAllRecords,
75
+ } = props;
76
+
77
+ const [items, setItems] = useState<any[]>([]);
78
+
79
+ const errorInParseColors = useRef<boolean>(false);
80
+
81
+ const [treeOoui, setTreeOoui] = useState<TreeOoui>();
82
+
83
+ const { t } = useContext(LocaleContext) as LocaleContextType;
84
+ const internalLimit = useRef(limit);
85
+
86
+ const actionViewContext = useContext(
87
+ ActionViewContext,
88
+ ) as ActionViewContextType;
89
+ const { title = undefined, setTitle = undefined } =
90
+ (rootTree ? actionViewContext : {}) || {};
91
+
92
+ const columns = useMemo(() => {
93
+ if (!treeOoui) {
94
+ return undefined;
95
+ }
96
+
97
+ return getTableColumns(
98
+ treeOoui,
99
+ {
100
+ ...COLUMN_COMPONENTS,
101
+ },
102
+ context,
103
+ );
104
+ }, [context, treeOoui]);
105
+
106
+ useEffect(() => {
107
+ const treeOoui = getTree(treeView);
108
+ setTreeOoui(treeOoui);
109
+ if (treeOoui.string && title !== treeOoui.string) {
110
+ setTitle?.(treeOoui.string);
111
+ }
112
+ // eslint-disable-next-line react-hooks/exhaustive-deps
113
+ }, [treeView, title]);
114
+
115
+ useEffect(() => {
116
+ if (!treeOoui) {
117
+ return;
118
+ }
119
+ errorInParseColors.current = false;
120
+ const items = getTableItems(treeOoui, results);
121
+ setItems(items);
122
+ // eslint-disable-next-line react-hooks/exhaustive-deps
123
+ }, [results]);
124
+
125
+ useEffect(() => {
126
+ internalLimit.current = limit;
127
+ }, [limit]);
128
+
129
+ const from = (page - 1) * internalLimit.current + 1;
130
+ const to = from - 1 + items.length;
131
+ const summary =
132
+ total === undefined
133
+ ? null
134
+ : total === 0
135
+ ? t("no_results")
136
+ : t("summary")
137
+ .replace("{from}", from?.toString())
138
+ .replace("{to}", to?.toString())
139
+ .replace("{total}", total?.toString());
140
+
141
+ const pagination = useMemo(() => {
142
+ if (!showPagination || treeView.isExpandable) {
143
+ return null;
144
+ }
145
+
146
+ const numberOfVisibleSelectedRows = items?.filter(
147
+ (entry) => selectedRowKeys && selectedRowKeys.includes(entry.id),
148
+ ).length;
149
+
150
+ return loading ? null : total === undefined ? (
151
+ <Spin className="pb-4" />
152
+ ) : (
153
+ <Row align="bottom" className="pb-4">
154
+ <Col span={onSelectAllRecords ? 8 : 12}>
155
+ <AntPagination
156
+ total={total}
157
+ pageSize={
158
+ internalLimit.current === 0 ? total : internalLimit.current
159
+ }
160
+ current={page}
161
+ showSizeChanger={false}
162
+ onChange={onRequestPageChange}
163
+ />
164
+ </Col>
165
+ {onSelectAllRecords && (
166
+ <Col span={8} className="text-center">
167
+ <SelectAllRecordsRow
168
+ numberOfVisibleSelectedRows={numberOfVisibleSelectedRows}
169
+ numberOfRealSelectedRows={selectedRowKeys?.length || 0}
170
+ numberOfTotalRows={items.length}
171
+ totalRecords={total || 0}
172
+ onSelectAllRecords={onSelectAllRecords}
173
+ />
174
+ </Col>
175
+ )}
176
+ <Col span={onSelectAllRecords ? 8 : 12} className="text-right">
177
+ {summary}
178
+ </Col>
179
+ </Row>
180
+ );
181
+ }, [
182
+ items,
183
+ loading,
184
+ onRequestPageChange,
185
+ onSelectAllRecords,
186
+ page,
187
+ selectedRowKeys,
188
+ showPagination,
189
+ summary,
190
+ total,
191
+ treeView.isExpandable,
192
+ ]);
193
+
194
+ const sums = useMemo(() => {
195
+ if (!treeOoui) {
196
+ return null;
197
+ }
198
+ const sumFields = treeOoui.columns
199
+ .filter((it) => it.sum !== undefined)
200
+ .map((it) => {
201
+ return { label: it.sum, field: it.id };
202
+ });
203
+
204
+ if (!sumFields || sumFields.length === 0) {
205
+ return null;
206
+ }
207
+
208
+ const summary: string[] = [];
209
+ const sumItems =
210
+ selectedRowKeys?.length > 0
211
+ ? items.filter((result: any) => {
212
+ return selectedRowKeys.includes(result.id);
213
+ })
214
+ : items;
215
+
216
+ sumFields.forEach((sumField) => {
217
+ const total = sumItems.reduce((prev, current) => {
218
+ if (current[sumField.field] && !isNaN(current[sumField.field]))
219
+ return prev + current[sumField.field];
220
+ else return prev;
221
+ }, 0);
222
+
223
+ summary.push(`${sumField.label}: ${Math.round(total * 100) / 100}`);
224
+ });
225
+
226
+ return <div className="p-1 pb-0 pl-2 mt-2 ">{summary.join(", ")}</div>;
227
+ }, [items, selectedRowKeys, treeOoui]);
228
+
229
+ const dataTable = useMemo(() => {
230
+ if (treeOoui !== null && columns && columns.length > 0) {
231
+ const maxWidthPerCell = 600;
232
+ return calculateColumnsWidth(columns, items, maxWidthPerCell);
233
+ }
234
+ return undefined;
235
+ }, [columns, items, treeOoui]);
236
+
237
+ const adjustedHeight = useMemo(() => {
238
+ if (scrollY && treeOoui?.columns.some((it: any) => it.sum !== undefined)) {
239
+ return scrollY - 30;
240
+ }
241
+ return scrollY;
242
+ }, [scrollY, treeOoui?.columns]);
243
+
244
+ if (treeOoui === null || !dataTable || dataTable?.columns?.length === 0) {
245
+ return <Spin style={{ padding: "2rem" }} />;
246
+ }
247
+
248
+ return (
249
+ <ErrorBoundary>
250
+ {pagination}
251
+ <GisceTable
252
+ height={adjustedHeight!}
253
+ columns={dataTable.columns}
254
+ dataSource={items}
255
+ loading={loading}
256
+ loadingComponent={<Spin />}
257
+ onRowStyle={(record: any) => {
258
+ if (colorsForResults![record.id]) {
259
+ return { color: colorsForResults![record.id] };
260
+ }
261
+ return undefined;
262
+ }}
263
+ onRowStatus={
264
+ hasActualValues(statusForResults)
265
+ ? (record: any) => {
266
+ if (statusForResults![record.id]) {
267
+ return <Badge color={statusForResults[record.id]} />;
268
+ }
269
+ return undefined;
270
+ }
271
+ : undefined
272
+ }
273
+ onRowDoubleClick={onRowClicked}
274
+ onRowSelectionChange={onRowSelectionChange}
275
+ onChangeSort={onChangeSort}
276
+ sorter={sorter}
277
+ readonly={readonly}
278
+ expandableOpts={
279
+ onFetchChildrenForRecord
280
+ ? {
281
+ expandIcon: PlusSquareOutlined,
282
+ collapseIcon: MinusSquareOutlined,
283
+ loadingIcon: LoadingOutlined,
284
+ onFetchChildrenForRecord,
285
+ childField: childField!,
286
+ }
287
+ : undefined
288
+ }
289
+ />
290
+ {sums}
291
+ </ErrorBoundary>
292
+ );
293
+ });
294
+
295
+ Tree.displayName = "Tree";
@@ -0,0 +1 @@
1
+ export * from "./Tree";
@@ -0,0 +1,276 @@
1
+ import { ReactElement, useCallback, useEffect, useMemo, useState } from "react";
2
+ import { Checkbox, Spin } from "antd";
3
+ import { parseFloatToString } from "@/helpers/timeHelper";
4
+ import { ProgressBarInput } from "../../base/ProgressBar";
5
+ import { One2manyValue } from "../../base/one2many/One2manyInput";
6
+ import { Interweave } from "interweave";
7
+ import { Many2oneTree } from "../../base/many2one/Many2oneTree";
8
+ import { ReferenceTree } from "../../base/ReferenceTree";
9
+ import dayjs from "@/helpers/dayjs";
10
+ import Avatar from "../../custom/Avatar";
11
+ import { CustomTag, TagInput } from "../../custom/Tag";
12
+ import { DatePickerConfig } from "@/common/DatePicker";
13
+ import ConnectionProvider from "@/ConnectionProvider";
14
+ import { colorFromString } from "@/helpers/formHelper";
15
+
16
+ export const BooleanComponent = ({
17
+ value,
18
+ }: {
19
+ value: boolean;
20
+ }): ReactElement => {
21
+ return useMemo(
22
+ () => (
23
+ <div
24
+ style={{
25
+ display: "flex",
26
+ justifyContent: "center",
27
+ alignContent: "center",
28
+ }}
29
+ >
30
+ <Checkbox checked={value} disabled />
31
+ </div>
32
+ ),
33
+ [value],
34
+ );
35
+ };
36
+
37
+ export const Many2OneComponent = ({ value }: { value: any }): ReactElement => {
38
+ return useMemo(() => <Many2oneTree m2oField={value} />, [value]);
39
+ };
40
+
41
+ export const TextComponent = ({ value }: { value: any }): ReactElement => {
42
+ return useMemo(
43
+ () => (
44
+ <Interweave
45
+ content={value?.toString().replace(/(?:\r\n|\r|\n)/g, "<br>")}
46
+ />
47
+ ),
48
+ [value],
49
+ );
50
+ };
51
+
52
+ export const DateComponent = ({ value }: { value: any }): ReactElement => {
53
+ return useMemo(() => {
54
+ if (!value || (value && value.length === 0)) return <></>;
55
+
56
+ const formattedValue = dayjs(
57
+ value,
58
+ DatePickerConfig.date.dateInternalFormat,
59
+ ).format(DatePickerConfig.date.dateDisplayFormat);
60
+ return <>{formattedValue}</>;
61
+ }, [value]);
62
+ };
63
+
64
+ export const DateTimeComponent = ({ value }: { value: any }): ReactElement => {
65
+ return useMemo(() => {
66
+ if (!value || (value && value.length === 0)) return <></>;
67
+ const formattedValue = dayjs(
68
+ value,
69
+ DatePickerConfig.time.dateInternalFormat,
70
+ ).format(DatePickerConfig.time.dateDisplayFormat);
71
+ return <>{formattedValue}</>;
72
+ }, [value]);
73
+ };
74
+
75
+ export const One2ManyComponent = ({
76
+ value,
77
+ }: {
78
+ value: One2manyValue;
79
+ }): ReactElement => {
80
+ return useMemo(() => {
81
+ const length = Array.isArray(value?.items) ? value?.items.length : 0;
82
+ return <>{`( ${length} )`}</>;
83
+ }, [value]);
84
+ };
85
+
86
+ export const ProgressBarComponent = ({
87
+ value,
88
+ }: {
89
+ value: any;
90
+ }): ReactElement => {
91
+ return useMemo(() => <ProgressBarInput value={value} />, [value]);
92
+ };
93
+
94
+ export const FloatTimeComponent = ({ value }: { value: any }): ReactElement => {
95
+ return useMemo(() => <>{parseFloatToString(value)}</>, [value]);
96
+ };
97
+
98
+ export const NumberComponent = ({ value }: { value: number }): ReactElement => {
99
+ return useMemo(
100
+ () => <div style={{ textAlign: "right" }}>{value}</div>,
101
+ [value],
102
+ );
103
+ };
104
+
105
+ export const ImageComponent = ({ value }: { value: string }): ReactElement => {
106
+ return useMemo(
107
+ () => (
108
+ <img
109
+ src={`data:image/*;base64,${value}`}
110
+ style={{ maxWidth: "50px", padding: "5px" }}
111
+ />
112
+ ),
113
+ [value],
114
+ );
115
+ };
116
+
117
+ export const TagComponent = ({
118
+ value,
119
+ key,
120
+ ooui,
121
+ context,
122
+ }: {
123
+ value: any;
124
+ key: string;
125
+ ooui: any;
126
+ context: any;
127
+ }): ReactElement => {
128
+ return useMemo(() => <TagInput ooui={ooui} value={value} />, [ooui, value]);
129
+ };
130
+
131
+ export const SelectionComponent = ({
132
+ value,
133
+ key,
134
+ ooui,
135
+ context,
136
+ }: {
137
+ value: any;
138
+ key: string;
139
+ ooui: any;
140
+ context: any;
141
+ }): ReactElement => {
142
+ return useMemo(() => <>{ooui.selectionValues.get(value)}</>, [ooui, value]);
143
+ };
144
+
145
+ export const ReferenceComponent = ({
146
+ value,
147
+ key,
148
+ ooui,
149
+ context,
150
+ }: {
151
+ value: any;
152
+ key: string;
153
+ ooui: any;
154
+ context: any;
155
+ }): ReactElement => {
156
+ return useMemo(
157
+ () => (
158
+ <ReferenceTree
159
+ value={value}
160
+ selectionValues={ooui.selectionValues}
161
+ context={context}
162
+ />
163
+ ),
164
+ [context, ooui.selectionValues, value],
165
+ );
166
+ };
167
+
168
+ export const AvatarComponent = ({
169
+ value,
170
+ key,
171
+ ooui,
172
+ context,
173
+ }: {
174
+ value: any;
175
+ key: string;
176
+ ooui: any;
177
+ context: any;
178
+ }): ReactElement => {
179
+ return useMemo(() => <Avatar ooui={ooui} value={value} />, [ooui, value]);
180
+ };
181
+
182
+ export const TagsComponent = ({
183
+ value,
184
+ key,
185
+ ooui,
186
+ context,
187
+ }: {
188
+ value: any;
189
+ key: string;
190
+ ooui: any;
191
+ context: any;
192
+ }): ReactElement => {
193
+ const [values, setValues] = useState<Array<{ id: number; name: string }>>([]);
194
+ const [loading, setLoading] = useState<boolean>(false);
195
+ const { relation, field } = ooui;
196
+
197
+ const loadValues = useCallback(async () => {
198
+ try {
199
+ setLoading(true);
200
+ const optionsRead = await ConnectionProvider.getHandler().search({
201
+ model: relation,
202
+ params: [["id", "in", value.items.map((v: any) => v.id)]],
203
+ fields: [field],
204
+ context,
205
+ });
206
+ setValues(
207
+ optionsRead.map((i: any) => {
208
+ const { id, name } = i;
209
+ return { id, name };
210
+ }),
211
+ );
212
+ } catch (error) {
213
+ console.error("Error loading data", error);
214
+ } finally {
215
+ setLoading(false);
216
+ }
217
+ }, [context, field, relation, value.items]);
218
+
219
+ useEffect(() => {
220
+ if (value?.items && value?.items.length > 0) {
221
+ loadValues();
222
+ }
223
+ // eslint-disable-next-line react-hooks/exhaustive-deps
224
+ }, [value?.items]);
225
+
226
+ const tags = useMemo(
227
+ () =>
228
+ values.map((entry) => {
229
+ const { id, name } = entry;
230
+ const color = colorFromString(name);
231
+ return (
232
+ <CustomTag key={`${id}`} color={color}>
233
+ {name}
234
+ </CustomTag>
235
+ );
236
+ }),
237
+ [values],
238
+ );
239
+
240
+ return useMemo(() => {
241
+ if (loading) {
242
+ return <Spin />;
243
+ }
244
+ return (
245
+ <div
246
+ style={{
247
+ maxWidth: "300px",
248
+ whiteSpace: "break-spaces",
249
+ lineHeight: "30px",
250
+ }}
251
+ >
252
+ {tags}
253
+ </div>
254
+ );
255
+ }, [tags, loading]);
256
+ };
257
+
258
+ export const COLUMN_COMPONENTS = {
259
+ boolean: BooleanComponent,
260
+ many2one: Many2OneComponent,
261
+ text: TextComponent,
262
+ one2many: One2ManyComponent,
263
+ many2many: One2ManyComponent,
264
+ progressbar: ProgressBarComponent,
265
+ float_time: FloatTimeComponent,
266
+ image: ImageComponent,
267
+ integer: NumberComponent,
268
+ float: NumberComponent,
269
+ reference: ReferenceComponent,
270
+ tag: TagComponent,
271
+ selection: SelectionComponent,
272
+ date: DateComponent,
273
+ datetime: DateTimeComponent,
274
+ avatar: AvatarComponent,
275
+ tags: TagsComponent,
276
+ };
@@ -1 +0,0 @@
1
- {"version":3,"file":"Tree.d.ts","sourceRoot":"","sources":["../../../src/widgets/views/Tree.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkD,MAAM,OAAO,CAAC;AAUvE,OAAO,EAAE,QAAQ,EAAU,MAAM,SAAS,CAAC;AA2B3C,KAAK,KAAK,GAAG;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;IACnB,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,mBAAmB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAChE,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC;IACrC,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAC7C,gBAAgB,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAC7C,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IACtC,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,wBAAwB,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACzD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,kBAAkB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C,CAAC;AAsJF,iBAAS,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,YAAY,CA8O9C;AAED,eAAe,IAAI,CAAC"}