aio-table 9.3.1 → 10.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 +8 -4
  2. package/index.js +123 -112
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -12,7 +12,7 @@ type I_rowDetail<T> = {
12
12
  type I_cellDetail<T> = I_rowDetail<T> & {
13
13
  column: I_table_column<T>;
14
14
  change: (newRow: T) => void;
15
- isDate: boolean;
15
+ date: number[];
16
16
  };
17
17
  export type I_table_paging = UT.I_paging;
18
18
  export type I_table_sort<T> = UT.I_sort<T>;
@@ -22,6 +22,7 @@ export type I_table_filter_saved_item = UT.I_filter_saved_item;
22
22
  export type I_table_column<T> = {
23
23
  title?: any;
24
24
  sort?: true | I_table_sort<T>;
25
+ jalali?: boolean;
25
26
  filterId?: string;
26
27
  search?: boolean;
27
28
  id?: string;
@@ -41,7 +42,10 @@ export type I_table_column<T> = {
41
42
  titleAttrs?: {
42
43
  [attrs: string]: any;
43
44
  };
44
- type?: 'text' | 'number' | 'month' | 'day' | 'hour' | 'minute';
45
+ type?: 'text' | 'number' | 'date';
46
+ show?: boolean;
47
+ pattern?: string;
48
+ data?: any;
45
49
  };
46
50
  export type I_table<T> = {
47
51
  fa?: boolean;
@@ -94,8 +98,8 @@ export type I_table<T> = {
94
98
  filter?: I_table_filter;
95
99
  gap?: [number, number];
96
100
  striped?: [string, string];
97
- visibleColumns?: {
98
- [columnId: string]: boolean | undefined;
101
+ columnOption?: {
102
+ [key in keyof I_table_column<any>]: (p: I_cellDetail<T>) => any;
99
103
  };
100
104
  onChangeColumns?: (v: I_table_column<any>[]) => void;
101
105
  };
package/index.js CHANGED
@@ -33,7 +33,25 @@ const AIOTable = (props) => {
33
33
  const propsRef = useRef(props);
34
34
  propsRef.current = props;
35
35
  const pagingHook = UT.usePaging({ rows: props.value, paging: props.paging, onChange: props.onChangePaging });
36
- const tableHook = useTable(() => propsRef.current, () => props.paging);
36
+ const getColumnOption = (key, cellDetails) => {
37
+ const columnValue = cellDetails.column[key];
38
+ if (columnValue !== undefined) {
39
+ const { getValue = {} } = props;
40
+ if (getValue[columnValue]) {
41
+ return getValue[columnValue](cellDetails);
42
+ }
43
+ return cellDetails.column[key];
44
+ }
45
+ const { columnOption = {} } = props;
46
+ const fn = columnOption[key] || (() => { });
47
+ const res = fn(cellDetails);
48
+ if (res !== undefined) {
49
+ return res;
50
+ }
51
+ const defaults = { show: true, title: '', justify: false, titleAttrs: {} };
52
+ return defaults[key];
53
+ };
54
+ const tableHook = useTable(() => propsRef.current, () => props.paging, getColumnOption);
37
55
  const getIconRef = useRef(new UT.GetSvg());
38
56
  const getIcon = getIconRef.current.getIcon;
39
57
  const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => {
@@ -42,15 +60,17 @@ const AIOTable = (props) => {
42
60
  props.onChangeColumns(newColumns);
43
61
  }
44
62
  setColumns(newColumns);
45
- });
63
+ }, 'aio-table-title', 'aio-table-unit');
46
64
  const getGetValue = (sort, column) => {
47
65
  if (sort.getValue) {
48
66
  return sort.getValue;
49
67
  }
50
68
  return (row) => {
51
- const isDate = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
52
- const cellValue = tableHook.getCellValue({ row, rowIndex: 0, isFirst: false, isLast: false, column, change: () => { }, isDate }, column.value);
53
- if (isDate) {
69
+ const cellDetail = tableHook.getCellDetail({ column, row });
70
+ const type = getColumnOption('type', cellDetail) || 'text';
71
+ const value = getColumnOption('value', cellDetail);
72
+ const cellValue = tableHook.getCellValue(cellDetail, value);
73
+ if (type === 'date') {
54
74
  const DATE = new AIODate();
55
75
  try {
56
76
  return DATE.getTime(cellValue);
@@ -67,37 +87,34 @@ const AIOTable = (props) => {
67
87
  for (let i = 0; i < columns.length; i++) {
68
88
  const column = columns[i];
69
89
  const { _id } = column;
70
- const sort = column.sort === true ? { sortId: _id } : column.sort;
90
+ const cellDetail = tableHook.getCellDetail({ column });
91
+ const sortValue = getColumnOption('sort', cellDetail);
92
+ const sort = sortValue === true ? { sortId: _id } : sortValue;
71
93
  if (!sort) {
72
94
  continue;
73
95
  }
74
96
  let { active = false, dir = 'dec', sortId } = sort;
75
- let sortItem = { dir, title: sort.title || column.title, sortId, active, getValue: getGetValue(sort, column) };
97
+ let sortItem = { dir, title: sort.title || getColumnOption('title', cellDetail), sortId, active, getValue: getGetValue(sort, column) };
76
98
  sorts.push(sortItem);
77
99
  }
78
100
  return sorts;
79
101
  };
80
- const isColumnVisible = (column) => {
81
- const { visibleColumns = {} } = props;
82
- const { filterId } = column;
83
- if (!filterId) {
84
- return true;
85
- }
86
- return !!visibleColumns[filterId];
87
- };
88
102
  const sortHook = UT.useSort({
89
103
  sorts: [],
90
104
  rows: propsRef.current.value,
91
105
  onChangeRows: props.onChange,
92
106
  onChangeSort: props.onChangeSort,
93
107
  });
94
- const isDate = (column) => ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
95
108
  function getColumns() {
96
109
  let { columns = [] } = props;
97
110
  let searchColumns = [], excelColumns = [], filterColumns = [];
98
- let updatedColumns = columns.map((o) => {
99
- let { id = 'aitc' + Math.round(Math.random() * 1000000), filterId, search, excel } = o;
100
- let column = Object.assign(Object.assign({}, o), { _id: id });
111
+ let updatedColumns = columns.map((column) => {
112
+ const cellDetail = tableHook.getCellDetail({ column });
113
+ const filterId = getColumnOption('filterId', cellDetail);
114
+ const search = getColumnOption('search', cellDetail);
115
+ 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 });
101
118
  if (search) {
102
119
  searchColumns.push(column);
103
120
  }
@@ -124,27 +141,6 @@ const AIOTable = (props) => {
124
141
  pagingHook.changePaging(props.paging);
125
142
  }
126
143
  }, [JSON.stringify(props.paging)]);
127
- function getTimeText(column, value) {
128
- if (!value || value === null) {
129
- return '';
130
- }
131
- const t = column.type;
132
- const DATE = new AIODate();
133
- let pattern = '{year}/{month}';
134
- if (t !== 'month') {
135
- pattern += '/{day}';
136
- if (t !== 'day') {
137
- pattern += ' {hour}';
138
- if (t === 'minute') {
139
- pattern += ' : {minute}';
140
- }
141
- else {
142
- pattern += ' : 00';
143
- }
144
- }
145
- }
146
- return DATE.getDateByPattern(value, pattern);
147
- }
148
144
  function add() {
149
145
  return __awaiter(this, void 0, void 0, function* () {
150
146
  if (!props.onAdd) {
@@ -178,11 +174,14 @@ const AIOTable = (props) => {
178
174
  const isLast = rowIndex === props.value.length - 1;
179
175
  let row = props.value[rowIndex], json = {};
180
176
  for (let j = 0; j < excelColumns.length; j++) {
181
- const column = excelColumns[j], { excel } = column;
177
+ const column = excelColumns[j];
178
+ const cellDetail = tableHook.getCellDetail({ row, rowIndex, isFirst, isLast, column });
179
+ const excel = getColumnOption('excel', cellDetail);
182
180
  let key = '';
183
181
  if (excel === true) {
184
- if (typeof column.title === 'string') {
185
- key = column.title;
182
+ const title = getColumnOption('title', cellDetail);
183
+ if (typeof title === 'string') {
184
+ key = title;
186
185
  }
187
186
  else {
188
187
  key = 'untitle';
@@ -194,8 +193,8 @@ const AIOTable = (props) => {
194
193
  else {
195
194
  continue;
196
195
  }
197
- const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
198
- json[key] = tableHook.getCellValue(cellDetail, column.value, '');
196
+ const value = getColumnOption('value', cellDetail);
197
+ json[key] = tableHook.getCellValue(cellDetail, value, '');
199
198
  }
200
199
  list.push(json);
201
200
  }
@@ -215,8 +214,9 @@ const AIOTable = (props) => {
215
214
  let str = '';
216
215
  for (let i = 0; i < searchColumns.length; i++) {
217
216
  let column = searchColumns[i];
218
- const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
219
- let cellValue = tableHook.getCellValue(cellDetail, column.value, '');
217
+ const cellDetail = tableHook.getCellDetail({ row, rowIndex, isFirst, isLast, column });
218
+ const value = getColumnOption('value', cellDetail);
219
+ let cellValue = tableHook.getCellValue(cellDetail, value, '');
220
220
  if (cellValue) {
221
221
  str += cellValue + ' ';
222
222
  }
@@ -244,29 +244,19 @@ const AIOTable = (props) => {
244
244
  props.onSearch(searchValue);
245
245
  }
246
246
  }
247
- const changeCell = (cellDetail, cellNewValue) => {
248
- if (cellNewValue) { }
249
- if (!props.onChange) {
250
- return;
251
- }
252
- const { column } = cellDetail;
253
- if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
254
- return;
255
- }
256
- let row = JSON.parse(JSON.stringify(cellDetail.row));
257
- const rowId = row._id;
258
- eval(`${column.value} = cellNewValue`);
259
- props.onChange(props.value.map((o) => o._id !== rowId ? o : row));
260
- };
261
247
  let ROWS = getRows();
262
248
  const { gap = [0, 1] } = props;
263
249
  let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
264
250
  const context = {
265
- rootProps: props, getTimeText, isDate, columns, excelColumns, filterColumns, changeCell, tableHook, sortHook, ROWS,
266
- getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, isColumnVisible
251
+ rootProps: props, columns, excelColumns, filterColumns, tableHook, sortHook, ROWS,
252
+ getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, getColumnOption
267
253
  };
268
254
  return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter &&
269
- _jsx(UT.Filterbar, { columns: filterColumns, filter: props.filter, fa: props.fa, 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()] }));
255
+ _jsx(UT.Filterbar, { columns: filterColumns, filter: props.filter, fa: props.fa, columnOption: {
256
+ text: (column) => getColumnOption('title', tableHook.getCellDetail({ column })),
257
+ id: (column) => getColumnOption('filterId', tableHook.getCellDetail({ column })),
258
+ type: (column) => getColumnOption('type', tableHook.getCellDetail({ column })) || 'text'
259
+ } }), _jsxs("div", { className: 'aio-table-unit aio-table-scroll', style: { gap: gap[1] }, children: [_jsx(TableHeader, {}), _jsx(TableRows, {})] }), pagingHook.render()] })), popup.render()] }));
270
260
  };
271
261
  export default AIOTable;
272
262
  const TableRows = () => {
@@ -333,8 +323,8 @@ const TableHeader = () => {
333
323
  };
334
324
  const TableTitle = (p) => {
335
325
  const { column, colIndex } = p;
336
- let { tableHook, DragColumns, isColumnVisible } = useProvider();
337
- if (!isColumnVisible(column)) {
326
+ let { tableHook, DragColumns, getColumnOption } = useProvider();
327
+ if (!getColumnOption('show', tableHook.getCellDetail({ column }))) {
338
328
  return null;
339
329
  }
340
330
  const attrs = Object.assign(Object.assign(Object.assign({}, tableHook.getTitleAttrs(column)), DragColumns.getDragAttrs(colIndex)), DragColumns.getDropAttrs(colIndex));
@@ -344,27 +334,17 @@ const TableRow = (props) => {
344
334
  const { rowDetail } = props;
345
335
  const { row, rowIndex } = rowDetail;
346
336
  const rowId = row._id;
347
- let { remove, rootProps, columns, tableHook, getIcon, isDate, isColumnVisible } = useProvider();
337
+ let { remove, rootProps, columns, tableHook, getIcon, getColumnOption } = useProvider();
348
338
  const isOdd = rowIndex % 2 === 0;
349
339
  function getCells() {
350
340
  return columns.map((column) => {
351
- if (!isColumnVisible(column)) {
341
+ if (!getColumnOption('show', tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })))) {
352
342
  return null;
353
343
  }
354
344
  const key = rowId + ' ' + column._id;
355
- const cellDetail = Object.assign(Object.assign({}, rowDetail), { column, change: (cellNewValue) => {
356
- if (cellNewValue) { }
357
- if (!rootProps.onChange) {
358
- return;
359
- }
360
- if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
361
- return;
362
- }
363
- let row = JSON.parse(JSON.stringify(cellDetail.row));
364
- eval(`${column.value} = cellNewValue`);
365
- rootProps.onChange(rootProps.value.map((o) => o._id !== rowId ? o : row));
366
- }, isDate: isDate(column) });
367
- const cellValue = tableHook.getCellValue(cellDetail, column.value);
345
+ const cellDetail = tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column, change: (v) => tableHook.changeCell(tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })), v) }));
346
+ const value = getColumnOption('value', cellDetail);
347
+ const cellValue = tableHook.getCellValue(cellDetail, value);
368
348
  return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue, isOdd: isOdd }, key));
