@dxos/react-ui-table 0.4.10-main.c32f430 → 0.4.10-main.c42bfdb

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 (59) hide show
  1. package/dist/lib/browser/index.mjs +435 -283
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/{ColumnMenu.d.ts → ColumnMenu/ColumnMenu.d.ts} +1 -1
  5. package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -0
  6. package/dist/types/src/components/{ColumnMenu.stories.d.ts → ColumnMenu/ColumnMenu.stories.d.ts} +1 -1
  7. package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -0
  8. package/dist/types/src/components/{ColumnSettingsForm.d.ts → ColumnMenu/ColumnSettingsForm.d.ts} +5 -4
  9. package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -0
  10. package/dist/types/src/components/ColumnMenu/index.d.ts +3 -0
  11. package/dist/types/src/components/ColumnMenu/index.d.ts.map +1 -0
  12. package/dist/types/src/components/index.d.ts +1 -1
  13. package/dist/types/src/components/index.d.ts.map +1 -1
  14. package/dist/types/src/helpers.d.ts +4 -0
  15. package/dist/types/src/helpers.d.ts.map +1 -1
  16. package/dist/types/src/hooks/index.d.ts +3 -0
  17. package/dist/types/src/hooks/index.d.ts.map +1 -0
  18. package/dist/types/src/hooks/useColumnSorting.d.ts.map +1 -0
  19. package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -0
  20. package/dist/types/src/index.d.ts +2 -1
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/schema/SchemaToTable.stories.d.ts +11 -0
  23. package/dist/types/src/schema/SchemaToTable.stories.d.ts.map +1 -0
  24. package/dist/types/src/schema/index.d.ts +3 -0
  25. package/dist/types/src/schema/index.d.ts.map +1 -0
  26. package/dist/types/src/schema/schema.d.ts +5 -0
  27. package/dist/types/src/schema/schema.d.ts.map +1 -0
  28. package/dist/types/src/schema/schema.test.d.ts +2 -0
  29. package/dist/types/src/schema/schema.test.d.ts.map +1 -0
  30. package/dist/types/src/schema/schemaToColumns.d.ts +4 -0
  31. package/dist/types/src/schema/schemaToColumns.d.ts.map +1 -0
  32. package/dist/types/src/{schema.d.ts → schema/types.d.ts} +1 -1
  33. package/dist/types/src/schema/types.d.ts.map +1 -0
  34. package/package.json +18 -15
  35. package/src/components/{ColumnMenu.stories.tsx → ColumnMenu/ColumnMenu.stories.tsx} +1 -1
  36. package/src/components/{ColumnMenu.tsx → ColumnMenu/ColumnMenu.tsx} +2 -2
  37. package/src/components/{ColumnSettingsForm.tsx → ColumnMenu/ColumnSettingsForm.tsx} +14 -12
  38. package/src/components/ColumnMenu/index.ts +6 -0
  39. package/src/components/Table/Table.tsx +1 -1
  40. package/src/components/index.ts +1 -1
  41. package/src/helpers.tsx +22 -0
  42. package/src/hooks/index.ts +6 -0
  43. package/src/index.ts +3 -1
  44. package/src/schema/SchemaToTable.stories.tsx +72 -0
  45. package/src/schema/index.ts +6 -0
  46. package/src/schema/schema.test.ts +148 -0
  47. package/src/schema/schema.ts +93 -0
  48. package/src/schema/schemaToColumns.ts +47 -0
  49. package/dist/types/src/components/ColumnMenu.d.ts.map +0 -1
  50. package/dist/types/src/components/ColumnMenu.stories.d.ts.map +0 -1
  51. package/dist/types/src/components/ColumnSettingsForm.d.ts.map +0 -1
  52. package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +0 -1
  53. package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +0 -1
  54. package/dist/types/src/schema.d.ts.map +0 -1
  55. /package/dist/types/src/{components/hooks → hooks}/useColumnSorting.d.ts +0 -0
  56. /package/dist/types/src/{components/hooks → hooks}/usePinLastRow.d.ts +0 -0
  57. /package/src/{components/hooks → hooks}/useColumnSorting.ts +0 -0
  58. /package/src/{components/hooks → hooks}/usePinLastRow.ts +0 -0
  59. /package/src/{schema.ts → schema/types.ts} +0 -0
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './schemaToColumns';
6
+ export * from './types';
@@ -0,0 +1,148 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import * as S from '@effect/schema/Schema';
6
+ import { expect } from 'chai';
7
+
8
+ import { describe, test } from '@dxos/test';
9
+
10
+ import { classifySchemaProperties } from './schema';
11
+
12
+ describe('schema->column-type', () => {
13
+ test('basic', () => {
14
+ const simpleSchema = S.struct({
15
+ field1: S.string,
16
+ field2: S.number,
17
+ });
18
+
19
+ const columns = classifySchemaProperties(simpleSchema);
20
+
21
+ expect(columns).to.deep.equal([
22
+ ['field1', 'string'],
23
+ ['field2', 'number'],
24
+ ]);
25
+ });
26
+
27
+ test('optional properties', () => {
28
+ const simpleSchema = S.struct({
29
+ field1: S.optional(S.string),
30
+ field2: S.optional(S.number),
31
+ });
32
+
33
+ const columns = classifySchemaProperties(simpleSchema);
34
+
35
+ expect(columns).to.deep.equal([
36
+ ['field1', 'string'],
37
+ ['field2', 'number'],
38
+ ]);
39
+ });
40
+
41
+ test('dates', () => {
42
+ const simpleSchema = S.struct({
43
+ field1: S.Date,
44
+ field2: S.optional(S.Date),
45
+ });
46
+
47
+ const columns = classifySchemaProperties(simpleSchema);
48
+
49
+ expect(columns).to.deep.equal([
50
+ ['field1', 'date'],
51
+ ['field2', 'date'],
52
+ ]);
53
+ });
54
+
55
+ test('Piped validators', () => {
56
+ const simpleSchema = S.struct({
57
+ name: S.optional(S.string.pipe(S.nonEmpty(), S.maxLength(10))),
58
+ age: S.number.pipe(S.negative()),
59
+ });
60
+
61
+ const columns = classifySchemaProperties(simpleSchema);
62
+
63
+ expect(columns).to.deep.equal([
64
+ ['name', 'string'],
65
+ ['age', 'number'],
66
+ ]);
67
+ });
68
+
69
+ test('nested schema', () => {
70
+ const simpleSchema = S.struct({
71
+ name: S.string,
72
+ nested: S.struct({
73
+ age: S.number,
74
+ }),
75
+ someUnion: S.union(S.string, S.number),
76
+ });
77
+
78
+ const columns = classifySchemaProperties(simpleSchema);
79
+
80
+ expect(columns).to.deep.equal([
81
+ ['name', 'string'],
82
+ ['nested.age', 'number'],
83
+ ['someUnion', 'display'],
84
+ ]);
85
+ });
86
+
87
+ test('deeply nested schema', () => {
88
+ const simpleSchema = S.struct({
89
+ name: S.string,
90
+ nested: S.struct({
91
+ age: S.number,
92
+ deeplyNested: S.struct({
93
+ height: S.number,
94
+ }),
95
+ }),
96
+ });
97
+
98
+ const columns = classifySchemaProperties(simpleSchema);
99
+
100
+ expect(columns).to.deep.equal([
101
+ ['name', 'string'],
102
+ ['nested.age', 'number'],
103
+ ['nested.deeplyNested.height', 'number'],
104
+ ]);
105
+ });
106
+
107
+ test('even more deeply nested schema with optionals', () => {
108
+ const simpleSchema = S.struct({
109
+ name: S.string,
110
+ nested: S.struct({
111
+ age: S.number,
112
+ deeplyNested: S.optional(
113
+ S.struct({
114
+ height: S.optional(S.number),
115
+ }),
116
+ ),
117
+ }),
118
+ });
119
+
120
+ const columns = classifySchemaProperties(simpleSchema);
121
+
122
+ expect(columns).to.deep.equal([
123
+ ['name', 'string'],
124
+ ['nested.age', 'number'],
125
+ ['nested.deeplyNested.height', 'number'],
126
+ ]);
127
+ });
128
+
129
+ test('multiple-nested schemata as sibblings', () => {
130
+ const simpleSchema = S.struct({
131
+ name: S.string,
132
+ nested: S.struct({
133
+ age: S.number,
134
+ }),
135
+ nested2: S.struct({
136
+ height: S.number,
137
+ }),
138
+ });
139
+
140
+ const columns = classifySchemaProperties(simpleSchema);
141
+
142
+ expect(columns).to.deep.equal([
143
+ ['name', 'string'],
144
+ ['nested.age', 'number'],
145
+ ['nested2.height', 'number'],
146
+ ]);
147
+ });
148
+ });
@@ -0,0 +1,93 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { AST } from '@effect/schema';
6
+ import type * as S from '@effect/schema/Schema';
7
+
8
+ import { type ColumnType } from './types';
9
+
10
+ export type ClassifiedColumnType = ColumnType | 'display';
11
+
12
+ const isOptionalUnion = (prop: AST.PropertySignature) => AST.isUnion(prop.type) && prop.isOptional;
13
+
14
+ const unwrapOptionProperty = (prop: AST.PropertySignature) => {
15
+ if (!isOptionalUnion(prop)) {
16
+ throw new Error('Not an optional property');
17
+ }
18
+
19
+ if (!AST.isUnion(prop.type)) {
20
+ throw new Error('Not a union type');
21
+ }
22
+
23
+ const [type, _undefinedCase] = prop.type.types;
24
+
25
+ return type;
26
+ };
27
+
28
+ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
29
+ if (AST.isStringKeyword(type)) {
30
+ return 'string';
31
+ } else if (AST.isNumberKeyword(type)) {
32
+ return 'number';
33
+ } else if (AST.isBooleanKeyword(type)) {
34
+ return 'boolean';
35
+ }
36
+
37
+ if (AST.isRefinement(type)) {
38
+ return typeToColumn(type.from);
39
+ }
40
+
41
+ // TODO(zan): How should we be thinking about transformations?
42
+ // - Which of these are we storing in the database?
43
+ // - For types that aren't the 'DateFromString' transformation, should we be using the 'from' or 'to' type?
44
+
45
+ if (AST.isTransform(type)) {
46
+ const identifier = AST.getIdentifierAnnotation(type);
47
+ if (identifier._tag === 'Some') {
48
+ if (identifier.value === 'DateFromString') {
49
+ return 'date';
50
+ }
51
+ }
52
+ }
53
+
54
+ return 'display';
55
+ };
56
+
57
+ const propertyToColumn = (property: AST.PropertySignature): ClassifiedColumnType => {
58
+ let type = property.type;
59
+
60
+ if (property.isOptional) {
61
+ type = unwrapOptionProperty(property);
62
+ }
63
+
64
+ return typeToColumn(type);
65
+ };
66
+
67
+ const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
68
+
69
+ export const classifySchemaProperties = (schema: S.Schema<any, any>) => {
70
+ const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
71
+ const properties = AST.getPropertySignatures(node);
72
+ properties.forEach((prop) => {
73
+ const propName = prop.name.toString();
74
+
75
+ if (prop.isOptional) {
76
+ const unwrappedAst = unwrapOptionProperty(prop);
77
+ if (isStruct(unwrappedAst)) {
78
+ return recurse(unwrappedAst, [...path, propName], acc);
79
+ }
80
+ }
81
+
82
+ if (isStruct(prop.type)) {
83
+ recurse(prop.type, [...path, propName], acc);
84
+ } else {
85
+ acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
86
+ }
87
+ });
88
+
89
+ return acc;
90
+ };
91
+
92
+ return recurse(schema.ast, [], []);
93
+ };
@@ -0,0 +1,47 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type * as S from '@effect/schema/Schema';
6
+ import { type ColumnDef } from '@tanstack/react-table';
7
+
8
+ import { classifySchemaProperties } from './schema';
9
+ import { createColumnBuilder } from '../helpers';
10
+
11
+ export const schemaToColumnDefs = <T>(schema: S.Schema<T, any>): ColumnDef<T, any>[] => {
12
+ const { helper, builder } = createColumnBuilder<T>();
13
+
14
+ const classified = classifySchemaProperties(schema);
15
+ return classified.map(([name, type]) => {
16
+ const propertyKey = name.toString();
17
+
18
+ let column: Partial<ColumnDef<any, any>> | undefined;
19
+ switch (type) {
20
+ case 'string': {
21
+ column = builder.string({ label: propertyKey });
22
+ break;
23
+ }
24
+ case 'number': {
25
+ column = builder.number({ label: propertyKey });
26
+ break;
27
+ }
28
+ case 'boolean': {
29
+ column = builder.switch({ label: propertyKey });
30
+ break;
31
+ }
32
+ case 'date': {
33
+ column = builder.date({ label: propertyKey });
34
+ break;
35
+ }
36
+ case 'display': {
37
+ column = builder.display({ label: propertyKey, id: propertyKey });
38
+ break;
39
+ }
40
+ default: {
41
+ throw new Error(`Unhandled column type: ${type}`);
42
+ }
43
+ }
44
+
45
+ return helper.accessor(propertyKey as any, column);
46
+ });
47
+ };
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnMenu.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnMenu.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAQ7F,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAE5D,MAAM,MAAM,eAAe,CAAC,KAAK,SAAS,OAAO,EAAE,MAAM,IAAI;IAC3D,OAAO,EAAE,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACtC,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,KAAK,IAAI,CAAC;IACrD,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,UAAU,sGAqFtB,CAAC;AAEF,eAAO,MAAM,mBAAmB;aAOe,MAAM,IAAI;iBAkBxD,CAAC;AAEF,eAAO,MAAM,aAAa;eAIb,aAAa,GAAG,KAAK;iBACnB,GAAG,KAAK,IAAI;wBAsB1B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnMenu.stories.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnMenu.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,YAAY,CAAC;AAQpB,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;;;;;;AAE7C,wBAIE;AAEF,eAAO,MAAM,OAAO;;;;;;;2CAUa,OAAO;;;;;;;yBAY5B,GAAG;;;;CASd,CAAC;AAEF,eAAO,MAAM,YAAY;;CA2CxB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnSettingsForm.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnSettingsForm.tsx"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAmB,MAAM,WAAW,CAAC;AAS7E,eAAO,MAAM,kBAAkB;YAQrB,WAAW;cACT,QAAQ;eACP,QAAQ,EAAE;qBACJ,MAAM,UAAU,WAAW,KAAK,IAAI;qBACpC,MAAM,KAAK,IAAI;aACvB,MAAM,IAAI;iBA4KpB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"useColumnSorting.d.ts","sourceRoot":"","sources":["../../../../../src/components/hooks/useColumnSorting.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGxE,eAAO,MAAM,gBAAgB,WAAY,OAAO,GAAG,CAAC;;;8BAoCjB,aAAa;;;CAU/C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"usePinLastRow.d.ts","sourceRoot":"","sources":["../../../../../src/components/hooks/usePinLastRow.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAGnD,eAAO,MAAM,aAAa,eACZ,OAAO,GAAG,SAAS,SACxB,MAAM,GAAG,CAAC,QACX,GAAG,qBACU,MAAM,OAAO,GAAG,IAAI,SA+BxC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/schema.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,EAAE,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,WAAW,GAAG;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IAGf,MAAM,CAAC,EAAE,MAAM,CAAC;IAGhB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IAGjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC"}
File without changes