aio-table 10.0.0 → 11.0.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -3
  2. package/index.js +30 -20
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -25,8 +25,7 @@ export type I_table_column<T> = {
25
25
  jalali?: boolean;
26
26
  filterId?: string;
27
27
  search?: boolean;
28
- id?: string;
29
- _id?: string;
28
+ id: string;
30
29
  width?: any;
31
30
  minWidth?: any;
32
31
  excel?: string | boolean;
@@ -99,7 +98,7 @@ export type I_table<T> = {
99
98
  gap?: [number, number];
100
99
  striped?: [string, string];
101
100
  columnOption?: {
102
- [key in keyof I_table_column<any>]: (p: I_cellDetail<T>) => any;
101
+ [key in keyof I_table_column<any>]?: (p: I_cellDetail<any>) => any;
103
102
  };
104
103
  onChangeColumns?: (v: I_table_column<any>[]) => void;
105
104
  };
package/index.js CHANGED
@@ -21,7 +21,6 @@ const AIOTable = (props) => {
21
21
  const popup = usePopup();
22
22
  let [dom] = useState(createRef());
23
23
  let [searchValue, setSearchValue] = useState('');
24
- const [columns, setColumns] = useState([]);
25
24
  const [searchColumns, setSearchColumns] = useState([]);
26
25
  const [excelColumns, setExcelColumns] = useState([]);
27
26
  const [filterColumns, setFilterColumns] = useState([]);
@@ -42,12 +41,15 @@ const AIOTable = (props) => {
42
41
  }
43
42
  return cellDetails.column[key];
44
43
  }
45
- const { columnOption = {} } = props;
46
- const fn = columnOption[key] || (() => { });
44
+ const { columnOption } = props;
45
+ const fn = (columnOption || {})[key] || (() => { });
47
46
  const res = fn(cellDetails);
48
47
  if (res !== undefined) {
49
48
  return res;
50
49
  }
50
+ if (key === 'value') {
51
+ return cellDetails.row[cellDetails.column.id];
52
+ }
51
53
  const defaults = { show: true, title: '', justify: false, titleAttrs: {} };
52
54
  return defaults[key];
53
55
  };
@@ -55,11 +57,10 @@ const AIOTable = (props) => {
55
57
  const getIconRef = useRef(new UT.GetSvg());
56
58
  const getIcon = getIconRef.current.getIcon;
57
59
  const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => {
58
- const newColumns = reOrder(columns, dragIndex, dropIndex);
60
+ const newColumns = reOrder(props.columns || [], dragIndex, dropIndex);
59
61
  if (props.onChangeColumns) {
60
62
  props.onChangeColumns(newColumns);
61
63
  }
62
- setColumns(newColumns);
63
64
  }, 'aio-table-title', 'aio-table-unit');
