aio-table 10.0.0 → 11.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.
package/index.css CHANGED
@@ -182,7 +182,10 @@
182
182
  border: none;
183
183
  background: none;
184
184
  }
185
-
185
+ .aio-table-toolbar-button .aio-input{
186
+ border:none;
187
+ background: none;
188
+ }
186
189
 
187
190
 
188
191
  .aio-table-remove {
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;
@@ -46,6 +45,7 @@ export type I_table_column<T> = {
46
45
  show?: boolean;
47
46
  pattern?: string;
48
47
  data?: any;
48
+ toggle?: boolean;
49
49
  };
50
50
  export type I_table<T> = {
51
51
  fa?: boolean;
@@ -99,7 +99,7 @@ export type I_table<T> = {
99
99
  gap?: [number, number];
100
100
  striped?: [string, string];
101
101
  columnOption?: {
102
- [key in keyof I_table_column<any>]: (p: I_cellDetail<T>) => any;
102
+ [key in keyof I_table_column<any>]?: (p: I_cellDetail<any>) => any;
103
103
  };
104
104
  onChangeColumns?: (v: I_table_column<any>[]) => void;
105
105
  };
package/index.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
11
11
  import { createContext, createRef, Fragment, useContext, useEffect, useRef, useState } from "react";
12
- import AIOInput from "aio-input";
12
+ import AIOInput, { AISelect } from "aio-input";
13
13
  import * as UT from 'aio-utils';
14
14
  import usePopup from "aio-popup";
15
15
  import AIODate from "aio-date";
@@ -21,10 +21,10 @@ 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([]);
27
+ const [toggleColumns, setToggleColumns] = useState([]);
28
28
  const filterColumnsRef = useRef(filterColumns);
29
29
  filterColumnsRef.current = filterColumns;
30
30
  const rowsIndexDicRef = useRef({});
@@ -42,12 +42,15 @@ const AIOTable = (props) => {
42
42
  }
43
43
  return cellDetails.column[key];
44
44
  }
45
- const { columnOption = {} } = props;
46
- const fn = columnOption[key] || (() => { });
45
+ const { columnOption } = props;
46
+ const fn = (columnOption || {})[key] || (() => { });
47
47
  const res = fn(cellDetails);
48
48
  if (res !== undefined) {
49
49
  return res;
50
50
  }
51
+ if (key === 'value') {
52
+ return cellDetails.row[cellDetails.column.id];
53
+ }
51
54
  const defaults = { show: true, title: '', justify: false, titleAttrs: {} };
52
55
  return defaults[key];
53
56
  };
@@ -55,11 +58,10 @@ const AIOTable = (props) => {
55
58
  const getIconRef = useRef(new UT.GetSvg());
56
59
  const getIcon = getIconRef.current.getIcon;
57
60
  const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => {
58
- const newColumns = reOrder(columns, dragIndex, dropIndex);
61
+ const newColumns = reOrder(props.columns || [], dragIndex, dropIndex);
59
62
  if (props.onChangeColumns) {
60
63
  props.onChangeColumns(newColumns);
61
64
  }
62
- setColumns(newColumns);
63
65
  }, 'aio-table-title', 'aio-table-unit');
