aio-table 14.0.0 → 14.0.1

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 (4) hide show
  1. package/index.css +4 -3
  2. package/index.d.ts +14 -15
  3. package/index.js +70 -127
  4. package/package.json +1 -1
package/index.css CHANGED
@@ -163,9 +163,9 @@
163
163
  background: #f8f8f8;
164
164
  z-index: 100;
165
165
  box-sizing: border-box;
166
- min-height: 36px;
167
- gap: 6px;
168
- padding: 6px;
166
+ height:3em;
167
+ gap: 0.4em;
168
+ padding: 0.4em;
169
169
  }
170
170
 
171
171
  .aio-table-toolbar-content {
@@ -219,6 +219,7 @@
219
219
  flex: 1;
220
220
  height: 100%;
221
221
  box-sizing: border-box;
222
+ border:none;
222
223
  }
223
224
 
224
225
  .aio-table-search .aio-input {
package/index.d.ts CHANGED
@@ -93,6 +93,13 @@ export type I_table_column<T> = {
93
93
  pattern?: string;
94
94
  affix?: string;
95
95
  prefix?: string;
96
+ cellAttrs?: (p: {
97
+ row: T;
98
+ column: I_table_column<T>;
99
+ value: any;
100
+ rowIndex: number;
101
+ }) => any;
102
+ titleAttrs?: (column: I_table_column<T>) => any;
96
103
  };
97
104
  export type I_table_filter_saved = {
98
105
  name: string;
@@ -125,25 +132,18 @@ export type I_table<T> = {
125
132
  columns?: I_table_column<T>[];
126
133
  excel?: string | ((value: any[]) => any[]);
127
134
  headerAttrs?: any;
128
- onAdd?: () => Promise<T | undefined>;
129
- onRemove?: (p: {
130
- row: T;
131
- rowIndex?: number;
132
- }) => Promise<boolean>;
133
135
  onChangePaging?: (newPaging: I_table_paging) => void;
134
136
  onChangeSort?: (sorts: I_table_sort<T>[]) => Promise<boolean>;
135
- onSwap?: true | ((newValue: T[], startRow: T, endRow: T) => void);
137
+ onSwap?: (newValue: T[], startRow: T, endRow: T) => void;
136
138
  onSearch?: true | ((searchValue: string) => void);
137
139
  paging?: I_table_paging;
138
140
  removeText?: string;
139
- rowOption?: {
140
- before?: I_rowOption<T, ReactNode>;
141
- after?: I_rowOption<T, ReactNode>;
142
- attrs?: I_rowOption<T, {
143
- [attrs: string]: any;
144
- }>;
145
- template?: I_rowOption<T, ReactNode>;
146
- };
141
+ rowAttrs?: I_rowOption<T, {
142
+ [attrs: string]: any;
143
+ }>;
144
+ rowBefore?: I_rowOption<T, ReactNode>;
145
+ rowAfter?: I_rowOption<T, ReactNode>;
146
+ rowTemplate?: I_rowOption<T, ReactNode>;
147
147
  rowsTemplate?: (rows: T[]) => ReactNode;
148
148
  toolbar?: ReactNode | (() => ReactNode);
149
149
  toolbarAttrs?: any;
@@ -227,4 +227,3 @@ export type I_pagingHook<T> = {
227
227
  getPagedRows: (rows: T[]) => T[];
228
228
  changePaging: (obj: Partial<I_table_paging>) => void;
229
229
  };
230
- export declare function sortRows<T>(rows: T[], sorts: I_table_sort<T>[]): T[];
package/index.js CHANGED
@@ -57,7 +57,11 @@ const AIOTable = (props) => {
57
57
  }
58
58
  }
59
59
  }
60
- return Object.assign(Object.assign({}, column), col);
60
+ const res = Object.assign(Object.assign({}, column), col);
61
+ if (!res.value) {
62
+ res.value = `row.${column.id}`;
63
+ }
64
+ return res;
61
65
  };
62
66
  const getColumns = () => {
63
67
  let { columns = [] } = props;
@@ -141,28 +145,6 @@ const AIOTable = (props) => {
141
145
  pagingHook.changePaging(props.paging);
142
146
  }
143
147
  }, [JSON.stringify(props.paging)]);
