aio-table 9.3.0 → 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 +11 -3
  2. package/index.js +133 -103
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ReactNode } from "react";
1
+ import { type ReactNode } from "react";
2
2
  import * as UT from 'aio-utils';
3
3
  import "./index.css";
4
4
  type I_rowOption<T, R> = (p: I_rowDetail<T>) => R;
@@ -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,6 +98,10 @@ export type I_table<T> = {
94
98
  filter?: I_table_filter;
95
99
  gap?: [number, number];
96
100
  striped?: [string, string];
101
+ columnOption?: {
102
+ [key in keyof I_table_column<any>]: (p: I_cellDetail<T>) => any;
103
+ };
104
+ onChangeColumns?: (v: I_table_column<any>[]) => void;
97
105
  };
98
106
  declare const AIOTable: <T>(props: I_table<T>) => import("react/jsx-runtime").JSX.Element;
99
107
  export default AIOTable;
package/index.js CHANGED
@@ -33,18 +33,44 @@ 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
- const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => setColumns(reOrder(columns, dragIndex, dropIndex)));
57
+ const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => {
58
+ const newColumns = reOrder(columns, dragIndex, dropIndex);
59
+ if (props.onChangeColumns) {
60
+ props.onChangeColumns(newColumns);
61
+ }
62
+ setColumns(newColumns);
63
+ }, 'aio-table-title', 'aio-table-unit');
40
64
  const getGetValue = (sort, column) => {
41
65
  if (sort.getValue) {
42
66
  return sort.getValue;
43
67
  }
44
68
  return (row) => {
45
- const isDate = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
46
- const cellValue = tableHook.getCellValue({ row, rowIndex: 0, isFirst: false, isLast: false, column, change: () => { }, isDate }, column.value);
47
- 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') {
48
74
  const DATE = new AIODate();
49
75
  try {
50
76
  return DATE.getTime(cellValue);
@@ -61,12 +87,14 @@ const AIOTable = (props) => {
61
87
  for (let i = 0; i < columns.length; i++) {
62
88
  const column = columns[i];
63
89
  const { _id } = column;
64
- 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;
65
93
  if (!sort) {
66
94
  continue;
67
95
  }
68
96
  let { active = false, dir = 'dec', sortId } = sort;
69
- 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) };
70
98
  sorts.push(sortItem);
71
99
  }
72
100
  return sorts;
@@ -77,13 +105,16 @@ const AIOTable = (props) => {
77
105
  onChangeRows: props.onChange,
78
106
  onChangeSort: props.onChangeSort,
79
107
  });
80
- const isDate = (column) => ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
81
108
  function getColumns() {
82
109
  let { columns = [] } = props;
83
110
  let searchColumns = [], excelColumns = [], filterColumns = [];
84
- let updatedColumns = columns.map((o) => {
85
- let { id = 'aitc' + Math.round(Math.random() * 1000000), filterId, search, excel } = o;
86
- 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 });
87
118
  if (search) {
88
119
  searchColumns.push(column);
89
120
  }
@@ -107,31 +138,9 @@ const AIOTable = (props) => {
107
138
  }, []);
108
139
  useEffect(() => {
109
140
  if (props.paging) {
110
- debugger;
111
141
  pagingHook.changePaging(props.paging);
112
142
  }
113
143
  }, [JSON.stringify(props.paging)]);
114
- function getTimeText(column, value) {
115
- if (!value || value === null) {
116
- return '';
117
- }
118
- const t = column.type;
119
- const DATE = new AIODate();
120
- let pattern = '{year}/{month}';
121
- if (t !== 'month') {
122
- pattern += '/{day}';
123
- if (t !== 'day') {
124
- pattern += ' {hour}';
125
- if (t === 'minute') {
126
- pattern += ' : {minute}';
127
- }
128
- else {
129
- pattern += ' : 00';
130
- }
131
- }
132
- }
133
- return DATE.getDateByPattern(value, pattern);
134
- }
135
144
  function add() {
136
145
  return __awaiter(this, void 0, void 0, function* () {
137
146
  if (!props.onAdd) {
@@ -165,11 +174,14 @@ const AIOTable = (props) => {
165
174
  const isLast = rowIndex === props.value.length - 1;
166
175
  let row = props.value[rowIndex], json = {};
167
176
  for (let j = 0; j < excelColumns.length; j++) {
168
- 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);
169
180
  let key = '';
170
181
  if (excel === true) {
171
- if (typeof column.title === 'string') {
172
- key = column.title;
182
+ const title = getColumnOption('title', cellDetail);
183
+ if (typeof title === 'string') {
184
+ key = title;
173
185
  }
174
186
  else {
175
187
  key = 'untitle';
@@ -181,8 +193,8 @@ const AIOTable = (props) => {
181
193
  else {
182
194
  continue;
183
195
  }
184
- const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
185
- json[key] = tableHook.getCellValue(cellDetail, column.value, '');
196
+ const value = getColumnOption('value', cellDetail);
197
+ json[key] = tableHook.getCellValue(cellDetail, value, '');
186
198
  }
187
199
  list.push(json);
188
200
  }
@@ -202,8 +214,9 @@ const AIOTable = (props) => {
202
214
  let str = '';
203
215
  for (let i = 0; i < searchColumns.length; i++) {
204
216
  let column = searchColumns[i];
205
- const cellDetail = { row, rowIndex, isFirst, isLast, column, change: () => { }, isDate: isDate(column) };
206
- 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, '');
207
220
  if (cellValue) {
208
221
  str += cellValue + ' ';
209
222
  }
@@ -231,29 +244,19 @@ const AIOTable = (props) => {
231
244
  props.onSearch(searchValue);
232
245
  }
233
246
  }
234
- const changeCell = (cellDetail, cellNewValue) => {
235
- if (cellNewValue) { }
236
- if (!props.onChange) {
237
- return;
238
- }
239
- const { column } = cellDetail;
240
- if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
241
- return;
242
- }
243
- let row = JSON.parse(JSON.stringify(cellDetail.row));
244
- const rowId = row._id;
245
- eval(`${column.value} = cellNewValue`);
246
- props.onChange(props.value.map((o) => o._id !== rowId ? o : row));
247
- };
248
247
  let ROWS = getRows();
249
248
  const { gap = [0, 1] } = props;
250
249
  let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
251
250
  const context = {
252
- rootProps: props, getTimeText, isDate, columns, excelColumns, filterColumns, changeCell, tableHook, sortHook, ROWS,
253
- getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup,
251
+ rootProps: props, columns, excelColumns, filterColumns, tableHook, sortHook, ROWS,
252
+ getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, getColumnOption
254
253
  };
255
254
  return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter &&
256
- _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()] }));
257
260
  };
258
261
  export default AIOTable;
259
262
  const TableRows = () => {
@@ -320,7 +323,10 @@ const TableHeader = () => {
320
323
  };
321
324
  const TableTitle = (p) => {
322
325
  const { column, colIndex } = p;
323
- let { tableHook, DragColumns } = useProvider();
326
+ let { tableHook, DragColumns, getColumnOption } = useProvider();
327
+ if (!getColumnOption('show', tableHook.getCellDetail({ column }))) {
328
+ return null;
329
+ }
324
330
  const attrs = Object.assign(Object.assign(Object.assign({}, tableHook.getTitleAttrs(column)), DragColumns.getDragAttrs(colIndex)), DragColumns.getDropAttrs(colIndex));
325
331
  return _jsx("div", Object.assign({}, attrs, { children: attrs.title }));
326
332
  };
@@ -328,24 +334,17 @@ const TableRow = (props) => {
328
334
  const { rowDetail } = props;
329
335
  const { row, rowIndex } = rowDetail;
330
336
  const rowId = row._id;
331
- let { remove, rootProps, columns, tableHook, getIcon, isDate } = useProvider();
337
+ let { remove, rootProps, columns, tableHook, getIcon, getColumnOption } = useProvider();
332
338
  const isOdd = rowIndex % 2 === 0;
333
339
  function getCells() {
334
340
  return columns.map((column) => {
341
+ if (!getColumnOption('show', tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })))) {
342
+ return null;
343
+ }
335
344
  const key = rowId + ' ' + column._id;
336
- const cellDetail = Object.assign(Object.assign({}, rowDetail), { column, change: (cellNewValue) => {
337
- if (cellNewValue) { }
338
- if (!rootProps.onChange) {
339
- return;
340
- }
341
- if (typeof column.value !== 'string' || column.value.indexOf('row.') !== 0) {
342
- return;
343
- }
344
- let row = JSON.parse(JSON.stringify(cellDetail.row));
345
- eval(`${column.value} = cellNewValue`);
346
- rootProps.onChange(rootProps.value.map((o) => o._id !== rowId ? o : row));
347
- }, isDate: isDate(column) });
348
- 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);
349
348
  return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue, isOdd: isOdd }, key));