369
349
  });
370
350
  }
@@ -372,24 +352,49 @@ const TableRow = (props) => {
372
352
  return (_jsx(_Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getRowAttrs(props.rowDetail, isOdd), { children: [getCells(), onRemove ? _jsx("button", { className: 'aio-table-remove', onClick: () => remove(row, rowIndex), children: getIcon('mdiClose', 0.8) }) : null] }), rowId) }));
373
353
  };
374
354
  const TableCell = (props) => {
375
- const { cellDetail, cellValue, isOdd } = props;
355
+ let { cellDetail, cellValue, isOdd } = props;
376
356
  const { row, column } = cellDetail;
377
- const { tableHook, getTimeText, rootProps } = useProvider();
378
- const { template, before, after, subtext } = column;
357
+ const { tableHook, rootProps, getColumnOption } = useProvider();
358
+ const { columnOption = {} } = rootProps;
359
+ const type = getColumnOption('type', cellDetail);
360
+ if (type === 'number') {
361
+ cellValue = UT.SplitNumber(cellValue);
362
+ }
363
+ const before = getColumnOption('before', cellDetail);
364
+ const after = getColumnOption('after', cellDetail);
365
+ const subtext = getColumnOption('subtext', cellDetail);
379
366
  const rowId = row._id;
380
367
  const colId = column._id;
381
- const isTime = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
382
- const templateValue = isTime ? getTimeText(cellValue) : tableHook.getCellValue(cellDetail, template);
368
+ const getTemplateValue = () => {
369
+ const { getValue = {} } = rootProps;
370
+ if (typeof column.template === 'function') {
371
+ return column.template(cellDetail);
372
+ }
373
+ if (typeof column.template === 'string' && getValue[column.template]) {
374
+ return getValue[column.template](cellDetail);
375
+ }
376
+ if (columnOption.template) {
377
+ return columnOption.template(cellDetail);
378
+ }
379
+ const pattern = getColumnOption('pattern', cellDetail);
380
+ if (pattern) {
381
+ if (type === 'date') {
382
+ return new AIODate().getDateByPattern(cellValue, pattern);
383
+ }
384
+ else {
385
+ return pattern.replace('{value}', cellValue);
386
+ }
387
+ }
388
+ };
389
+ const templateValue = getTemplateValue();
383
390
  const beforeValue = tableHook.getCellValue(cellDetail, before, undefined);
384
391
  const afterValue = tableHook.getCellValue(cellDetail, after, undefined);
385
392
  const subtextValue = tableHook.getCellValue(cellDetail, subtext, undefined);
386
393
  return (_jsx(Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getCellAttrs(props.cellDetail, props.cellValue, isOdd, rootProps.striped), { 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));
387
394
  };
388
- const useTable = (getProps, getPaging) => {
389
- const DragRows = UT.useDrag((dragData, dropData, reOrder) => {
390
- const { onSwap, onChange } = getProps();
391
- const { dragIndex } = dragData;
392
- const { dropIndex, rows } = dropData;
395
+ const useTable = (getProps, getPaging, getColumnOption) => {
396
+ const DragRows = UT.useDrag((dragIndex, dropIndex, reOrder) => {
397
+ const { onSwap, onChange, value: rows } = getProps();
393
398
  const newRows = reOrder(rows, dragIndex, dropIndex);
394
399
  const from = rows[dragIndex];
395
400
  const to = rows[dropIndex];
@@ -399,7 +404,7 @@ const useTable = (getProps, getPaging) => {
399
404
  else if (onChange) {
400
405
  onChange(newRows);
401
406
  }
402
- });
407
+ }, 'aio-table-row', 'aio-table-rows');
403
408
  const getCellValue = (cellDetail, cellValue, def) => {
404
409
  const { getValue = {} } = getProps();
405
410
  const paging = getPaging();
@@ -433,24 +438,28 @@ const useTable = (getProps, getPaging) => {
433
438
  }
434
439
  return cellValue === undefined ? def : cellValue;
435
440
  };
436
- const getColValue = (column, field, def) => {
437
- const colValue = column[field];
438
- let type = typeof colValue;
439
- let result;
440
- if (type === 'function') {
441
- result = colValue(column);
442
- }
443
- else {
444
- result = colValue;
441
+ function changeCell(cellDetail, newRow) {
442
+ if (newRow) { }
443
+ const { onChange, value: propsValue } = getProps();
444
+ if (!onChange) {
445
+ return;
445
446
  }
446
- return result === undefined ? def : result;
447
- };
447
+ const rowId = cellDetail.row._id;
448
+ onChange(propsValue.map((o) => o._id !== rowId ? o : newRow));
449
+ }
450
+ function getCellDetail(v) {
451
+ const res = Object.assign({ column: {}, row: {}, rowIndex: 0, isFirst: false, isLast: false, date: [], change: () => { } }, v);
452
+ const value = getColumnOption('value', res);
453
+ const jalali = getColumnOption('jalali', res);
454
+ const date = res.column.type === 'date' ? new AIODate().convertToArray(getCellValue(res, value), jalali) : [];
455
+ return Object.assign(Object.assign(Object.assign({}, res), { date }), v);
456
+ }
448
457
  const getCellAttrs = (cellDetail, cellValue, isOdd, striped) => {
449
458
  const { column } = cellDetail;
450
459
  const attrs = getCellValue(cellDetail, column.attrs, {});
451
- const justify = getColValue(column, 'justify', false);
452
- const width = getColValue(column, 'width');
453
- const minWidth = getColValue(column, 'minWidth');
460
+ const justify = getColumnOption('justify', cellDetail);
461
+ const width = getColumnOption('width', cellDetail);
462
+ const minWidth = getColumnOption('minWidth', cellDetail);
454
463
  const className = `aio-table-cell` + (justify ? ` aio-table-cell-justify` : '');
455
464
  const style = { width, minWidth, flex: width ? undefined : 1 };
456
465
  if (striped) {
@@ -459,13 +468,15 @@ const useTable = (getProps, getPaging) => {
459
468
  return UT.AddToAttrs(attrs, { className: [className, isOdd ? 'aio-table-cell-odd' : 'aio-table-cell-even'], style, attrs: { title: typeof cellValue === 'string' ? cellValue : undefined } });
460
469
  };
461
470
  const getTitleAttrs = (column) => {
462
- const attrs = getColValue(column, 'titleAttrs', {});
463
- const justify = getColValue(column, 'justify', false);
464
- const width = getColValue(column, 'width');
465
- const minWidth = getColValue(column, 'minWidth');
471
+ const cellDetail = getCellDetail({ column });
472
+ const attrs = getColumnOption('titleAttrs', cellDetail);
473
+ const justify = getColumnOption('justify', cellDetail);
474
+ const width = getColumnOption('width', cellDetail);
475
+ const minWidth = getColumnOption('minWidth', cellDetail);
466
476
  const className = `aio-table-title` + (justify ? ` aio-table-title-justify` : '');
467
477
  const style = { width, minWidth, flex: width ? undefined : 1 };
468
- return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof column.title === 'string' ? column.title : undefined } });
478
+ const title = getColumnOption('title', cellDetail);
479
+ return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof title === 'string' ? title : undefined } });
469
480
  };
470
481
  const getRowAttrs = (rowDetail, isOdd) => {
471
482
  const { rowOption = {}, onSwap, value, gap = [0, 1] } = getProps();
@@ -477,5 +488,5 @@ const useTable = (getProps, getPaging) => {
477
488
  }
478
489
  return obj;
479
490
  };
480
- return { getCellValue, getColValue, getCellAttrs, getTitleAttrs, getRowAttrs };
491
+ return { getCellValue, getCellAttrs, getTitleAttrs, getRowAttrs, changeCell, getCellDetail };
481
492
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-table",
3
- "version": "9.3.1",
3
+ "version": "10.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",