64
66
  const getGetValue = (sort, column) => {
65
67
  if (sort.getValue) {
@@ -86,10 +88,10 @@ const AIOTable = (props) => {
86
88
  let sorts = [];
87
89
  for (let i = 0; i < columns.length; i++) {
88
90
  const column = columns[i];
89
- const { _id } = column;
90
91
  const cellDetail = tableHook.getCellDetail({ column });
92
+ const columnId = getColumnOption('id', cellDetail);
91
93
  const sortValue = getColumnOption('sort', cellDetail);
92
- const sort = sortValue === true ? { sortId: _id } : sortValue;
94
+ const sort = sortValue === true ? { sortId: columnId } : sortValue;
93
95
  if (!sort) {
94
96
  continue;
95
97
  }
@@ -107,14 +109,17 @@ const AIOTable = (props) => {
107
109
  });
108
110
  function getColumns() {
109
111
  let { columns = [] } = props;
110
- let searchColumns = [], excelColumns = [], filterColumns = [];
111
- let updatedColumns = columns.map((column) => {
112
+ let searchColumns = [], excelColumns = [], filterColumns = [], toggleColumns = [];
113
+ let updatedColumns = columns.map((column, i) => {
112
114
  const cellDetail = tableHook.getCellDetail({ column });
113
115
  const filterId = getColumnOption('filterId', cellDetail);
114
116
  const search = getColumnOption('search', cellDetail);
115
117
  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 });
118
+ const toggle = getColumnOption('toggle', cellDetail);
119
+ const id = getColumnOption('id', cellDetail);
120
+ if (id === undefined) {
121
+ console.error(`aio-table error => missing column id in props.columns[${i}]`);
122
+ }
118
123
  if (search) {
119
124
  searchColumns.push(column);
120
125
  }
@@ -124,12 +129,18 @@ const AIOTable = (props) => {
124
129
  if (filterId) {
125
130
  filterColumns.push(column);
126
131
  }
132
+ if (toggle) {
133
+ toggleColumns.push(column);
134
+ }
127
135
  return column;
128
136
  });
129
- setColumns(updatedColumns);
137
+ if (props.onChangeColumns) {
138
+ props.onChangeColumns(updatedColumns);
139
+ }
130
140
  setSearchColumns(searchColumns);
131
141
  setExcelColumns(excelColumns);
132
142
  setFilterColumns(filterColumns);
143
+ setToggleColumns(toggleColumns);
133
144
  return updatedColumns;
134
145
  }
135
146
  useEffect(() => {
@@ -248,7 +259,7 @@ const AIOTable = (props) => {
248
259
  const { gap = [0, 1] } = props;
249
260
  let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
250
261
  const context = {
251
- rootProps: props, columns, excelColumns, filterColumns, tableHook, sortHook, ROWS,
262
+ rootProps: props, excelColumns, filterColumns, tableHook, sortHook, ROWS, toggleColumns,
252
263
  getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, getColumnOption
253
264
  };
254
265
  return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter &&
@@ -297,10 +308,12 @@ const TableRows = () => {
297
308
  return _jsx("div", { className: 'aio-table-rows', style: { gap: gap[1] }, children: content });
298
309
  };
299
310
  const TableToolbar = () => {
300
- let { add, exportToExcel, search, rootProps, excelColumns, getIcon, sortHook } = useProvider();
301
- let { toolbarAttrs, toolbar, onAdd, onSearch, value } = rootProps;
311
+ let { add, exportToExcel, search, rootProps, excelColumns, toggleColumns, getIcon, sortHook, getColumnOption, tableHook } = useProvider();
312
+ let { toolbarAttrs, toolbar, onAdd, onSearch, value, onChangeColumns, columns } = rootProps;
313
+ const columnsRef = useRef(columns);
314
+ columnsRef.current = columns;
302
315
  toolbarAttrs = UT.AddToAttrs(toolbarAttrs, { className: 'aio-table-toolbar' });
303
- if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length) {
316
+ if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length && !toggleColumns.length) {
304
317
  return null;
305
318
  }
306
319
  function getAddText() {
@@ -310,14 +323,34 @@ const TableToolbar = () => {
310
323
  }
311
324
  return typeof addText === 'function' ? addText(value) : addText;
312
325
  }
326
+ const getColumnOptionWrapper = (columnId, key) => {
327
+ const columns = columnsRef.current || [];
328
+ const column = columns.find((o) => o.id === columnId);
329
+ return getColumnOption(key, tableHook.getCellDetail({ column }));
330
+ };
313
331
  return (_jsxs("div", Object.assign({}, toolbarAttrs, { children: [toolbar && _jsx("div", { className: 'aio-table-toolbar-content', children: typeof toolbar === 'function' ? toolbar() : toolbar }), !!onSearch &&
314
- _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() })] })));
332
+ _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) }), !!toggleColumns.length &&
333
+ _jsx("div", { className: 'aio-table-toolbar-button', children: _jsx(AISelect, { text: getIcon('mdiEye', 0.8), hideTags: true, options: toggleColumns, caret: false, option: {
334
+ text: (column) => getColumnOptionWrapper(column.id, 'title'),
335
+ checked: (column) => getColumnOptionWrapper(column.id, 'show'),
336
+ onClick: (column) => {
337
+ if (!onChangeColumns) {
338
+ return;
339
+ }
340
+ const columns = columnsRef.current || [];
341
+ onChangeColumns(columns.map((o) => column.id === o.id ? Object.assign(Object.assign({}, o), { show: !getColumnOptionWrapper(o.id, 'show') }) : o));
342
+ },
343
+ close: () => false
344
+ } }) }), !!onAdd && _jsx("div", { className: 'aio-table-toolbar-button', onClick: () => add(), children: getAddText() })] })));
315
345
  };