144
- function add() {
145
- return __awaiter(this, void 0, void 0, function* () {
146
- if (!props.onAdd) {
147
- return;
148
- }
149
- const res = yield props.onAdd();
150
- if (res && props.onChange) {
151
- props.onChange([res, ...props.value]);
152
- }
153
- });
154
- }
155
- function remove(row, index) {
156
- return __awaiter(this, void 0, void 0, function* () {
157
- if (!props.onRemove) {
158
- return;
159
- }
160
- const res = yield props.onRemove({ row, rowIndex: index });
161
- if (res === true && props.onChange) {
162
- props.onChange(props.value.filter((o) => o._id !== row._id));
163
- }
164
- });
165
- }
166
148
  function exportToExcel() {
167
149
  let list = [];
168
150
  if (typeof props.excel === 'function') {
@@ -230,11 +212,11 @@ const AIOTable = (props) => {
230
212
  props.onSearch(searchValue);
231
213
  }
232
214
  }
233
- let ROWS = getRows();
234
215
  const { columns, searchColumns, excelColumns, filterColumns, toggleColumns } = useMemo(() => getColumns(), [props.columns]);
216
+ let ROWS = getRows();
235
217
  const context = {
236
218
  rootProps: props, excelColumns, filterColumns, tableHook, sortHook, ROWS, toggleColumns, pagingHook,
237
- add, remove, search, exportToExcel, DragColumns, popup, columns
219
+ search, exportToExcel, DragColumns, popup, columns
238
220
  };
239
221
  return (_jsxs(Provider, { value: context, children: [_jsx(AIOTableComponent, Object.assign({}, props)), popup.render()] }));
240
222
  };
@@ -250,27 +232,50 @@ const AIOTableComponent = (props) => {
250
232
  };
251
233
  const TableRows = () => {
252
234
  let { ROWS, rootProps } = useProvider();
253
- let { rowOption = {}, rowsTemplate, placeholder = 'there is not any items' } = rootProps;
254
- const { before: rowBefore = () => null, after: rowAfter = () => null, template: rowTemplate, } = rowOption;
235
+ let { rowBefore = () => null, rowAfter = () => null, rowTemplate, rowsTemplate, placeholder = 'there is not any items' } = rootProps;
255
236
  let rows = ROWS.pagedRows || [];
256
237
  let content;
257
238
  if (rowsTemplate) {
258
239
  content = rowsTemplate(rows);
259
240
  }
260
241
  else if (rows.length) {
261
- content = rows.map((o, i) => {
262
- let { id = 'ailr' + Math.round(Math.random() * 10000000) } = o;
263
- o._id = o._id === undefined ? id : o._id;
264
- const rowDetail = { row: o, rowIndex: i };
265
- let Row;
266
- if (rowTemplate) {
267
- Row = rowTemplate(rowDetail);
268
- }
269
- else {
270
- Row = _jsx(TableRow, { rowDetail: rowDetail }, o._id);
271
- }
272
- return (_jsxs(Fragment, { children: [rowBefore(rowDetail), Row, rowAfter(rowDetail)] }, o._id));
273
- });
242
+ if (rootProps.onSwap) {
243
+ content = (_jsx(UT.DragAndDrop, { data: rows, getId: (o) => {
244
+ let { id = 'ailr' + Math.round(Math.random() * 10000000) } = o;
245
+ o._id = o._id === undefined ? id : o._id;
246
+ return o._id;
247
+ }, getContent: (o, { index, id }) => {
248
+ o._id = o._id === undefined ? id : o._id;
249
+ const rowDetail = { row: o, rowIndex: index };
250
+ let Row;
251
+ if (rowTemplate) {
252
+ Row = rowTemplate(rowDetail);
253
+ }
254
+ else {
255
+ Row = _jsx(TableRow, { rowDetail: rowDetail }, o._id);
256
+ }
257
+ return (_jsxs(Fragment, { children: [rowBefore(rowDetail), Row, rowAfter(rowDetail)] }, o._id));
258
+ }, onChange: (newRows) => {
259
+ if (rootProps.onChange) {
260
+ rootProps.onChange(newRows);
261
+ }
262
+ } }));
263
+ }
264
+ else {
265
+ content = rows.map((o, i) => {
266
+ let { id = 'ailr' + Math.round(Math.random() * 10000000) } = o;
267
+ o._id = o._id === undefined ? id : o._id;
268
+ const rowDetail = { row: o, rowIndex: i };
269
+ let Row;
270
+ if (rowTemplate) {
271
+ Row = rowTemplate(rowDetail);
272
+ }
273
+ else {
274
+ Row = _jsx(TableRow, { rowDetail: rowDetail }, o._id);
275
+ }
276
+ return (_jsxs(Fragment, { children: [rowBefore(rowDetail), Row, rowAfter(rowDetail)] }, o._id));
277
+ });
278
+ }
274
279
  }
275
280
  else if (placeholder) {
276
281
  content = _jsx("div", { style: { width: '100%', textAlign: 'center', padding: 12, boxSizing: 'border-box' }, children: placeholder });
@@ -282,12 +287,12 @@ const TableRows = () => {
282
287
  return _jsx("div", { className: 'aio-table-rows', style: { gap: gap[1] }, children: content });
283
288
  };
284
289
  const TableToolbar = ({ filterHook }) => {
285
- let { add, exportToExcel, search, rootProps, excelColumns, toggleColumns, sortHook, columns } = useProvider();
286
- let { toolbarAttrs, toolbar, onAdd, onSearch, value, onChangeColumns, fa, filter, inlineSort } = rootProps;
290
+ let { exportToExcel, search, rootProps, excelColumns, toggleColumns, sortHook, columns } = useProvider();
291
+ let { toolbarAttrs, toolbar, onSearch, value, onChangeColumns, fa, filter, inlineSort } = rootProps;
287
292
  const columnsRef = useRef(columns);
288
293
  columnsRef.current = columns;
289
294
  toolbarAttrs = UT.AddToAttrs(toolbarAttrs, { className: 'aio-table-toolbar' });
290
- if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length && !toggleColumns.length && !filter) {
295
+ if (!toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length && !toggleColumns.length && !filter) {
291
296
  return null;
292
297
  }
293
298
  function getAddText() {
@@ -317,18 +322,17 @@ const TableToolbar = ({ filterHook }) => {
317
322
  show: () => true,
318
323
  justify: () => false,
319
324
  close: () => false
320
- } }), !!onAdd && _jsx("div", { className: 'aio-table-toolbar-button aio-table-add-button', onClick: () => add(), children: getAddText() })] })));
325
+ } })] })));
321
326
  };
