aio-table 9.0.0 → 9.1.0
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.
- package/index.css +226 -0
- package/index.d.ts +56 -97
- package/index.js +295 -369
- package/package.json +1 -1
package/index.css
CHANGED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--aio-table-color: #0069ff;
|
|
3
|
+
--aio-table-transition: 0.3s;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.aio-table-rtl {
|
|
7
|
+
direction: rtl;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.aio-table .aio-table-text {
|
|
11
|
+
height: 100%;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.aio-table-color input {
|
|
15
|
+
width: 100%;
|
|
16
|
+
padding: 0;
|
|
17
|
+
margin: 0;
|
|
18
|
+
border: none;
|
|
19
|
+
outline: none;
|
|
20
|
+
-webkit-appearance: none;
|
|
21
|
+
-moz-appearance: none;
|
|
22
|
+
appearance: none;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.aio-table-color img {
|
|
26
|
+
display: none;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.aio-table-color input::-webkit-color-swatch {
|
|
30
|
+
border: none;
|
|
31
|
+
padding: 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.aio-table {
|
|
35
|
+
box-sizing: border-box;
|
|
36
|
+
border-radius: 4px;
|
|
37
|
+
font-size: 12px;
|
|
38
|
+
height: fit-content;
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
width: 100%;
|
|
42
|
+
background: #eee;
|
|
43
|
+
color: #888;
|
|
44
|
+
padding: 0;
|
|
45
|
+
border: 1px solid #eee;
|
|
46
|
+
gap: 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.aio-table .aio-table {
|
|
50
|
+
color: inherit;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.aio-table-unit {
|
|
54
|
+
width: 100%;
|
|
55
|
+
flex: 1;
|
|
56
|
+
overflow: auto;
|
|
57
|
+
display:flex;
|
|
58
|
+
flex-direction: column;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.aio-table-header {
|
|
62
|
+
top: 0;
|
|
63
|
+
display: flex;
|
|
64
|
+
position: sticky;
|
|
65
|
+
position: -webkit-sticky;
|
|
66
|
+
z-index: 10;
|
|
67
|
+
background: #f8f8f8;
|
|
68
|
+
min-width: fit-content;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.aio-table-title {
|
|
72
|
+
padding: 0 12px;
|
|
73
|
+
font-weight: bold;
|
|
74
|
+
height: 36px;
|
|
75
|
+
display: flex;
|
|
76
|
+
align-items: center;
|
|
77
|
+
box-sizing: border-box;
|
|
78
|
+
flex-shrink: 0;
|
|
79
|
+
overflow: hidden;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.aio-table-title-justify {
|
|
83
|
+
padding: 0;
|
|
84
|
+
justify-content: center;
|
|
85
|
+
}
|
|
86
|
+
.aio-table-rows{
|
|
87
|
+
display:flex;
|
|
88
|
+
flex-direction: column;
|
|
89
|
+
gap:1px;
|
|
90
|
+
}
|
|
91
|
+
.aio-table-row {
|
|
92
|
+
display: flex;
|
|
93
|
+
min-width: fit-content;
|
|
94
|
+
}
|
|
95
|
+
.aio-table-cell {
|
|
96
|
+
padding: 6px 12px;
|
|
97
|
+
box-sizing: border-box;
|
|
98
|
+
display: flex;
|
|
99
|
+
align-items: center;
|
|
100
|
+
flex-shrink: 0;
|
|
101
|
+
min-height: 24px;
|
|
102
|
+
gap:6px;
|
|
103
|
+
background:#fff;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.aio-table-cell-before{
|
|
107
|
+
display:flex;
|
|
108
|
+
align-items: center;
|
|
109
|
+
justify-content: center;
|
|
110
|
+
}
|
|
111
|
+
.aio-table-cell-after{
|
|
112
|
+
display:flex;
|
|
113
|
+
align-items: center;
|
|
114
|
+
justify-content: center;
|
|
115
|
+
}
|
|
116
|
+
.aio-table-cell-value{
|
|
117
|
+
flex:1;
|
|
118
|
+
display:flex;
|
|
119
|
+
flex-direction: column;
|
|
120
|
+
}
|
|
121
|
+
.aio-table-cell-value.has-subtext:after {
|
|
122
|
+
content: attr(data-subtext);
|
|
123
|
+
font-size: 85%;
|
|
124
|
+
opacity: 0.7;
|
|
125
|
+
}
|
|
126
|
+
.aio-table-cell .aio-table-container {
|
|
127
|
+
width: 100%;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.aio-table-cell .aio-table {
|
|
131
|
+
width: 100%;
|
|
132
|
+
background: none;
|
|
133
|
+
border: none;
|
|
134
|
+
border-radius: 0;
|
|
135
|
+
background: none;
|
|
136
|
+
height: 100%;
|
|
137
|
+
min-height: 30px;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.aio-table-cell .aio-table input {
|
|
141
|
+
padding: 0;
|
|
142
|
+
background: none;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.aio-table-cell-justify.aio-table-cell{
|
|
146
|
+
padding: 0 !important;
|
|
147
|
+
}
|
|
148
|
+
.aio-table-cell-justify .aio-table-cell-value{
|
|
149
|
+
padding: 0;
|
|
150
|
+
align-items: center;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.aio-table-cell-justify > input {
|
|
154
|
+
text-align: center;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.aio-table-toolbar {
|
|
158
|
+
left: 0;
|
|
159
|
+
display: flex;
|
|
160
|
+
width: 100%;
|
|
161
|
+
align-items: center;
|
|
162
|
+
background: #f8f8f8;
|
|
163
|
+
z-index: 100;
|
|
164
|
+
box-sizing: border-box;
|
|
165
|
+
min-height: 36px;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
.aio-table-toolbar-content {
|
|
169
|
+
font-weight: bold;
|
|
170
|
+
padding: 0 12px;
|
|
171
|
+
height: 100%;
|
|
172
|
+
display: flex;
|
|
173
|
+
align-items: center;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.aio-table-toolbar-button {
|
|
177
|
+
height: 100%;
|
|
178
|
+
display: flex;
|
|
179
|
+
align-items: center;
|
|
180
|
+
justify-content: center;
|
|
181
|
+
min-width: 36px;
|
|
182
|
+
border: none;
|
|
183
|
+
background: none;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
.aio-table-remove {
|
|
189
|
+
border: none;
|
|
190
|
+
width: 36px;
|
|
191
|
+
background: none;
|
|
192
|
+
color: inherit;
|
|
193
|
+
align-items: center;
|
|
194
|
+
display: flex;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.aio-table-remove-title {
|
|
198
|
+
border: none;
|
|
199
|
+
width: 36px;
|
|
200
|
+
background: none;
|
|
201
|
+
color: inherit;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.aio-table-border-h {
|
|
205
|
+
height: 1px;
|
|
206
|
+
flex-shrink: 0;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.aio-table-border-v {
|
|
210
|
+
width: 1px;
|
|
211
|
+
flex-shrink: 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.aio-table-search {
|
|
215
|
+
flex: 1;
|
|
216
|
+
height: 100%;
|
|
217
|
+
padding: 6px;
|
|
218
|
+
box-sizing: border-box;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
.aio-table-search .aio-input {
|
|
222
|
+
background: #fff;
|
|
223
|
+
border: none;
|
|
224
|
+
min-height: 30px;
|
|
225
|
+
}
|
|
226
|
+
|
package/index.d.ts
CHANGED
|
@@ -1,124 +1,87 @@
|
|
|
1
1
|
import { ReactNode } from "react";
|
|
2
|
-
import * as UT from 'aio-utils';
|
|
3
2
|
import "./index.css";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
pagedRows: T[];
|
|
9
|
-
};
|
|
10
|
-
export type AI_table_param<T> = {
|
|
3
|
+
import { I_filter, I_filter_item, I_filter_saved_item, I_paging, I_sort } from "../../aio-component-utils";
|
|
4
|
+
type I_rowOption<T, R> = (p: I_rowDetail<T>) => R;
|
|
5
|
+
type I_cellOption<T, R> = ((p: I_cellDetail<T>) => R) | string;
|
|
6
|
+
type I_rowDetail<T> = {
|
|
11
7
|
row: T;
|
|
12
|
-
column: I_column<T>;
|
|
13
8
|
rowIndex: number;
|
|
9
|
+
isFirst: boolean;
|
|
10
|
+
isLast: boolean;
|
|
11
|
+
};
|
|
12
|
+
type I_cellDetail<T> = I_rowDetail<T> & {
|
|
13
|
+
column: I_table_column<T>;
|
|
14
|
+
change: (newRow: T) => void;
|
|
15
|
+
isDate: boolean;
|
|
14
16
|
};
|
|
15
|
-
export type
|
|
17
|
+
export type I_table_paging = I_paging;
|
|
18
|
+
export type I_table_sort<T> = I_sort<T>;
|
|
19
|
+
export type I_table_filter = I_filter;
|
|
20
|
+
export type I_table_filter_item = I_filter_item;
|
|
21
|
+
export type I_table_filter_saved_item = I_filter_saved_item;
|
|
22
|
+
export type I_table_column<T> = {
|
|
16
23
|
title?: any;
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
sort?: true | I_table_sort<T>;
|
|
25
|
+
filterId?: string;
|
|
19
26
|
search?: boolean;
|
|
20
27
|
id?: string;
|
|
21
28
|
_id?: string;
|
|
22
29
|
width?: any;
|
|
23
30
|
minWidth?: any;
|
|
24
|
-
titleAttrs?: {
|
|
25
|
-
[key: string]: any;
|
|
26
|
-
} | string;
|
|
27
|
-
template?: string | ((p: {
|
|
28
|
-
row: T;
|
|
29
|
-
column: I_column<T>;
|
|
30
|
-
rowIndex: number;
|
|
31
|
-
change: (newRow: T) => void;
|
|
32
|
-
}) => ReactNode);
|
|
33
31
|
excel?: string | boolean;
|
|
34
32
|
justify?: boolean;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
type?: 'string' | 'number';
|
|
48
|
-
sortId?: string;
|
|
49
|
-
getValue?: (row: T) => any;
|
|
50
|
-
};
|
|
51
|
-
export type I_context<T> = {
|
|
52
|
-
rootProps: I_AIOTable<T>;
|
|
53
|
-
columns: I_column<T>[];
|
|
54
|
-
ROWS: I_rows<T>;
|
|
55
|
-
add: () => void;
|
|
56
|
-
remove: (row: T, index: number) => void;
|
|
57
|
-
search: (searchValue: string) => void;
|
|
58
|
-
exportToExcel: () => void;
|
|
59
|
-
sorts: I_sort<T>[];
|
|
60
|
-
setSorts: (newSorts: I_sort<T>[]) => void;
|
|
61
|
-
sortRows: (rows: T[], sorts: I_sort<T>[]) => T[];
|
|
62
|
-
excelColumns: I_column<T>[];
|
|
63
|
-
getRowAttrs: (row: T, rowIndex: number) => any;
|
|
64
|
-
getCellAttrs: I_getCellAttrs<T>;
|
|
65
|
-
getDynamics: any;
|
|
66
|
-
DragColumns: UT.I_useDrag;
|
|
67
|
-
getIcon: UT.GetSvg["getIcon"];
|
|
68
|
-
};
|
|
69
|
-
export type I_getCellAttrs<T> = (p: {
|
|
70
|
-
row: T;
|
|
71
|
-
rowIndex: number;
|
|
72
|
-
column: I_column<T>;
|
|
73
|
-
type: 'title' | 'cell';
|
|
74
|
-
}) => any;
|
|
75
|
-
export type AI_table_paging = {
|
|
76
|
-
serverSide?: boolean;
|
|
77
|
-
number: number;
|
|
78
|
-
size: number;
|
|
79
|
-
length?: number;
|
|
80
|
-
sizes?: number[];
|
|
33
|
+
value?: string;
|
|
34
|
+
attrs?: I_cellOption<T, {
|
|
35
|
+
[attrs: string]: any;
|
|
36
|
+
}>;
|
|
37
|
+
before?: I_cellOption<T, ReactNode>;
|
|
38
|
+
after?: I_cellOption<T, ReactNode>;
|
|
39
|
+
subtext?: I_cellOption<T, ReactNode>;
|
|
40
|
+
template?: I_cellOption<T, ReactNode>;
|
|
41
|
+
titleAttrs?: {
|
|
42
|
+
[attrs: string]: any;
|
|
43
|
+
};
|
|
44
|
+
type?: 'text' | 'number' | 'month' | 'day' | 'hour' | 'minute';
|
|
81
45
|
};
|
|
82
|
-
export type
|
|
46
|
+
export type I_table<T> = {
|
|
47
|
+
fa?: boolean;
|
|
83
48
|
addText?: ReactNode | ((value: any) => ReactNode);
|
|
84
|
-
|
|
85
|
-
columns?: I_column<T>[];
|
|
49
|
+
columns?: I_table_column<T>[];
|
|
86
50
|
excel?: string | ((value: any[]) => any[]);
|
|
87
51
|
getValue?: {
|
|
88
52
|
[key: string]: (p: {
|
|
89
53
|
row: T;
|
|
90
|
-
column:
|
|
54
|
+
column: I_table_column<T>;
|
|
91
55
|
rowIndex: number;
|
|
92
56
|
change: (newRow: T) => void;
|
|
93
57
|
}) => any;
|
|
94
58
|
};
|
|
95
59
|
headerAttrs?: any;
|
|
96
60
|
onAdd?: () => Promise<T | undefined>;
|
|
97
|
-
|
|
98
|
-
|
|
61
|
+
onRemove?: (p: {
|
|
62
|
+
row: T;
|
|
63
|
+
rowIndex?: number;
|
|
64
|
+
}) => Promise<boolean>;
|
|
65
|
+
onChangePaging?: (newPaging: I_table_paging) => void;
|
|
66
|
+
onChangeSort?: (sorts: I_table_sort<T>[]) => Promise<boolean>;
|
|
99
67
|
onSwap?: true | ((newValue: T[], startRow: T, endRow: T) => void);
|
|
100
68
|
onSearch?: true | ((searchValue: string) => void);
|
|
101
|
-
paging?:
|
|
69
|
+
paging?: I_table_paging;
|
|
102
70
|
removeText?: string;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
71
|
+
rowOption?: {
|
|
72
|
+
before?: I_rowOption<T, ReactNode>;
|
|
73
|
+
after?: I_rowOption<T, ReactNode>;
|
|
74
|
+
attrs?: I_rowOption<T, {
|
|
75
|
+
[attrs: string]: any;
|
|
76
|
+
}>;
|
|
77
|
+
template?: I_rowOption<T, ReactNode>;
|
|
78
|
+
};
|
|
79
|
+
cellAttrs?: string | ((p: {
|
|
112
80
|
row: T;
|
|
81
|
+
column: I_table_column<T>;
|
|
113
82
|
rowIndex: number;
|
|
114
|
-
}) =>
|
|
115
|
-
rowGap?: number;
|
|
83
|
+
}) => any);
|
|
116
84
|
rowsTemplate?: (rows: T[]) => ReactNode;
|
|
117
|
-
rowTemplate?: (p: {
|
|
118
|
-
row: T;
|
|
119
|
-
rowIndex: number;
|
|
120
|
-
isLast: boolean;
|
|
121
|
-
}) => ReactNode;
|
|
122
85
|
toolbar?: ReactNode | (() => ReactNode);
|
|
123
86
|
toolbarAttrs?: any;
|
|
124
87
|
tabIndex?: number;
|
|
@@ -127,13 +90,9 @@ export type I_AIOTable<T> = {
|
|
|
127
90
|
className?: string;
|
|
128
91
|
style?: any;
|
|
129
92
|
attrs?: any;
|
|
130
|
-
onRemove?: true | ((p: {
|
|
131
|
-
row: T;
|
|
132
|
-
action?: Function;
|
|
133
|
-
rowIndex?: number;
|
|
134
|
-
parent?: T;
|
|
135
|
-
}) => Promise<boolean | void>);
|
|
136
93
|
placeholder?: ReactNode;
|
|
94
|
+
filter?: I_table_filter;
|
|
95
|
+
gap?: [number, number];
|
|
137
96
|
};
|
|
138
|
-
declare const AIOTable: <T>(
|
|
97
|
+
declare const AIOTable: <T>(props: I_table<T>) => JSX.Element;
|
|
139
98
|
export default AIOTable;
|
package/index.js
CHANGED
|
@@ -7,45 +7,83 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { createElement as _createElement } from "react";
|
|
11
10
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
12
11
|
import { createContext, createRef, Fragment, useContext, useEffect, useRef, useState } from "react";
|
|
13
12
|
import AIOInput from "aio-input";
|
|
14
13
|
import * as UT from 'aio-utils';
|
|
14
|
+
import usePopup from "aio-popup";
|
|
15
|
+
import AIODate from "aio-date";
|
|
16
|
+
import { Filterbar, usePaging, useSort } from "aio-component-utils";
|
|
15
17
|
import "./index.css";
|
|
16
18
|
const Context = createContext({});
|
|
17
19
|
const Provider = (p) => _jsx(Context.Provider, { value: p.value, children: p.children });
|
|
18
20
|
const useProvider = () => useContext(Context);
|
|
19
|
-
const AIOTable = (
|
|
20
|
-
|
|
21
|
+
const AIOTable = (props) => {
|
|
22
|
+
const popup = usePopup();
|
|
21
23
|
let [dom] = useState(createRef());
|
|
22
24
|
let [searchValue, setSearchValue] = useState('');
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
const [columns, setColumns] = useState([]);
|
|
26
|
+
const [searchColumns, setSearchColumns] = useState([]);
|
|
27
|
+
const [excelColumns, setExcelColumns] = useState([]);
|
|
28
|
+
const [filterColumns, setFilterColumns] = useState([]);
|
|
29
|
+
const filterColumnsRef = useRef(filterColumns);
|
|
30
|
+
filterColumnsRef.current = filterColumns;
|
|
31
|
+
const rowsIndexDicRef = useRef({});
|
|
32
|
+
const setRowsIndexDic = (rowsIndexDic) => rowsIndexDicRef.current = rowsIndexDic;
|
|
33
|
+
const getRowsIndexDic = () => rowsIndexDicRef.current;
|
|
34
|
+
const propsRef = useRef(props);
|
|
35
|
+
propsRef.current = props;
|
|
36
|
+
const pagingHook = usePaging({ rows: props.value, paging: props.paging, onChange: props.onChangePaging });
|
|
37
|
+
const tableHook = useTable(() => propsRef.current, () => pagingHook.paging);
|
|
26
38
|
const getIconRef = useRef(new UT.GetSvg());
|
|
27
39
|
const getIcon = getIconRef.current.getIcon;
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => setColumns(reOrder(columns, dragIndex, dropIndex)));
|
|
41
|
+
const getGetValue = (sort, column) => {
|
|
42
|
+
if (sort.getValue) {
|
|
43
|
+
return sort.getValue;
|
|
44
|
+
}
|
|
45
|
+
return (row) => {
|
|
46
|
+
const isDate = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
|
|
47
|
+
const cellValue = tableHook.getCellValue({ row, rowIndex: 0, isFirst: false, isLast: false, column, change: () => { }, isDate }, column.value);
|
|
48
|
+
if (isDate) {
|
|
49
|
+
const DATE = new AIODate();
|
|
50
|
+
try {
|
|
51
|
+
return DATE.getTime(cellValue);
|
|
52
|
+
}
|
|
53
|
+
catch (_a) {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return cellValue;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const getSorts = (columns) => {
|
|
61
|
+
let sorts = [];
|
|
62
|
+
for (let i = 0; i < columns.length; i++) {
|
|
63
|
+
const column = columns[i];
|
|
64
|
+
const { _id } = column;
|
|
65
|
+
const sort = column.sort === true ? { sortId: _id } : column.sort;
|
|
66
|
+
if (!sort) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
let { active = false, dir = 'dec', sortId } = sort;
|
|
70
|
+
let sortItem = { dir, title: sort.title || column.title, sortId, active, getValue: getGetValue(sort, column) };
|
|
71
|
+
sorts.push(sortItem);
|
|
40
72
|
}
|
|
73
|
+
return sorts;
|
|
74
|
+
};
|
|
75
|
+
const sortHook = useSort({
|
|
76
|
+
sorts: [],
|
|
77
|
+
rows: propsRef.current.value,
|
|
78
|
+
onChangeRows: props.onChange,
|
|
79
|
+
onChangeSort: props.onChangeSort,
|
|
41
80
|
});
|
|
42
|
-
const
|
|
43
|
-
let [sorts, setSorts] = useState([]);
|
|
81
|
+
const isDate = (column) => ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
|
|
44
82
|
function getColumns() {
|
|
45
|
-
let { columns = [] } =
|
|
46
|
-
let searchColumns = [], excelColumns = [];
|
|
83
|
+
let { columns = [] } = props;
|
|
84
|
+
let searchColumns = [], excelColumns = [], filterColumns = [];
|
|
47
85
|
let updatedColumns = columns.map((o) => {
|
|
48
|
-
let { id = 'aitc' + Math.round(Math.random() * 1000000),
|
|
86
|
+
let { id = 'aitc' + Math.round(Math.random() * 1000000), filterId, search, excel } = o;
|
|
49
87
|
let column = Object.assign(Object.assign({}, o), { _id: id });
|
|
50
88
|
if (search) {
|
|
51
89
|
searchColumns.push(column);
|
|
@@ -53,95 +91,76 @@ const AIOTable = (rootProps) => {
|
|
|
53
91
|
if (excel) {
|
|
54
92
|
excelColumns.push(column);
|
|
55
93
|
}
|
|
94
|
+
if (filterId) {
|
|
95
|
+
filterColumns.push(column);
|
|
96
|
+
}
|
|
56
97
|
return column;
|
|
57
98
|
});
|
|
58
99
|
setColumns(updatedColumns);
|
|
59
100
|
setSearchColumns(searchColumns);
|
|
60
101
|
setExcelColumns(excelColumns);
|
|
102
|
+
setFilterColumns(filterColumns);
|
|
61
103
|
return updatedColumns;
|
|
62
104
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
let type = sort.type || 'string';
|
|
81
|
-
let sortItem = { dir, title: sort.title || column.title, sortId: _id, active, type, getValue };
|
|
82
|
-
sorts.push(sortItem);
|
|
83
|
-
}
|
|
84
|
-
setSorts(sorts);
|
|
85
|
-
}
|
|
86
|
-
function getDynamics(p) {
|
|
87
|
-
let { value, row, column, def, rowIndex } = p;
|
|
88
|
-
if (paging) {
|
|
89
|
-
let { number, size } = paging;
|
|
90
|
-
if (rowIndex)
|
|
91
|
-
rowIndex += ((number - 1) * size);
|
|
92
|
-
}
|
|
93
|
-
let type = typeof value;
|
|
94
|
-
if (type === 'string') {
|
|
95
|
-
let result = value;
|
|
96
|
-
let param = { row: row, column: column, rowIndex: rowIndex };
|
|
97
|
-
if (getValue[value]) {
|
|
98
|
-
result = getValue[value](Object.assign(Object.assign({}, param), { change: () => { } }));
|
|
99
|
-
}
|
|
100
|
-
else if (value.indexOf('row.') !== -1) {
|
|
101
|
-
try {
|
|
102
|
-
eval(`result = ${value}`);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
const columns = getColumns();
|
|
107
|
+
sortHook.setSorts(getSorts(columns));
|
|
108
|
+
}, []);
|
|
109
|
+
function getTimeText(column, value) {
|
|
110
|
+
if (!value || value === null) {
|
|
111
|
+
return '';
|
|
112
|
+
}
|
|
113
|
+
const t = column.type;
|
|
114
|
+
const DATE = new AIODate();
|
|
115
|
+
let pattern = '{year}/{month}';
|
|
116
|
+
if (t !== 'month') {
|
|
117
|
+
pattern += '/{day}';
|
|
118
|
+
if (t !== 'day') {
|
|
119
|
+
pattern += ' {hour}';
|
|
120
|
+
if (t === 'minute') {
|
|
121
|
+
pattern += ' : {minute}';
|
|
103
122
|
}
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
else {
|
|
124
|
+
pattern += ' : 00';
|
|
106
125
|
}
|
|
107
126
|
}
|
|
108
|
-
return result === undefined ? def : result;
|
|
109
127
|
}
|
|
110
|
-
|
|
111
|
-
return def;
|
|
112
|
-
}
|
|
113
|
-
if (type === 'function') {
|
|
114
|
-
return value({ row, column, rowIndex });
|
|
115
|
-
}
|
|
116
|
-
return value === undefined ? def : value;
|
|
128
|
+
return DATE.getDateByPattern(value, pattern);
|
|
117
129
|
}
|
|
118
|
-
useEffect(() => { getSorts(getColumns()); }, []);
|
|
119
130
|
function add() {
|
|
120
131
|
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
if (!onAdd) {
|
|
132
|
+
if (!props.onAdd) {
|
|
122
133
|
return;
|
|
123
134
|
}
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
|
|
135
|
+
const res = yield props.onAdd();
|
|
136
|
+
if (res && props.onChange) {
|
|
137
|
+
props.onChange([res, ...props.value]);
|
|
127
138
|
}
|
|
128
|
-
onChange([Object.assign({}, row), ...value]);
|
|
129
139
|
});
|
|
130
140
|
}
|
|
131
141
|
function remove(row, index) {
|
|
132
|
-
|
|
133
|
-
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
if (!props.onRemove) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const res = yield props.onRemove({ row, rowIndex: index });
|
|
147
|
+
if (res === true && props.onChange) {
|
|
148
|
+
props.onChange(props.value.filter((o) => o._id !== row._id));
|
|
149
|
+
}
|
|
150
|
+
});
|
|
134
151
|
}
|
|
135
152
|
function exportToExcel() {
|
|
136
153
|
let list = [];
|
|
137
|
-
if (typeof
|
|
138
|
-
list =
|
|
154
|
+
if (typeof props.excel === 'function') {
|
|
155
|
+
list = props.excel(props.value);
|
|
139
156
|
}
|
|
140
157
|
else {
|
|
141
|
-
for (let
|
|
142
|
-
|
|
158
|
+
for (let rowIndex = 0; rowIndex < props.value.length; rowIndex++) {
|
|
159
|
+
const isFirst = rowIndex === 0;
|
|
160
|
+
const isLast = rowIndex === props.value.length - 1;
|
|
161
|
+
let row = props.value[rowIndex], json = {};
|
|
143
162
|
for (let j = 0; j < excelColumns.length; j++) {
|
|
144
|
-
|
|
163
|
+
const column = excelColumns[j], { excel } = column;
|
|
145
164
|
let key = '';
|
|
146
165
|
if (excel === true) {
|
|
147
166
|
if (typeof column.title === 'string') {
|
|
@@ -157,213 +176,84 @@ const AIOTable = (rootProps) => {
|
|
|
157
176
|
else {
|
|
158
177
|
continue;
|
|
159
178
|
}
|
|
160
|
-
|
|
179
|
+
const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
|
|
180
|
+
json[key] = tableHook.getCellValue(cellDetail, column.value, '');
|
|
161
181
|
}
|
|
162
182
|
list.push(json);
|
|
163
183
|
}
|
|
164
184
|
}
|
|
165
|
-
UT.ExportToExcel(list, { promptText: typeof excel === 'string' ? excel : 'Inter Excel File Name' });
|
|
185
|
+
UT.ExportToExcel(list, { promptText: typeof props.excel === 'string' ? props.excel : 'Inter Excel File Name' });
|
|
166
186
|
}
|
|
167
187
|
function getSearchedRows(rows) {
|
|
168
|
-
if (onSearch !== true) {
|
|
188
|
+
if (props.onSearch !== true) {
|
|
169
189
|
return rows;
|
|
170
190
|
}
|
|
171
191
|
if (!searchColumns.length || !searchValue) {
|
|
172
192
|
return rows;
|
|
173
193
|
}
|
|
174
|
-
|
|
194
|
+
const rowsIndexDic = getRowsIndexDic();
|
|
195
|
+
return UT.Search(rows, searchValue, (row) => {
|
|
196
|
+
const { isFirst, isLast, rowIndex } = rowsIndexDic[(row)._id];
|
|
175
197
|
let str = '';
|
|
176
198
|
for (let i = 0; i < searchColumns.length; i++) {
|
|
177
199
|
let column = searchColumns[i];
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
200
|
+
const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
|
|
201
|
+
let cellValue = tableHook.getCellValue(cellDetail, column.value, '');
|
|
202
|
+
if (cellValue) {
|
|
203
|
+
str += cellValue + ' ';
|
|
181
204
|
}
|
|
182
205
|
}
|
|
183
206
|
return str;
|
|
184
207
|
});
|
|
185
208
|
}
|
|
186
|
-
function sortRows(rows, sorts) {
|
|
187
|
-
if (!rows) {
|
|
188
|
-
return [];
|
|
189
|
-
}
|
|
190
|
-
if (!sorts || !sorts.length) {
|
|
191
|
-
return rows;
|
|
192
|
-
}
|
|
193
|
-
return rows.sort((a, b) => {
|
|
194
|
-
for (let i = 0; i < sorts.length; i++) {
|
|
195
|
-
let { dir, getValue } = sorts[i];
|
|
196
|
-
if (!getValue) {
|
|
197
|
-
return 0;
|
|
198
|
-
}
|
|
199
|
-
let aValue = getValue(a), bValue = getValue(b);
|
|
200
|
-
if (aValue < bValue) {
|
|
201
|
-
return -1 * (dir === 'dec' ? -1 : 1);
|
|
202
|
-
}
|
|
203
|
-
if (aValue > bValue) {
|
|
204
|
-
return 1 * (dir === 'dec' ? -1 : 1);
|
|
205
|
-
}
|
|
206
|
-
if (i === sorts.length - 1) {
|
|
207
|
-
return 0;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return 0;
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
function getSortedRows(rows) {
|
|
214
|
-
if (temp.isInitSortExecuted) {
|
|
215
|
-
return rows;
|
|
216
|
-
}
|
|
217
|
-
if (onChangeSort) {
|
|
218
|
-
return rows;
|
|
219
|
-
}
|
|
220
|
-
let activeSorts = sorts.filter((sort) => sort.active !== false);
|
|
221
|
-
if (!activeSorts.length || !rows.length) {
|
|
222
|
-
return rows;
|
|
223
|
-
}
|
|
224
|
-
temp.isInitSortExecuted = true;
|
|
225
|
-
let sortedRows = sortRows(rows, activeSorts);
|
|
226
|
-
onChange(sortedRows);
|
|
227
|
-
return sortedRows;
|
|
228
|
-
}
|
|
229
209
|
function getRows() {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
let
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
let
|
|
238
|
-
let
|
|
239
|
-
return {
|
|
240
|
-
}
|
|
241
|
-
const getCellAttrs = ({ row, rowIndex, column, type }) => {
|
|
242
|
-
let { cellAttrs, titleAttrs } = column;
|
|
243
|
-
let attrs = getDynamics({ value: type === 'title' ? titleAttrs : cellAttrs, column, def: {}, row, rowIndex });
|
|
244
|
-
let justify = getDynamics({ value: column.justify, def: false });
|
|
245
|
-
let cls = `aio-input-table-${type}` + (justify ? ` aio-input-table-${type}-justify` : '');
|
|
246
|
-
attrs = UT.AddToAttrs(attrs, { className: cls, style: getCellStyle(column) });
|
|
247
|
-
if (type === 'title') {
|
|
248
|
-
attrs.title = getDynamics({ value: column.title, def: '' });
|
|
249
|
-
}
|
|
250
|
-
return Object.assign({}, attrs);
|
|
251
|
-
};
|
|
252
|
-
function getRowAttrs(row, rowIndex) {
|
|
253
|
-
let attrs = rowAttrs ? rowAttrs({ row, rowIndex }) : {};
|
|
254
|
-
let obj = UT.AddToAttrs(attrs, { className: 'aio-input-table-row' });
|
|
255
|
-
if (onSwap) {
|
|
256
|
-
obj = Object.assign(Object.assign(Object.assign({}, obj), DragRows.getDragAttrs({ dragIndex: rowIndex })), DragRows.getDropAttrs({ dropIndex: rowIndex, rows: value }));
|
|
257
|
-
}
|
|
258
|
-
return obj;
|
|
210
|
+
const rows = props.value;
|
|
211
|
+
const rowsIndexDic = {};
|
|
212
|
+
for (let i = 0; i < props.value.length; i++) {
|
|
213
|
+
rowsIndexDic[props.value[i]._id] = { rowIndex: i, isFirst: i === 0, isLast: i === rows.length - 1 };
|
|
214
|
+
}
|
|
215
|
+
setRowsIndexDic(rowsIndexDic);
|
|
216
|
+
let searchedRows = getSearchedRows(rows);
|
|
217
|
+
let sortedRows = sortHook.getSortedRows(searchedRows);
|
|
218
|
+
let pagedRows = pagingHook.getPagedRows(sortedRows);
|
|
219
|
+
return { rows: props.value, searchedRows, sortedRows, pagedRows };
|
|
259
220
|
}
|
|
260
221
|
function search(searchValue) {
|
|
261
|
-
if (onSearch === true) {
|
|
222
|
+
if (props.onSearch === true) {
|
|
262
223
|
setSearchValue(searchValue);
|
|
263
224
|
}
|
|
264
|
-
else if (typeof onSearch === 'function') {
|
|
265
|
-
onSearch(searchValue);
|
|
225
|
+
else if (typeof props.onSearch === 'function') {
|
|
226
|
+
props.onSearch(searchValue);
|
|
266
227
|
}
|
|
267
228
|
}
|
|
229
|
+
const changeCell = (cellDetail, cellNewValue) => {
|
|
230
|
+
if (!props.onChange) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const { column } = cellDetail;
|
|
234
|
+
if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
let row = JSON.parse(JSON.stringify(cellDetail.row));
|
|
238
|
+
const rowId = row._id;
|
|
239
|
+
eval(`${column.value} = cellNewValue`);
|
|
240
|
+
props.onChange(props.value.map((o) => o._id !== rowId ? o : row));
|
|
241
|
+
};
|
|
268
242
|
let ROWS = getRows();
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
243
|
+
const { gap = [0, 1] } = props;
|
|
244
|
+
let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
|
|
245
|
+
const context = {
|
|
246
|
+
rootProps: props, getTimeText, isDate, columns, excelColumns, filterColumns, changeCell, tableHook, sortHook, ROWS,
|
|
247
|
+
getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup,
|
|
248
|
+
};
|
|
249
|
+
return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter &&
|
|
250
|
+
_jsx(Filterbar, { columns: filterColumns, filter: props.filter, columnOption: { text: (column) => column.title, id: (column) => column.filterId, type: (column) => column.type || 'text' } }), _jsxs("div", { className: 'aio-table-unit aio-table-scroll', style: { gap: gap[1] }, children: [_jsx(TableHeader, {}), _jsx(TableRows, {})] }), pagingHook.render()] })), popup.render()] }));
|
|
274
251
|
};
|
|
275
252
|
export default AIOTable;
|
|
276
|
-
const TableGap = ({ dir }) => {
|
|
277
|
-
const { rootProps } = useProvider(), { rowGap, columnGap } = rootProps;
|
|
278
|
-
return _jsx("div", { className: `aio-input-table-border-${dir}`, style: dir === 'h' ? { height: rowGap } : { width: columnGap } });
|
|
279
|
-
};
|
|
280
|
-
function TablePaging() {
|
|
281
|
-
let { ROWS, rootProps } = useProvider();
|
|
282
|
-
let [temp] = useState({ timeout: undefined, start: undefined, end: undefined, pages: 0 });
|
|
283
|
-
function fix(paging) {
|
|
284
|
-
if (typeof rootProps.onChangePaging !== 'function') {
|
|
285
|
-
alert('aio-input error => in type table you set paging but forget to set onChangePaging function prop to aio input');
|
|
286
|
-
return { number: 0, size: 0 };
|
|
287
|
-
}
|
|
288
|
-
let { number, size = 20, length = 0, sizes = [1, 5, 10, 15, 20, 30, 50, 70, 100], serverSide } = paging;
|
|
289
|
-
if (!serverSide) {
|
|
290
|
-
length = ROWS.sortedRows.length;
|
|
291
|
-
}
|
|
292
|
-
if (sizes.indexOf(size) === -1) {
|
|
293
|
-
size = sizes[0];
|
|
294
|
-
}
|
|
295
|
-
let pages = Math.ceil(length / size);
|
|
296
|
-
number = number > pages ? pages : number;
|
|
297
|
-
number = number < 1 ? 1 : number;
|
|
298
|
-
let start = number - 3, end = number + 3;
|
|
299
|
-
temp.start = start;
|
|
300
|
-
temp.end = end;
|
|
301
|
-
temp.pages = pages;
|
|
302
|
-
return Object.assign(Object.assign({}, paging), { length, number, size, sizes });
|
|
303
|
-
}
|
|
304
|
-
let [paging, setPaging] = useState(fix(rootProps.paging || { size: 0, number: 0 }));
|
|
305
|
-
useEffect(() => {
|
|
306
|
-
if (rootProps.paging) {
|
|
307
|
-
setPaging(fix(rootProps.paging));
|
|
308
|
-
}
|
|
309
|
-
}, [(rootProps.paging || { size: 0, number: 0, length: 0 }).size, (rootProps.paging || { size: 0, number: 0, length: 0 }).number, (rootProps.paging || { size: 0, number: 0, length: 0 }).length]);
|
|
310
|
-
function changePaging(obj) {
|
|
311
|
-
let newPaging = fix(Object.assign(Object.assign({}, paging), obj));
|
|
312
|
-
setPaging(newPaging);
|
|
313
|
-
if (rootProps.onChangePaging) {
|
|
314
|
-
if (newPaging.serverSide) {
|
|
315
|
-
clearTimeout(temp.timeout);
|
|
316
|
-
temp.timeout = setTimeout(() => {
|
|
317
|
-
//be khatere fahme payine typescript majbooram dobare in shart ro bezanam
|
|
318
|
-
if (rootProps.onChangePaging) {
|
|
319
|
-
rootProps.onChangePaging(newPaging);
|
|
320
|
-
}
|
|
321
|
-
}, 800);
|
|
322
|
-
}
|
|
323
|
-
else {
|
|
324
|
-
rootProps.onChangePaging(newPaging);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
let { number, size, sizes } = paging;
|
|
329
|
-
let buttons = [];
|
|
330
|
-
let isFirst = true;
|
|
331
|
-
for (let i = temp.start; i <= temp.end; i++) {
|
|
332
|
-
if (i < 1 || i > temp.pages) {
|
|
333
|
-
buttons.push(_jsx("button", { className: 'aio-input-table-paging-button aio-input-table-paging-button-hidden', children: i }, i));
|
|
334
|
-
}
|
|
335
|
-
else {
|
|
336
|
-
let index;
|
|
337
|
-
if (isFirst) {
|
|
338
|
-
index = 1;
|
|
339
|
-
isFirst = false;
|
|
340
|
-
}
|
|
341
|
-
else if (i === Math.min(temp.end, temp.pages)) {
|
|
342
|
-
index = temp.pages;
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
index = i;
|
|
346
|
-
}
|
|
347
|
-
buttons.push(_jsx("button", { className: 'aio-input-table-paging-button' + (index === number ? ' active' : ''), onClick: () => changePaging({ number: index }), children: index }, index));
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
function changeSizeButton() {
|
|
351
|
-
if (!sizes || !sizes.length) {
|
|
352
|
-
return null;
|
|
353
|
-
}
|
|
354
|
-
let p = {
|
|
355
|
-
attrs: { className: 'aio-input-table-paging-button aio-input-table-paging-size' },
|
|
356
|
-
type: 'select', value: size, options: sizes, option: { text: 'option', value: 'option' },
|
|
357
|
-
onChange: (value) => changePaging({ size: value }),
|
|
358
|
-
popover: { fitHorizontal: true },
|
|
359
|
-
};
|
|
360
|
-
return (_jsx(AIOInput, Object.assign({}, p)));
|
|
361
|
-
}
|
|
362
|
-
return (_jsxs("div", { className: 'aio-input-table-paging', children: [buttons, changeSizeButton()] }));
|
|
363
|
-
}
|
|
364
253
|
const TableRows = () => {
|
|
365
254
|
let { ROWS, rootProps } = useProvider();
|
|
366
|
-
let {
|
|
255
|
+
let { rowOption = {}, rowsTemplate, placeholder = 'there is not any items' } = rootProps;
|
|
256
|
+
const { before: rowBefore = () => null, after: rowAfter = () => null, template: rowTemplate, } = rowOption;
|
|
367
257
|
let rows = ROWS.pagedRows || [];
|
|
368
258
|
let content;
|
|
369
259
|
if (rowsTemplate) {
|
|
@@ -373,14 +263,19 @@ const TableRows = () => {
|
|
|
373
263
|
content = rows.map((o, i) => {
|
|
374
264
|
let { id = 'ailr' + Math.round(Math.random() * 10000000) } = o;
|
|
375
265
|
o._id = o._id === undefined ? id : o._id;
|
|
376
|
-
|
|
266
|
+
const rowDetail = {
|
|
267
|
+
row: o, rowIndex: i,
|
|
268
|
+
isFirst: i === 0,
|
|
269
|
+
isLast: i === rows.length - 1
|
|
270
|
+
};
|
|
271
|
+
let Row;
|
|
377
272
|
if (rowTemplate) {
|
|
378
|
-
Row = rowTemplate(
|
|
273
|
+
Row = rowTemplate(rowDetail);
|
|
379
274
|
}
|
|
380
275
|
else {
|
|
381
|
-
Row = _jsx(TableRow, {
|
|
276
|
+
Row = _jsx(TableRow, { rowDetail: rowDetail }, o._id);
|
|
382
277
|
}
|
|
383
|
-
return (_jsxs(Fragment, { children: [rowBefore(
|
|
278
|
+
return (_jsxs(Fragment, { children: [rowBefore(rowDetail), Row, rowAfter(rowDetail)] }, o._id));
|
|
384
279
|
});
|
|
385
280
|
}
|
|
386
281
|
else if (placeholder) {
|
|
@@ -389,75 +284,16 @@ const TableRows = () => {
|
|
|
389
284
|
else {
|
|
390
285
|
return null;
|
|
391
286
|
}
|
|
392
|
-
|
|
287
|
+
const { gap = [0, 1] } = rootProps;
|
|
288
|
+
return _jsx("div", { className: 'aio-table-rows', style: { gap: gap[1] }, children: content });
|
|
393
289
|
};
|
|
394
290
|
const TableToolbar = () => {
|
|
395
|
-
let { add, exportToExcel,
|
|
396
|
-
let { toolbarAttrs, toolbar, onAdd, onSearch,
|
|
397
|
-
toolbarAttrs = UT.AddToAttrs(toolbarAttrs, { className: 'aio-
|
|
398
|
-
if (!onAdd && !toolbar && !onSearch && !sorts.length && !excelColumns.length) {
|
|
291
|
+
let { add, exportToExcel, search, rootProps, excelColumns, getIcon, sortHook } = useProvider();
|
|
292
|
+
let { toolbarAttrs, toolbar, onAdd, onSearch, value } = rootProps;
|
|
293
|
+
toolbarAttrs = UT.AddToAttrs(toolbarAttrs, { className: 'aio-table-toolbar' });
|
|
294
|
+
if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length) {
|
|
399
295
|
return null;
|
|
400
296
|
}
|
|
401
|
-
function changeSort(sortId, changeObject) {
|
|
402
|
-
let newSorts = sorts.map((sort) => {
|
|
403
|
-
if (sort.sortId === sortId) {
|
|
404
|
-
let newSort = Object.assign(Object.assign({}, sort), changeObject);
|
|
405
|
-
return newSort;
|
|
406
|
-
}
|
|
407
|
-
return sort;
|
|
408
|
-
});
|
|
409
|
-
changeSorts(newSorts);
|
|
410
|
-
}
|
|
411
|
-
function changeSorts(sorts) {
|
|
412
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
413
|
-
if (onChangeSort) {
|
|
414
|
-
let res = yield onChangeSort(sorts);
|
|
415
|
-
if (res !== false) {
|
|
416
|
-
setSorts(sorts);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
setSorts(sorts);
|
|
421
|
-
let activeSorts = sorts.filter((sort) => sort.active !== false);
|
|
422
|
-
if (activeSorts.length) {
|
|
423
|
-
onChange(sortRows(value, activeSorts));
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
function button() {
|
|
429
|
-
if (!sorts.length) {
|
|
430
|
-
return null;
|
|
431
|
-
}
|
|
432
|
-
let p = {
|
|
433
|
-
popover: {
|
|
434
|
-
header: { title: 'Sort', onClose: false },
|
|
435
|
-
setAttrs: (key) => { if (key === 'header') {
|
|
436
|
-
return { className: 'aio-input-table-toolbar-popover-header' };
|
|
437
|
-
} },
|
|
438
|
-
limitTo: '.aio-input-table'
|
|
439
|
-
},
|
|
440
|
-
caret: false, type: 'select', options: sorts,
|
|
441
|
-
option: {
|
|
442
|
-
text: 'option.title',
|
|
443
|
-
checked: '!!option.active',
|
|
444
|
-
close: () => false,
|
|
445
|
-
value: 'option.sortId',
|
|
446
|
-
after: (option) => {
|
|
447
|
-
let { dir = 'dec', sortId } = option;
|
|
448
|
-
return (_jsx("div", { onClick: (e) => {
|
|
449
|
-
e.stopPropagation();
|
|
450
|
-
changeSort(sortId, { dir: dir === 'dec' ? 'inc' : 'dec' });
|
|
451
|
-
}, children: getIcon(dir === 'dec' ? 'mdiArrowDown' : 'mdiArrowUp', 0.8) }));
|
|
452
|
-
}
|
|
453
|
-
},
|
|
454
|
-
attrs: { className: 'aio-input-table-toolbar-button' },
|
|
455
|
-
text: getIcon('mdiSort', 0.7),
|
|
456
|
-
onSwap: (newSorts, from, to) => changeSorts(newSorts),
|
|
457
|
-
onChange: (value, option) => changeSort(value, { active: !option.checked })
|
|
458
|
-
};
|
|
459
|
-
return (_createElement(AIOInput, Object.assign({}, p, { key: 'sortbutton' })));
|
|
460
|
-
}
|
|
461
297
|
function getAddText() {
|
|
462
298
|
let { addText } = rootProps;
|
|
463
299
|
if (!rootProps.addText) {
|
|
@@ -465,59 +301,149 @@ const TableToolbar = () => {
|
|
|
465
301
|
}
|
|
466
302
|
return typeof addText === 'function' ? addText(value) : addText;
|
|
467
303
|
}
|
|
468
|
-
return (_jsxs(
|
|
304
|
+
return (_jsxs("div", Object.assign({}, toolbarAttrs, { children: [toolbar && _jsx("div", { className: 'aio-table-toolbar-content', children: typeof toolbar === 'function' ? toolbar() : toolbar }), _jsx("div", { className: 'aio-table-search', children: !!onSearch && _jsx(AIOInput, { type: 'text', onChange: (value) => search(value), after: getIcon('mdiMagnify', 0.7) }) }), sortHook.renderSortButton(), !!excelColumns.length && _jsx("div", { className: 'aio-table-toolbar-button', onClick: () => exportToExcel(), children: getIcon('mdiFileExcel', 0.8) }), !!onAdd && _jsx("div", { className: 'aio-table-toolbar-button', onClick: () => add(), children: getAddText() })] })));
|
|
469
305
|
};
|
|
470
306
|
const TableHeader = () => {
|
|
471
307
|
let { rootProps, columns } = useProvider();
|
|
472
|
-
let { headerAttrs, onRemove } = rootProps;
|
|
473
|
-
headerAttrs = UT.AddToAttrs(headerAttrs, { className: 'aio-
|
|
308
|
+
let { headerAttrs, onRemove, gap = [0, 1] } = rootProps;
|
|
309
|
+
headerAttrs = UT.AddToAttrs(headerAttrs, { className: 'aio-table-header', style: { gap: gap[0] } });
|
|
474
310
|
let Titles = columns.map((o, i) => _jsx(TableTitle, { column: o, isLast: i === columns.length - 1, colIndex: i }, o._id));
|
|
475
|
-
let RemoveTitle = !onRemove ? null :
|
|
476
|
-
return _jsxs("div", Object.assign({}, headerAttrs, { children: [Titles, RemoveTitle
|
|
311
|
+
let RemoveTitle = !onRemove ? null : _jsx("div", { className: 'aio-table-remove-title' });
|
|
312
|
+
return _jsxs("div", Object.assign({}, headerAttrs, { children: [Titles, RemoveTitle] }));
|
|
477
313
|
};
|
|
478
314
|
const TableTitle = (p) => {
|
|
479
315
|
const { column, isLast, colIndex } = p;
|
|
480
|
-
let {
|
|
481
|
-
const attrs = Object.assign(Object.assign(Object.assign({},
|
|
482
|
-
return
|
|
316
|
+
let { tableHook, DragColumns } = useProvider();
|
|
317
|
+
const attrs = Object.assign(Object.assign(Object.assign({}, tableHook.getTitleAttrs(column)), DragColumns.getDragAttrs(colIndex)), DragColumns.getDropAttrs(colIndex));
|
|
318
|
+
return _jsx("div", Object.assign({}, attrs, { children: attrs.title }));
|
|
483
319
|
};
|
|
484
|
-
const TableRow = (
|
|
485
|
-
const {
|
|
486
|
-
|
|
320
|
+
const TableRow = (props) => {
|
|
321
|
+
const { rowDetail } = props;
|
|
322
|
+
const { row, rowIndex } = rowDetail;
|
|
323
|
+
const rowId = row._id;
|
|
324
|
+
let { remove, rootProps, columns, tableHook, getIcon, isDate } = useProvider();
|
|
487
325
|
function getCells() {
|
|
488
326
|
return columns.map((column, i) => {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
327
|
+
const key = rowId + ' ' + column._id;
|
|
328
|
+
const cellDetail = Object.assign(Object.assign({}, rowDetail), { column, change: (cellNewValue) => {
|
|
329
|
+
if (!rootProps.onChange) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
let row = JSON.parse(JSON.stringify(cellDetail.row));
|
|
336
|
+
eval(`${column.value} = cellNewValue`);
|
|
337
|
+
rootProps.onChange(rootProps.value.map((o) => o._id !== rowId ? o : row));
|
|
338
|
+
}, isDate: isDate(column) });
|
|
339
|
+
const cellValue = tableHook.getCellValue(cellDetail, column.value);
|
|
340
|
+
return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue }, key));
|
|
492
341
|
});
|
|
493
342
|
}
|
|
494
343
|
let { onRemove } = rootProps;
|
|
495
|
-
return (
|
|
344
|
+
return (_jsx(_Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getRowAttrs(props.rowDetail), { children: [getCells(), onRemove ? _jsx("button", { className: 'aio-table-remove', onClick: () => remove(row, rowIndex), children: getIcon('mdiClose', 0.8) }) : null] }), rowId) }));
|
|
496
345
|
};
|
|
497
|
-
const TableCell = (
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
346
|
+
const TableCell = (props) => {
|
|
347
|
+
const { cellDetail, cellValue } = props;
|
|
348
|
+
const { row, column, isLast } = cellDetail;
|
|
349
|
+
const { tableHook, getTimeText } = useProvider();
|
|
350
|
+
const { template, before, after, subtext } = column;
|
|
351
|
+
const rowId = row._id;
|
|
352
|
+
const colId = column._id;
|
|
353
|
+
const isTime = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
|
|
354
|
+
const templateValue = isTime ? getTimeText(cellValue) : tableHook.getCellValue(cellDetail, template);
|
|
355
|
+
const beforeValue = tableHook.getCellValue(cellDetail, before, undefined);
|
|
356
|
+
const afterValue = tableHook.getCellValue(cellDetail, after, undefined);
|
|
357
|
+
const subtextValue = tableHook.getCellValue(cellDetail, subtext, undefined);
|
|
358
|
+
return (_jsx(Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getCellAttrs(props.cellDetail, props.cellValue), { children: [beforeValue !== undefined && _jsx("div", { className: "aio-table-cell-before", children: beforeValue }), _jsxs("div", { className: `aio-table-cell-value${subtext !== undefined ? ' has-subtext' : ''}`, "data-subtext": subtextValue, children: [templateValue !== undefined && templateValue, templateValue === undefined && cellValue] }), afterValue !== undefined && _jsx("div", { className: "aio-table-cell-after", children: afterValue })] })) }, rowId + ' ' + colId));
|
|
508
359
|
};
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
const
|
|
515
|
-
|
|
516
|
-
|
|
360
|
+
const useTable = (getProps, getPaging) => {
|
|
361
|
+
const DragRows = UT.useDrag((dragData, dropData, reOrder) => {
|
|
362
|
+
const { onSwap, onChange } = getProps();
|
|
363
|
+
const { dragIndex } = dragData;
|
|
364
|
+
const { dropIndex, rows } = dropData;
|
|
365
|
+
const newRows = reOrder(rows, dragIndex, dropIndex);
|
|
366
|
+
const from = rows[dragIndex];
|
|
367
|
+
const to = rows[dropIndex];
|
|
368
|
+
if (typeof onSwap === 'function') {
|
|
369
|
+
onSwap(newRows, from, to);
|
|
517
370
|
}
|
|
518
|
-
if (
|
|
519
|
-
|
|
371
|
+
else if (onChange) {
|
|
372
|
+
onChange(newRows);
|
|
520
373
|
}
|
|
521
|
-
}
|
|
522
|
-
|
|
374
|
+
});
|
|
375
|
+
const getCellValue = (cellDetail, cellValue, def) => {
|
|
376
|
+
const { getValue = {} } = getProps();
|
|
377
|
+
const paging = getPaging();
|
|
378
|
+
if (paging) {
|
|
379
|
+
let { number, size } = paging;
|
|
380
|
+
cellDetail = Object.assign(Object.assign({}, cellDetail), { rowIndex: cellDetail.rowIndex + ((number - 1) * size) });
|
|
381
|
+
}
|
|
382
|
+
let type = typeof cellValue;
|
|
383
|
+
if (type === 'string') {
|
|
384
|
+
const { row } = cellDetail;
|
|
385
|
+
let result = cellValue;
|
|
386
|
+
if (getValue[cellValue]) {
|
|
387
|
+
result = getValue[cellValue](cellDetail);
|
|
388
|
+
}
|
|
389
|
+
else if (cellValue.indexOf('row.') !== -1) {
|
|
390
|
+
try {
|
|
391
|
+
eval(`result = ${cellValue}`);
|
|
392
|
+
}
|
|
393
|
+
catch (_a) {
|
|
394
|
+
result = '';
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return result === undefined ? def : result;
|
|
398
|
+
}
|
|
399
|
+
if (type === 'undefined') {
|
|
400
|
+
return def;
|
|
401
|
+
}
|
|
402
|
+
if (type === 'function') {
|
|
403
|
+
return cellValue(cellDetail);
|
|
404
|
+
}
|
|
405
|
+
return cellValue === undefined ? def : cellValue;
|
|
406
|
+
};
|
|
407
|
+
const getColValue = (column, field, def) => {
|
|
408
|
+
const colValue = column[field];
|
|
409
|
+
let type = typeof colValue;
|
|
410
|
+
let result;
|
|
411
|
+
if (type === 'function') {
|
|
412
|
+
result = colValue(column);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
result = colValue;
|
|
416
|
+
}
|
|
417
|
+
return result === undefined ? def : result;
|
|
418
|
+
};
|
|
419
|
+
const getCellAttrs = (cellDetail, cellValue) => {
|
|
420
|
+
const { column } = cellDetail;
|
|
421
|
+
const attrs = getCellValue(cellDetail, column.attrs, {});
|
|
422
|
+
const justify = getColValue(column, 'justify', false);
|
|
423
|
+
const width = getColValue(column, 'width');
|
|
424
|
+
const minWidth = getColValue(column, 'minWidth');
|
|
425
|
+
const className = `aio-table-cell` + (justify ? ` aio-table-cell-justify` : '');
|
|
426
|
+
const style = { width, minWidth, flex: width ? undefined : 1 };
|
|
427
|
+
return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof cellValue === 'string' ? cellValue : undefined } });
|
|
428
|
+
};
|
|
429
|
+
const getTitleAttrs = (column) => {
|
|
430
|
+
const attrs = getColValue(column, 'titleAttrs', {});
|
|
431
|
+
const justify = getColValue(column, 'justify', false);
|
|
432
|
+
const width = getColValue(column, 'width');
|
|
433
|
+
const minWidth = getColValue(column, 'minWidth');
|
|
434
|
+
const className = `aio-table-title` + (justify ? ` aio-table-title-justify` : '');
|
|
435
|
+
const style = { width, minWidth, flex: width ? undefined : 1 };
|
|
436
|
+
return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof column.title === 'string' ? column.title : undefined } });
|
|
437
|
+
};
|
|
438
|
+
const getRowAttrs = (rowDetail) => {
|
|
439
|
+
const { rowOption = {}, onSwap, value, gap = [0, 1] } = getProps();
|
|
440
|
+
const { attrs: rowAttrs } = rowOption;
|
|
441
|
+
const attrs = rowAttrs ? rowAttrs(rowDetail) : {};
|
|
442
|
+
let obj = UT.AddToAttrs(attrs, { className: 'aio-table-row', style: { gap: gap[0] } });
|
|
443
|
+
if (onSwap) {
|
|
444
|
+
obj = Object.assign(Object.assign(Object.assign({}, obj), DragRows.getDragAttrs({ dragIndex: rowDetail.rowIndex })), DragRows.getDropAttrs({ dropIndex: rowDetail.rowIndex, rows: value }));
|
|
445
|
+
}
|
|
446
|
+
return obj;
|
|
447
|
+
};
|
|
448
|
+
return { getCellValue, getColValue, getCellAttrs, getTitleAttrs, getRowAttrs };
|
|
523
449
|
};
|