64
65
  const getGetValue = (sort, column) => {
65
66
  if (sort.getValue) {
@@ -86,10 +87,10 @@ const AIOTable = (props) => {
86
87
  let sorts = [];
87
88
  for (let i = 0; i < columns.length; i++) {
88
89
  const column = columns[i];
89
- const { _id } = column;
90
90
  const cellDetail = tableHook.getCellDetail({ column });
91
+ const columnId = getColumnOption('id', cellDetail);
91
92
  const sortValue = getColumnOption('sort', cellDetail);
92
- const sort = sortValue === true ? { sortId: _id } : sortValue;
93
+ const sort = sortValue === true ? { sortId: columnId } : sortValue;
93
94
  if (!sort) {
94
95
  continue;
95
96
  }
@@ -108,13 +109,15 @@ const AIOTable = (props) => {
108
109
  function getColumns() {
109
110
  let { columns = [] } = props;
110
111
  let searchColumns = [], excelColumns = [], filterColumns = [];
111
- let updatedColumns = columns.map((column) => {
112
+ let updatedColumns = columns.map((column, i) => {
112
113
  const cellDetail = tableHook.getCellDetail({ column });
113
114
  const filterId = getColumnOption('filterId', cellDetail);
114
115
  const search = getColumnOption('search', cellDetail);
115
116
  const excel = getColumnOption('excel', cellDetail);
116
- const id = getColumnOption('id', cellDetail) || 'aitc' + Math.round(Math.random() * 1000000);
117
- column = Object.assign(Object.assign({}, column), { _id: id });
117
+ const id = getColumnOption('id', cellDetail);
118
+ if (id === undefined) {
119
+ console.error(`aio-table error => missing column id in props.columns[${i}]`);
120
+ }
118
121
  if (search) {
119
122
  searchColumns.push(column);
120
123
  }
@@ -126,7 +129,9 @@ const AIOTable = (props) => {
126
129
  }
127
130
  return column;
128
131
  });
129
- setColumns(updatedColumns);
132
+ if (props.onChangeColumns) {
133
+ props.onChangeColumns(updatedColumns);
134
+ }
130
135
  setSearchColumns(searchColumns);
131
136
  setExcelColumns(excelColumns);
132
137
  setFilterColumns(filterColumns);
@@ -248,7 +253,7 @@ const AIOTable = (props) => {
248
253
  const { gap = [0, 1] } = props;
249
254
  let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
250
255
  const context = {
251
- rootProps: props, columns, excelColumns, filterColumns, tableHook, sortHook, ROWS,
256
+ rootProps: props, excelColumns, filterColumns, tableHook, sortHook, ROWS,
252
257
  getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, getColumnOption
253
258
  };
254
259
  return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter &&
@@ -314,10 +319,13 @@ const TableToolbar = () => {
314
319
  _jsx("div", { className: 'aio-table-search', children: _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() })] })));
315
320
  };
316
321
  const TableHeader = () => {
317
- let { rootProps, columns } = useProvider();
318
- let { headerAttrs, onRemove, gap = [0, 1] } = rootProps;
322
+ let { rootProps, getColumnOption, tableHook } = useProvider();
323
+ let { headerAttrs, onRemove, gap = [0, 1], columns = [] } = rootProps;
319
324
  headerAttrs = UT.AddToAttrs(headerAttrs, { className: 'aio-table-header', style: { gap: gap[0] } });
320
- let Titles = columns.map((o, i) => _jsx(TableTitle, { column: o, isLast: i === columns.length - 1, colIndex: i }, o._id));
325
+ let Titles = columns.map((column, i) => {
326
+ const columnId = getColumnOption('id', tableHook.getCellDetail({ column }));
327
+ return _jsx(TableTitle, { column: column, isLast: i === columns.length - 1, colIndex: i }, columnId);
328
+ });
321
329
  let RemoveTitle = !onRemove ? null : _jsx("div", { className: 'aio-table-remove-title' });
322
330
  return _jsxs("div", Object.assign({}, headerAttrs, { children: [Titles, RemoveTitle] }));
323
331
  };
@@ -334,15 +342,17 @@ const TableRow = (props) => {
334
342
  const { rowDetail } = props;
335
343
  const { row, rowIndex } = rowDetail;
336
344
  const rowId = row._id;
337
- let { remove, rootProps, columns, tableHook, getIcon, getColumnOption } = useProvider();
345
+ let { remove, rootProps, tableHook, getIcon, getColumnOption } = useProvider();
346
+ const { columns = [] } = rootProps;
338
347
  const isOdd = rowIndex % 2 === 0;
339
348
  function getCells() {
340
349
  return columns.map((column) => {
341
350
  if (!getColumnOption('show', tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })))) {
342
351
  return null;
343
352
  }
344
- const key = rowId + ' ' + column._id;
345
353
  const cellDetail = tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column, change: (v) => tableHook.changeCell(tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })), v) }));
354
+ const columnId = getColumnOption('id', cellDetail);
355
+ const key = rowId + ' ' + columnId;
346
356
  const value = getColumnOption('value', cellDetail);
347
357
  const cellValue = tableHook.getCellValue(cellDetail, value);
348
358
  return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue, isOdd: isOdd }, key));
@@ -355,7 +365,7 @@ const TableCell = (props) => {
355
365
  let { cellDetail, cellValue, isOdd } = props;
356
366
  const { row, column } = cellDetail;
357
367
  const { tableHook, rootProps, getColumnOption } = useProvider();
358
- const { columnOption = {} } = rootProps;
368
+ const { columnOption } = rootProps;
359
369
  const type = getColumnOption('type', cellDetail);
360
370
  if (type === 'number') {
361
371
  cellValue = UT.SplitNumber(cellValue);
@@ -364,7 +374,7 @@ const TableCell = (props) => {
364
374
  const after = getColumnOption('after', cellDetail);
365
375
  const subtext = getColumnOption('subtext', cellDetail);
366
376
  const rowId = row._id;
367
- const colId = column._id;
377
+ const colId = getColumnOption('id', cellDetail);
368
378
  const getTemplateValue = () => {
369
379
  const { getValue = {} } = rootProps;
370
380
  if (typeof column.template === 'function') {
@@ -373,7 +383,7 @@ const TableCell = (props) => {
373
383
  if (typeof column.template === 'string' && getValue[column.template]) {
374
384
  return getValue[column.template](cellDetail);
375
385
  }
376
- if (columnOption.template) {
386
+ if ((columnOption || {}).template) {
377
387
  return columnOption.template(cellDetail);
378
388
  }
379
389
  const pattern = getColumnOption('pattern', cellDetail);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-table",
3
- "version": "10.0.0",
3
+ "version": "11.0.0",
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",