350
349
  });
351
350
  }
@@ -353,24 +352,49 @@ const TableRow = (props) => {
353
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) }));
354
353
  };
355
354
  const TableCell = (props) => {
356
- const { cellDetail, cellValue, isOdd } = props;
355
+ let { cellDetail, cellValue, isOdd } = props;
357
356
  const { row, column } = cellDetail;
358
- const { tableHook, getTimeText, rootProps } = useProvider();
359
- 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);
360
366
  const rowId = row._id;
361
367
  const colId = column._id;
362
- const isTime = ['month', 'day', 'hour', 'minute'].indexOf(column.type || 'text') !== -1;
363
- 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();
364
390
  const beforeValue = tableHook.getCellValue(cellDetail, before, undefined);
365
391
  const afterValue = tableHook.getCellValue(cellDetail, after, undefined);
366
392
  const subtextValue = tableHook.getCellValue(cellDetail, subtext, undefined);
367
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));
368
394
  };
369
- const useTable = (getProps, getPaging) => {
370
- const DragRows = UT.useDrag((dragData, dropData, reOrder) => {
371
- const { onSwap, onChange } = getProps();
372
- const { dragIndex } = dragData;
373
- 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();
374
398
  const newRows = reOrder(rows, dragIndex, dropIndex);
375
399
  const from = rows[dragIndex];
376
400
  const to = rows[dropIndex];
@@ -380,7 +404,7 @@ const useTable = (getProps, getPaging) => {
380
404
  else if (onChange) {
381
405
  onChange(newRows);
382
406
  }
383
- });
407
+ }, 'aio-table-row', 'aio-table-rows');
384
408
  const getCellValue = (cellDetail, cellValue, def) => {
385
409
  const { getValue = {} } = getProps();
386
410
  const paging = getPaging();
@@ -414,24 +438,28 @@ const useTable = (getProps, getPaging) => {
414
438
  }
415
439
  return cellValue === undefined ? def : cellValue;
416
440
  };