316
346
  const TableHeader = () => {
317
- let { rootProps, columns } = useProvider();
318
- let { headerAttrs, onRemove, gap = [0, 1] } = rootProps;
347
+ let { rootProps, getColumnOption, tableHook } = useProvider();
348
+ let { headerAttrs, onRemove, gap = [0, 1], columns = [] } = rootProps;
319
349
  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));
350
+ let Titles = columns.map((column, i) => {
351
+ const columnId = getColumnOption('id', tableHook.getCellDetail({ column }));
352
+ return _jsx(TableTitle, { column: column, isLast: i === columns.length - 1, colIndex: i }, columnId);
353
+ });
321
354
  let RemoveTitle = !onRemove ? null : _jsx("div", { className: 'aio-table-remove-title' });
322
355
  return _jsxs("div", Object.assign({}, headerAttrs, { children: [Titles, RemoveTitle] }));
323
356
  };
@@ -334,15 +367,17 @@ const TableRow = (props) => {
334
367
  const { rowDetail } = props;
335
368
  const { row, rowIndex } = rowDetail;
336
369
  const rowId = row._id;
337
- let { remove, rootProps, columns, tableHook, getIcon, getColumnOption } = useProvider();
370
+ let { remove, rootProps, tableHook, getIcon, getColumnOption } = useProvider();
371
+ const { columns = [] } = rootProps;
338
372
  const isOdd = rowIndex % 2 === 0;
339
373
  function getCells() {
340
374
  return columns.map((column) => {
341
375
  if (!getColumnOption('show', tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })))) {
342
376
  return null;
343
377
  }
344
- const key = rowId + ' ' + column._id;
345
378
  const cellDetail = tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column, change: (v) => tableHook.changeCell(tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })), v) }));
379
+ const columnId = getColumnOption('id', cellDetail);
380
+ const key = rowId + ' ' + columnId;
346
381
  const value = getColumnOption('value', cellDetail);
347
382
  const cellValue = tableHook.getCellValue(cellDetail, value);
348
383
  return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue, isOdd: isOdd }, key));
@@ -355,7 +390,7 @@ const TableCell = (props) => {
355
390
  let { cellDetail, cellValue, isOdd } = props;
356
391
  const { row, column } = cellDetail;
357
392
  const { tableHook, rootProps, getColumnOption } = useProvider();
358
- const { columnOption = {} } = rootProps;
393
+ const { columnOption } = rootProps;
359
394
  const type = getColumnOption('type', cellDetail);
360
395
  if (type === 'number') {
361
396
  cellValue = UT.SplitNumber(cellValue);
@@ -364,7 +399,7 @@ const TableCell = (props) => {
364
399
  const after = getColumnOption('after', cellDetail);
365
400
  const subtext = getColumnOption('subtext', cellDetail);
366
401
  const rowId = row._id;
367
- const colId = column._id;
402
+ const colId = getColumnOption('id', cellDetail);
368
403
  const getTemplateValue = () => {
369
404
  const { getValue = {} } = rootProps;
370
405
  if (typeof column.template === 'function') {
@@ -373,7 +408,7 @@ const TableCell = (props) => {
373
408
  if (typeof column.template === 'string' && getValue[column.template]) {
374
409
  return getValue[column.template](cellDetail);
375
410
  }
376
- if (columnOption.template) {
411
+ if ((columnOption || {}).template) {
377
412
  return columnOption.template(cellDetail);
378
413
  }
379
414
  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.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",