@sio-group/ui-datatable 0.1.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 (45) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +429 -0
  3. package/dist/index.cjs +647 -0
  4. package/dist/index.d.cts +83 -0
  5. package/dist/index.d.ts +83 -0
  6. package/dist/index.js +620 -0
  7. package/dist/styles/index.css +154 -0
  8. package/dist/styles/index.css.map +1 -0
  9. package/package.json +44 -0
  10. package/src/assets/scss/index.scss +170 -0
  11. package/src/assets/scss/tokens/_color.scss +19 -0
  12. package/src/assets/scss/tokens/_datatable.scss +10 -0
  13. package/src/components/ActionCell.tsx +88 -0
  14. package/src/components/DataTable.tsx +85 -0
  15. package/src/components/DataTableBody.tsx +34 -0
  16. package/src/components/DataTableControls.tsx +35 -0
  17. package/src/components/DataTableHeader.tsx +59 -0
  18. package/src/components/DefaultSortIcon.tsx +13 -0
  19. package/src/components/TableCell.tsx +17 -0
  20. package/src/components/cell-types/BooleanCell.tsx +29 -0
  21. package/src/components/cell-types/DateCell.tsx +28 -0
  22. package/src/components/cell-types/EmptyCell.tsx +3 -0
  23. package/src/components/cell-types/InlineInputCell.tsx +129 -0
  24. package/src/hooks/useDataTable.ts +113 -0
  25. package/src/index.ts +14 -0
  26. package/src/types/action-cell-props.d.ts +9 -0
  27. package/src/types/action-menu.d.ts +15 -0
  28. package/src/types/column.d.ts +10 -0
  29. package/src/types/data-table-body-props.d.ts +16 -0
  30. package/src/types/data-table-header-props.d.ts +11 -0
  31. package/src/types/data-table-props.d.ts +32 -0
  32. package/src/types/entity.d.ts +4 -0
  33. package/src/types/form-field.d.ts +8 -0
  34. package/src/types/index.ts +11 -0
  35. package/src/types/pagination-meta.d.ts +7 -0
  36. package/src/types/sort-state.d.ts +6 -0
  37. package/src/types/table-cell-props.d.ts +9 -0
  38. package/src/types/use-data-table-props.d.ts +14 -0
  39. package/src/types/use-data-table-return.d.ts +14 -0
  40. package/src/utils/is-pill-value.ts +7 -0
  41. package/src/utils/render-object.tsx +18 -0
  42. package/src/utils/render-value.tsx +89 -0
  43. package/tsconfig.json +17 -0
  44. package/tsup.config.ts +8 -0
  45. package/vitest.config.ts +9 -0
@@ -0,0 +1,89 @@
1
+ import {Column, FormField} from "../types";
2
+ import {EmptyCell} from "../components/cell-types/EmptyCell";
3
+ import {BooleanCell} from "../components/cell-types/BooleanCell";
4
+ import {Link, Pill} from "@sio-group/ui-core";
5
+ import {renderObject} from "./render-object";
6
+ import {DateCell} from "../components/cell-types/DateCell";
7
+ import {isPillValue} from "./is-pill-value";
8
+ import {InlineInputCell} from "../components/cell-types/InlineInputCell";
9
+
10
+ interface RenderValueProps <T extends { id: string | number }> {
11
+ value: T[keyof T];
12
+ column: Column<T>;
13
+ item: T;
14
+ formFields?: FormField[];
15
+ updateData?: (id: (string | number), values: Partial<T>) => void;
16
+ }
17
+
18
+ export const renderValue = <T extends { id: string | number }> ({value, column, item, formFields, updateData}: RenderValueProps<T>) => {
19
+ const formatKey: string | undefined = typeof column.format === 'object' ? column.format.key : undefined;
20
+
21
+ const formField: FormField | undefined = formFields?.find((f: FormField): boolean => f.name === column.name);
22
+ if (formField) {
23
+ return (
24
+ <InlineInputCell
25
+ column={column}
26
+ value={value}
27
+ item={item}
28
+ formField={formField}
29
+ updateData={updateData}
30
+ />
31
+ );
32
+ }
33
+
34
+ if (value === null || value === undefined) return <EmptyCell />;
35
+ if (Array.isArray(value) && !value.length) return <EmptyCell />;
36
+
37
+ if (column.format === "boolean" || column.format === "button") {
38
+ return (
39
+ <BooleanCell
40
+ column={column}
41
+ value={value}
42
+ item={item}
43
+ updateData={updateData}
44
+ />
45
+ );
46
+ }
47
+
48
+ if (column.format === "datetime" || column.format === "date") {
49
+ return (
50
+ <DateCell
51
+ column={column}
52
+ value={value}
53
+ />
54
+ );
55
+ }
56
+
57
+ if (column.format === "pill" && isPillValue(value)) {
58
+ return <Pill status={value.status} label={value.label} />;
59
+ }
60
+
61
+ if (column.format === "email") {
62
+ return <Link to={`mailto:${value}`}>{String(value)}</Link>
63
+ }
64
+
65
+ if (Array.isArray(value)) {
66
+ return (
67
+ <>
68
+ {(value as Array<Record<string, unknown> | string>).map((x, i) => (
69
+ <div key={i}>
70
+ {typeof x === 'object' && x !== null
71
+ ? formatKey
72
+ ? String((x as Record<string, unknown>)[formatKey] ?? '')
73
+ : renderObject(x)
74
+ : String(x)
75
+ }
76
+ </div>
77
+ ))}
78
+ </>
79
+ )
80
+ }
81
+
82
+ if (typeof value === 'object') {
83
+ return formatKey
84
+ ? String((value as Record<string, unknown>)[formatKey] ?? '')
85
+ : renderObject(value as Record<string, unknown>);
86
+ }
87
+
88
+ return String(value)
89
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "composite": false,
5
+ "outDir": "dist",
6
+ "rootDir": "src",
7
+ "jsx": "react-jsx",
8
+ "jsxImportSource": "react"
9
+ },
10
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
11
+ "exclude": ["dist", "node_modules"],
12
+ "references": [
13
+ { "path": "../ui-core" },
14
+ { "path": "../ui-pagination" },
15
+ { "path": "../ui-modal" },
16
+ ]
17
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm", "cjs"],
6
+ dts: true,
7
+ clean: true
8
+ });
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "jsdom",
6
+ globals: true,
7
+ //setupFiles: "./test/setup.ts"
8
+ }
9
+ });