417
- const getColValue = (column, field, def) => {
418
- const colValue = column[field];
419
- let type = typeof colValue;
420
- let result;
421
- if (type === 'function') {
422
- result = colValue(column);
423
- }
424
- else {
425
- result = colValue;
441
+ function changeCell(cellDetail, newRow) {
442
+ if (newRow) { }
443
+ const { onChange, value: propsValue } = getProps();
444
+ if (!onChange) {
445
+ return;
426
446
  }
427
- return result === undefined ? def : result;
428
- };
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
+ }
429
457
  const getCellAttrs = (cellDetail, cellValue, isOdd, striped) => {
430
458
  const { column } = cellDetail;
431
459
  const attrs = getCellValue(cellDetail, column.attrs, {});
432
- const justify = getColValue(column, 'justify', false);
433
- const width = getColValue(column, 'width');
434
- const minWidth = getColValue(column, 'minWidth');
460
+ const justify = getColumnOption('justify', cellDetail);
461
+ const width = getColumnOption('width', cellDetail);
462
+ const minWidth = getColumnOption('minWidth', cellDetail);
435
463
  const className = `aio-table-cell` + (justify ? ` aio-table-cell-justify` : '');
436
464
  const style = { width, minWidth, flex: width ? undefined : 1 };
437
465
  if (striped) {
@@ -440,13 +468,15 @@ const useTable = (getProps, getPaging) => {
440
468
  return UT.AddToAttrs(attrs, { className: [className, isOdd ? 'aio-table-cell-odd' : 'aio-table-cell-even'], style, attrs: { title: typeof cellValue === 'string' ? cellValue : undefined } });
441
469
  };
442
470
  const getTitleAttrs = (column) => {
443
- const attrs = getColValue(column, 'titleAttrs', {});
444
- const justify = getColValue(column, 'justify', false);
445
- const width = getColValue(column, 'width');
446
- 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);
447
476
  const className = `aio-table-title` + (justify ? ` aio-table-title-justify` : '');
448
477
  const style = { width, minWidth, flex: width ? undefined : 1 };
449
- 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 } });
450
480
  };
451
481
  const getRowAttrs = (rowDetail, isOdd) => {
452
482
  const { rowOption = {}, onSwap, value, gap = [0, 1] } = getProps();
@@ -458,5 +488,5 @@ const useTable = (getProps, getPaging) => {
458
488
  }
459
489
  return obj;
460
490
  };
461
- return { getCellValue, getColValue, getCellAttrs, getTitleAttrs, getRowAttrs };
491
+ return { getCellValue, getCellAttrs, getTitleAttrs, getRowAttrs, changeCell, getCellDetail };
462
492
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-table",
3
- "version": "9.3.0",
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",