322
327
  const TableHeader = () => {
323
328
  let { rootProps, columns } = useProvider();
324
- let { headerAttrs, onRemove, gap = [] } = rootProps;
329
+ let { headerAttrs, gap = [] } = rootProps;
325
330
  headerAttrs = UT.AddToAttrs(headerAttrs, { className: 'aio-table-header', style: { gap: gap[0] } });
326
331
  let Titles = columns.map((column, i) => {
327
332
  const columnId = column.id;
328
333
  return _jsx(TableTitle, { column: column, isLast: i === columns.length - 1, colIndex: i }, columnId);
329
334
  });
330
- let RemoveTitle = !onRemove ? null : _jsx("div", { className: 'aio-table-remove-title' });
331
- return _jsxs("div", Object.assign({}, headerAttrs, { children: [Titles, RemoveTitle] }));
335
+ return _jsx("div", Object.assign({}, headerAttrs, { children: Titles }));
332
336
  };
333
337
  const TableTitle = (p) => {
334
338
  const { column, colIndex } = p;
@@ -344,7 +348,7 @@ const TableRow = (props) => {
344
348
  const { rowDetail } = props;
345
349
  const { row, rowIndex } = rowDetail;
346
350
  const rowId = row._id;
347
- let { remove, rootProps, tableHook, columns } = useProvider();
351
+ let { rootProps, tableHook, columns } = useProvider();
348
352
  function getCells() {
349
353
  return columns.map((column) => {
350
354
  const { show = true } = column;
@@ -356,14 +360,12 @@ const TableRow = (props) => {
356
360
  return (_jsx(TableCell, { row: row, rowIndex: rowIndex, column: column }, key));
357
361
  });
358
362
  }
359
- let { onRemove } = rootProps;
360
- return (_jsx(_Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getRowAttrs({ row, rowIndex }), { children: [getCells(), onRemove ? _jsx("button", { className: 'aio-table-remove', onClick: () => remove(row, rowIndex), children: new UT.GetSvg('em').getIcon('mdiClose', 0.8) }) : null] }), rowId) }));
363
+ return (_jsx("div", Object.assign({}, tableHook.getRowAttrs({ row, rowIndex }), { children: getCells() }), rowId));
361
364
  };
362
365
  const TableCell = (props) => {
363
366
  var _a, _b;
364
367
  let { row, column, rowIndex } = props;
365
- const { tableHook, rootProps } = useProvider();
366
- const { templates } = rootProps;
368
+ const { tableHook } = useProvider();
367
369
  const { type = 'text', id } = column;
368
370
  let cellValue = tableHook.getCellValue({ row, rowIndex, column });
369
371
  if (type === 'date') {
@@ -417,18 +419,6 @@ const TableCell = (props) => {
417
419
  return (_jsx(Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getCellAttrs({ row, column, rowIndex }), { children: [before !== undefined && _jsx("div", { className: "aio-table-cell-before", children: before }), _jsx("div", { className: `aio-table-cell-value${subtext !== undefined ? ' has-subtext' : ''}`, "data-subtext": subtext, children: content }), after !== undefined && _jsx("div", { className: "aio-table-cell-after", children: after })] })) }, rowId + ' ' + colId));
418
420
  };
419
421
  const useTable = (getProps, getPaging) => {
420
- const DragRows = UT.useDrag((dragIndex, dropIndex, reOrder) => {
421
- const { onSwap, onChange, value: rows } = getProps();
422
- const newRows = reOrder(rows, dragIndex, dropIndex);
423
- const from = rows[dragIndex];
424
- const to = rows[dropIndex];
425
- if (typeof onSwap === 'function') {
426
- onSwap(newRows, from, to);
427
- }
428
- else if (onChange) {
429
- onChange(newRows);
430
- }
431
- }, 'aio-table-row');
432
422
  const fixIndexByPaging = (rowIndex) => {
433
423
  const paging = getPaging();
434
424
  if (paging) {
@@ -531,7 +521,9 @@ const useTable = (getProps, getPaging) => {
531
521
  const { row, rowIndex, column } = p;
532
522
  const { cellAttrs } = getProps();
533
523
  const cellValue = getCellValue({ row, column, rowIndex });
534
- const attrs = cellAttrs ? cellAttrs({ row, rowIndex: fixIndexByPaging(rowIndex), column, value: cellValue }) : {};
524
+ const propsCellAttrs = cellAttrs ? cellAttrs({ row, rowIndex: fixIndexByPaging(rowIndex), column, value: cellValue }) : {};
525
+ const columnCellAttrs = column.cellAttrs ? column.cellAttrs({ row, rowIndex: fixIndexByPaging(rowIndex), column, value: cellValue }) : {};
526
+ const attrs = Object.assign(Object.assign({}, propsCellAttrs), columnCellAttrs);
535
527
  const { justify, width, minWidth } = column;
536
528
  const className = `aio-table-cell` + (justify ? ` aio-table-cell-justify` : '');
537
529
  let isNumber = false;
@@ -550,19 +542,20 @@ const useTable = (getProps, getPaging) => {
550
542
  const getTitleAttrs = (column) => {
551
543
  const { justify, width, minWidth, title } = column;
552
544
  const { titleAttrs } = getProps();
553
- const attrs = titleAttrs ? titleAttrs(column) : {};
545
+ const propsTitleAttrs = titleAttrs ? titleAttrs(column) : {};
546
+ const columnTitleAttrs = column.titleAttrs ? column.titleAttrs(column) : {};
547
+ const attrs = Object.assign(Object.assign({}, propsTitleAttrs), columnTitleAttrs);
554
548
  const className = `aio-table-title` + (justify ? ` aio-table-title-justify` : '');
555
549
  const style = { width, minWidth, flex: width ? undefined : 1 };
556
550
  return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof title === 'string' ? title : undefined } });
557
551
  };
558
552
  const getRowAttrs = (p) => {
559
553
  const { row, rowIndex } = p;
560
- const { rowOption = {}, onSwap, gap = [] } = getProps();
561
- const { attrs: rowAttrs } = rowOption;
554
+ const { onSwap, gap = [], rowAttrs } = getProps();
562
555
  const attrs = rowAttrs ? rowAttrs({ row, rowIndex }) : {};
563
556
  let obj = UT.AddToAttrs(attrs, { className: ['aio-table-row', rowIndex % 2 === 0 ? 'aio-table-row-odd' : 'aio-table-row-even'], style: { gap: gap[0] } });
564
557
  if (onSwap) {
565
- obj = Object.assign(Object.assign(Object.assign({}, obj), DragRows.getDragAttrs(rowIndex)), DragRows.getDropAttrs(rowIndex));
558
+ obj = Object.assign({}, obj);
566
559
  }
567
560
  return obj;
568
561
  };
@@ -571,6 +564,10 @@ const useTable = (getProps, getPaging) => {
571
564
  export const useSort = (p) => {
572
565
  let [sorts, setSorts] = useState(p.sorts);
573
566
  const isInitSortExecutedRef = useRef(false);
567
+ const getActiveSorts = (sorts) => {
568
+ let activeSorts = (sorts || []).filter((sort) => sort.active !== false);
569
+ return activeSorts.map((o) => ({ getValue: o.getValue, dir: o.dir }));
570
+ };
574
571
  const getSortedRows = (rows) => {
575
572
  if (isInitSortExecutedRef.current) {
576
573
  return rows;
@@ -578,44 +575,17 @@ export const useSort = (p) => {
578
575
  if (p.onChangeSort) {
579
576
  return rows;
580
577
  }
581
- let activeSorts = sorts.filter((sort) => sort.active !== false);
578
+ let activeSorts = getActiveSorts(sorts);
582
579
  if (!activeSorts.length || !rows.length) {
583
580
  return rows;
584
581
  }
585
582
  isInitSortExecutedRef.current = true;
586
- let sortedRows = sortRows(rows, activeSorts);
583
+ let sortedRows = UT.SortArray(rows, activeSorts);
587
584
  if (p.onChangeRows) {
588
585
  p.onChangeRows(sortedRows);
589
586
  }
590
587
  return sortedRows;
591
588
  };
592
- const sortRows = (rows, sorts) => {
593
- if (!rows) {
594
- return [];
595
- }
596
- if (!sorts || !sorts.length) {
597
- return rows;
598
- }
599
- return rows.sort((a, b) => {
600
- for (let i = 0; i < sorts.length; i++) {
601
- let { dir, getValue } = sorts[i];
602
- if (!getValue) {
603
- return 0;
604
- }
605
- let aValue = getValue(a), bValue = getValue(b);
606
- if (aValue < bValue) {
607
- return -1 * (dir === 'dec' ? -1 : 1);
608
- }
609
- if (aValue > bValue) {
610
- return 1 * (dir === 'dec' ? -1 : 1);
611
- }
612
- if (i === sorts.length - 1) {
613
- return 0;
614
- }
615
- }
616
- return 0;
617
- });
618
- };
619
589
  const changeSort = (sortId, changeObject) => {
620
590
  let newSorts = sorts.map((sort) => {
621
591
  if (sort.sortId === sortId) {
@@ -635,9 +605,9 @@ export const useSort = (p) => {
635
605
  }
636
606
  else {
637
607
  setSorts(sorts);
638
- let activeSorts = sorts.filter((sort) => sort.active !== false);
608
+ const activeSorts = getActiveSorts(sorts);
639
609
  if (activeSorts.length && !!p.onChangeRows) {
640
- p.onChangeRows(sortRows(p.rows, activeSorts));
610
+ p.onChangeRows(UT.SortArray(p.rows, activeSorts));
641
611
  }
642
612
  }
643
613
  });
@@ -1222,30 +1192,3 @@ const useFilterData = (popup, trans) => {
1222
1192
  };
1223
1193
  return { getSaves, getData, save, removeSave, removeFilterItem, changeColumnsFilterItem, activeName, activeSave, addColumnsFilterItem };
1224
1194
  };
1225
- export function sortRows(rows, sorts) {
1226
- if (!rows) {
1227
- return [];
1228
- }
1229
- if (!sorts || !sorts.length) {
1230
- return rows;
1231
- }
1232
- return rows.sort((a, b) => {
1233
- for (let i = 0; i < sorts.length; i++) {
1234
- let { dir, getValue } = sorts[i];
1235
- if (!getValue) {
1236
- return 0;
1237
- }
1238
- let aValue = getValue(a), bValue = getValue(b);
1239
- if (aValue < bValue) {
1240
- return -1 * (dir === 'dec' ? -1 : 1);
1241
- }
1242
- if (aValue > bValue) {
1243
- return 1 * (dir === 'dec' ? -1 : 1);
1244
- }
1245
- if (i === sorts.length - 1) {
1246
- return 0;
1247
- }
1248
- }
1249
- return 0;
1250
- });
1251
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-table",
3
- "version": "14.0.0",
3
+ "version": "14.0.1",
4
4
  "description": "all in one table. tree mode , simple mode , tree mode, gantt mode , groupby mode, freeze mode.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",