@dxos/react-ui-table 0.4.10-main.0302e13 → 0.4.10-main.06ef97a
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/dist/lib/browser/index.mjs +540 -276
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts +19 -0
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -0
- package/dist/types/src/components/{ColumnMenu.stories.d.ts → ColumnMenu/ColumnMenu.stories.d.ts} +5 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -0
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts +11 -0
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -0
- package/dist/types/src/components/ColumnMenu/index.d.ts +3 -0
- package/dist/types/src/components/ColumnMenu/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/helpers.d.ts +4 -0
- package/dist/types/src/helpers.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +3 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useColumnSorting.d.ts +9 -0
- package/dist/types/src/hooks/useColumnSorting.d.ts.map +1 -0
- package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema/SchemaToTable.stories.d.ts +11 -0
- package/dist/types/src/schema/SchemaToTable.stories.d.ts.map +1 -0
- package/dist/types/src/schema/index.d.ts +3 -0
- package/dist/types/src/schema/index.d.ts.map +1 -0
- package/dist/types/src/schema/schema.d.ts +5 -0
- package/dist/types/src/schema/schema.d.ts.map +1 -0
- package/dist/types/src/schema/schema.test.d.ts +2 -0
- package/dist/types/src/schema/schema.test.d.ts.map +1 -0
- package/dist/types/src/schema/schemaToColumns.d.ts +4 -0
- package/dist/types/src/schema/schemaToColumns.d.ts.map +1 -0
- package/dist/types/src/{schema.d.ts → schema/types.d.ts} +1 -1
- package/dist/types/src/schema/types.d.ts.map +1 -0
- package/package.json +18 -15
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +97 -0
- package/src/components/ColumnMenu/ColumnMenu.tsx +166 -0
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +208 -0
- package/src/components/ColumnMenu/index.ts +6 -0
- package/src/components/Table/Table.tsx +1 -1
- package/src/components/index.ts +1 -1
- package/src/helpers.tsx +23 -1
- package/src/hooks/index.ts +6 -0
- package/src/hooks/useColumnSorting.ts +54 -0
- package/src/index.ts +3 -1
- package/src/schema/SchemaToTable.stories.tsx +72 -0
- package/src/schema/index.ts +6 -0
- package/src/schema/schema.test.ts +148 -0
- package/src/schema/schema.ts +93 -0
- package/src/schema/schemaToColumns.ts +47 -0
- package/src/theme.ts +1 -1
- package/dist/types/src/components/ColumnMenu.d.ts +0 -13
- package/dist/types/src/components/ColumnMenu.d.ts.map +0 -1
- package/dist/types/src/components/ColumnMenu.stories.d.ts.map +0 -1
- package/dist/types/src/components/SortButton.d.ts +0 -8
- package/dist/types/src/components/SortButton.d.ts.map +0 -1
- package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts.map +0 -1
- package/src/components/ColumnMenu.stories.tsx +0 -46
- package/src/components/ColumnMenu.tsx +0 -248
- package/src/components/SortButton.tsx +0 -34
- /package/dist/types/src/{components/hooks → hooks}/usePinLastRow.d.ts +0 -0
- /package/src/{components/hooks → hooks}/usePinLastRow.ts +0 -0
- /package/src/{schema.ts → schema/types.ts} +0 -0
|
@@ -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
|
+
};
|
package/src/theme.ts
CHANGED
|
@@ -82,7 +82,7 @@ export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent, is
|
|
|
82
82
|
canBeCurrent ? ghostSelectedCurrent : ghostSelected,
|
|
83
83
|
canBeCurrent && focusRing,
|
|
84
84
|
canBeCurrent && 'cursor-pointer rounded',
|
|
85
|
-
isPinned && 'sticky z-
|
|
85
|
+
isPinned && 'sticky z-1 bottom-[-1px] base-surface',
|
|
86
86
|
...etc,
|
|
87
87
|
);
|
|
88
88
|
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type HeaderContext, type RowData } from '@tanstack/react-table';
|
|
2
|
-
import { type TableDef, type ColumnProps } from '../schema';
|
|
3
|
-
export type ColumnMenuProps<TData extends RowData, TValue> = {
|
|
4
|
-
context: HeaderContext<TData, TValue>;
|
|
5
|
-
tableDefs: TableDef[];
|
|
6
|
-
tableDef: TableDef;
|
|
7
|
-
column: ColumnProps;
|
|
8
|
-
onUpdate?: (id: string, column: ColumnProps) => void;
|
|
9
|
-
onDelete?: (id: string) => void;
|
|
10
|
-
};
|
|
11
|
-
export declare const ColumnMenu: <TData extends unknown, TValue>({ column, ...props }: ColumnMenuProps<TData, TValue>) => JSX.Element;
|
|
12
|
-
export declare const ColumnPanel: <TData extends unknown, TValue>({ context, tableDefs, tableDef, column, onUpdate, onDelete, }: ColumnMenuProps<TData, TValue>) => JSX.Element;
|
|
13
|
-
//# sourceMappingURL=ColumnMenu.d.ts.map
|
|
@@ -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,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAQzE,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAmB,MAAM,WAAW,CAAC;AAG7E,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,sGAoBtB,CAAC;AAQF,eAAO,MAAM,WAAW,+IAkMvB,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;AAOpB,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;;;;;;AAE7C,wBAIE;AAEF,eAAO,MAAM,OAAO;;;;;;;;;;;;;yBAiBR,GAAG;;;;CASd,CAAC"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type SortDirection } from '@tanstack/react-table';
|
|
2
|
-
type SortButtonProps = {
|
|
3
|
-
sortDirection: false | SortDirection;
|
|
4
|
-
onClick: (e: any) => void;
|
|
5
|
-
};
|
|
6
|
-
export declare const SortButton: ({ sortDirection, onClick }: SortButtonProps) => JSX.Element;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=SortButton.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SortButton.d.ts","sourceRoot":"","sources":["../../../../src/components/SortButton.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAM3D,KAAK,eAAe,GAAG;IAAE,aAAa,EAAE,KAAK,GAAG,aAAa,CAAC;IAAC,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;CAAE,CAAC;AAE3F,eAAO,MAAM,UAAU,+BAAgC,eAAe,gBAoBrE,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"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxosTheme';
|
|
6
|
-
|
|
7
|
-
import React from 'react';
|
|
8
|
-
|
|
9
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
10
|
-
|
|
11
|
-
import { ColumnMenu } from './ColumnMenu';
|
|
12
|
-
import { type ColumnProps } from '../schema';
|
|
13
|
-
|
|
14
|
-
export default {
|
|
15
|
-
title: 'react-ui-table/ColumnMenu',
|
|
16
|
-
component: ColumnMenu,
|
|
17
|
-
decorators: [withTheme],
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const Default = {
|
|
21
|
-
args: {
|
|
22
|
-
column: {
|
|
23
|
-
id: 'test',
|
|
24
|
-
label: 'test',
|
|
25
|
-
} as ColumnProps,
|
|
26
|
-
context: {
|
|
27
|
-
header: {
|
|
28
|
-
column: {
|
|
29
|
-
getCanSort: () => true,
|
|
30
|
-
getToggleSortingHandler: () => () => console.log('toggleSort'),
|
|
31
|
-
getIsSorted: () => false,
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
decorators: [
|
|
37
|
-
(Story: any) => (
|
|
38
|
-
<div className='flex flex-col items-center h-screen w-full overflow-hidden'>
|
|
39
|
-
<Story />
|
|
40
|
-
</div>
|
|
41
|
-
),
|
|
42
|
-
],
|
|
43
|
-
parameters: {
|
|
44
|
-
layout: 'fullscreen',
|
|
45
|
-
},
|
|
46
|
-
};
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Check, Trash, X, GearSix } from '@phosphor-icons/react';
|
|
6
|
-
import { type HeaderContext, type RowData } from '@tanstack/react-table';
|
|
7
|
-
import React, { type FC, type PropsWithChildren, useRef, useState } from 'react';
|
|
8
|
-
|
|
9
|
-
import { Button, DensityProvider, Input, Popover, Select, Separator, useTranslation } from '@dxos/react-ui';
|
|
10
|
-
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
11
|
-
import { safeParseInt } from '@dxos/util';
|
|
12
|
-
|
|
13
|
-
import { SortButton } from './SortButton';
|
|
14
|
-
import { type TableDef, type ColumnProps, type ColumnType } from '../schema';
|
|
15
|
-
import { translationKey } from '../translations';
|
|
16
|
-
|
|
17
|
-
export type ColumnMenuProps<TData extends RowData, TValue> = {
|
|
18
|
-
context: HeaderContext<TData, TValue>;
|
|
19
|
-
tableDefs: TableDef[];
|
|
20
|
-
tableDef: TableDef;
|
|
21
|
-
column: ColumnProps;
|
|
22
|
-
onUpdate?: (id: string, column: ColumnProps) => void;
|
|
23
|
-
onDelete?: (id: string) => void;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const ColumnMenu = <TData extends RowData, TValue>({ column, ...props }: ColumnMenuProps<TData, TValue>) => {
|
|
27
|
-
const title = column.label?.length ? column.label : column.id;
|
|
28
|
-
|
|
29
|
-
const header = props.context.header;
|
|
30
|
-
|
|
31
|
-
const canSort = header.column.getCanSort();
|
|
32
|
-
const sortDirection = header.column.getIsSorted();
|
|
33
|
-
const toggleSort = header.column.getToggleSortingHandler();
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
<div className='flex items-center gap-2'>
|
|
37
|
-
<div className='flex-1 min-is-0 truncate' title={title}>
|
|
38
|
-
{title}
|
|
39
|
-
</div>
|
|
40
|
-
<div className='flex gap-0'>
|
|
41
|
-
{canSort && toggleSort && <SortButton sortDirection={sortDirection} onClick={toggleSort} />}
|
|
42
|
-
<ColumnPanel {...props} column={column} />
|
|
43
|
-
</div>
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const Section: FC<PropsWithChildren & { className?: string }> = ({ children, className }) => (
|
|
49
|
-
<div role='none' className={mx('p-2', className)}>
|
|
50
|
-
{children}
|
|
51
|
-
</div>
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
export const ColumnPanel = <TData extends RowData, TValue>({
|
|
55
|
-
context,
|
|
56
|
-
tableDefs,
|
|
57
|
-
tableDef,
|
|
58
|
-
column,
|
|
59
|
-
onUpdate,
|
|
60
|
-
onDelete,
|
|
61
|
-
}: ColumnMenuProps<TData, TValue>) => {
|
|
62
|
-
const [open, setOpen] = useState(false);
|
|
63
|
-
const [prop, setProp] = useState(column.id);
|
|
64
|
-
const [refTable, setRefTable] = useState(column.refTable);
|
|
65
|
-
const [refProp, setRefProp] = useState(column.refProp);
|
|
66
|
-
const [type, setType] = useState(String(column.type));
|
|
67
|
-
const [label, setLabel] = useState(column.label);
|
|
68
|
-
const [digits, setDigits] = useState(String(column.digits ?? '0'));
|
|
69
|
-
const propRef = useRef<HTMLInputElement>(null);
|
|
70
|
-
const { t } = useTranslation(translationKey);
|
|
71
|
-
|
|
72
|
-
const handleCancel = () => {
|
|
73
|
-
setProp(column.id);
|
|
74
|
-
setOpen(false);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const handleSave = () => {
|
|
78
|
-
// Check valid.
|
|
79
|
-
if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
|
|
80
|
-
propRef.current?.focus();
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// If already exists then check same type.
|
|
85
|
-
const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
|
|
86
|
-
if (current) {
|
|
87
|
-
// TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
|
|
88
|
-
if (current.type !== 'ref' /* || current.ref !== refTable */) {
|
|
89
|
-
// TODO(burdon): Show error.
|
|
90
|
-
propRef.current?.focus();
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
onUpdate?.(prop, {
|
|
96
|
-
id: prop, // TODO(burdon): Make unique.
|
|
97
|
-
prop,
|
|
98
|
-
label,
|
|
99
|
-
type: type as ColumnProps['type'],
|
|
100
|
-
refTable,
|
|
101
|
-
refProp,
|
|
102
|
-
digits: safeParseInt(digits),
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
setOpen(false);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
return (
|
|
109
|
-
<Popover.Root open={open} onOpenChange={(nextOpen) => setOpen(nextOpen)}>
|
|
110
|
-
<Popover.Trigger asChild>
|
|
111
|
-
<Button variant='ghost'>
|
|
112
|
-
<GearSix className={mx(getSize(4), 'rotate-90')} />
|
|
113
|
-
</Button>
|
|
114
|
-
</Popover.Trigger>
|
|
115
|
-
<Popover.Portal>
|
|
116
|
-
<Popover.Content>
|
|
117
|
-
<Popover.Viewport classNames='w-60'>
|
|
118
|
-
<DensityProvider density='fine'>
|
|
119
|
-
<div className='flex flex-col gap-1'>
|
|
120
|
-
<Section>
|
|
121
|
-
<Input.Root>
|
|
122
|
-
<Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
|
|
123
|
-
<Input.TextInput
|
|
124
|
-
placeholder='Column label'
|
|
125
|
-
value={label ?? ''}
|
|
126
|
-
onChange={(event) => setLabel(event.target.value)}
|
|
127
|
-
autoFocus
|
|
128
|
-
/>
|
|
129
|
-
</Input.Root>
|
|
130
|
-
<Input.Root>
|
|
131
|
-
<Input.Label classNames='mbe-1 mbs-3'>{t('property key label')}</Input.Label>
|
|
132
|
-
<Input.TextInput
|
|
133
|
-
ref={propRef}
|
|
134
|
-
placeholder='Property key'
|
|
135
|
-
// TODO(burdon): Provide hooks for value normalization, ENTER, ESC, etc.
|
|
136
|
-
value={prop ?? ''}
|
|
137
|
-
onChange={(event) => setProp(event.target.value.replace(/[^\w_]/g, ''))}
|
|
138
|
-
/>
|
|
139
|
-
</Input.Root>
|
|
140
|
-
<Input.Root>
|
|
141
|
-
<Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
|
|
142
|
-
<Select.Root value={type} onValueChange={setType}>
|
|
143
|
-
<Select.TriggerButton placeholder='Type' classNames='is-full' />
|
|
144
|
-
<Select.Portal>
|
|
145
|
-
<Select.Content>
|
|
146
|
-
<Select.Viewport>
|
|
147
|
-
{(['number', 'boolean', 'string', 'ref'] as ColumnType[]).map((type) => (
|
|
148
|
-
<Select.Option key={type} value={type}>
|
|
149
|
-
{t(`${type} column type label`)}
|
|
150
|
-
</Select.Option>
|
|
151
|
-
))}
|
|
152
|
-
</Select.Viewport>
|
|
153
|
-
</Select.Content>
|
|
154
|
-
</Select.Portal>
|
|
155
|
-
</Select.Root>
|
|
156
|
-
</Input.Root>
|
|
157
|
-
</Section>
|
|
158
|
-
|
|
159
|
-
{type === 'number' && (
|
|
160
|
-
<>
|
|
161
|
-
<Section>
|
|
162
|
-
<Input.Root>
|
|
163
|
-
<Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
|
|
164
|
-
{/* TODO(burdon): Constrain input to numbers. */}
|
|
165
|
-
<Input.TextInput value={digits ?? ''} onChange={(event) => setDigits(event.target.value)} />
|
|
166
|
-
</Input.Root>
|
|
167
|
-
</Section>
|
|
168
|
-
</>
|
|
169
|
-
)}
|
|
170
|
-
|
|
171
|
-
{/* TODO(burdon): Selectors on same line. */}
|
|
172
|
-
{type === 'ref' && (
|
|
173
|
-
<>
|
|
174
|
-
<Section>
|
|
175
|
-
<Input.Root>
|
|
176
|
-
<Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
|
|
177
|
-
<Select.Root value={refTable} onValueChange={setRefTable}>
|
|
178
|
-
<Select.TriggerButton placeholder='Table' classNames='is-full' />
|
|
179
|
-
<Select.Portal>
|
|
180
|
-
<Select.Content>
|
|
181
|
-
<Select.Viewport>
|
|
182
|
-
{tableDefs
|
|
183
|
-
.filter((t) => t.id !== tableDef.id)
|
|
184
|
-
.map(({ id, name }) => (
|
|
185
|
-
<Select.Option key={id} value={id}>
|
|
186
|
-
{name ?? id}
|
|
187
|
-
</Select.Option>
|
|
188
|
-
))}
|
|
189
|
-
</Select.Viewport>
|
|
190
|
-
</Select.Content>
|
|
191
|
-
</Select.Portal>
|
|
192
|
-
</Select.Root>
|
|
193
|
-
</Input.Root>
|
|
194
|
-
|
|
195
|
-
{refTable && (
|
|
196
|
-
<Input.Root>
|
|
197
|
-
<Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
|
|
198
|
-
<Select.Root value={refProp} onValueChange={setRefProp}>
|
|
199
|
-
<Select.TriggerButton placeholder='Property' classNames='is-full' />
|
|
200
|
-
<Select.Portal>
|
|
201
|
-
<Select.Content>
|
|
202
|
-
<Select.Viewport>
|
|
203
|
-
{tableDefs
|
|
204
|
-
.find((tableDef) => tableDef.id === refTable)
|
|
205
|
-
?.columns.map(({ id, label }) => (
|
|
206
|
-
<Select.Option key={id} value={id}>
|
|
207
|
-
{label ?? id}
|
|
208
|
-
</Select.Option>
|
|
209
|
-
))}
|
|
210
|
-
</Select.Viewport>
|
|
211
|
-
</Select.Content>
|
|
212
|
-
</Select.Portal>
|
|
213
|
-
</Select.Root>
|
|
214
|
-
</Input.Root>
|
|
215
|
-
)}
|
|
216
|
-
</Section>
|
|
217
|
-
</>
|
|
218
|
-
)}
|
|
219
|
-
|
|
220
|
-
<Separator classNames='mli-2' />
|
|
221
|
-
|
|
222
|
-
<Section className='space-b-1.5'>
|
|
223
|
-
{/* TODO(burdon): Style as DropdownMenuItem. */}
|
|
224
|
-
<Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
|
|
225
|
-
<span>{t('save label')}</span>
|
|
226
|
-
<div className='grow' />
|
|
227
|
-
<Check className={getSize(5)} />
|
|
228
|
-
</Button>
|
|
229
|
-
<Button classNames='is-full flex gap-2' onClick={handleCancel}>
|
|
230
|
-
<span>{t('cancel label', { ns: 'os' })}</span>
|
|
231
|
-
<div className='grow' />
|
|
232
|
-
<X className={getSize(5)} />
|
|
233
|
-
</Button>
|
|
234
|
-
<Button classNames='is-full flex gap-2' onClick={() => onDelete?.(column.id)}>
|
|
235
|
-
<span>{t('delete label')}</span>
|
|
236
|
-
<div className='grow' />
|
|
237
|
-
<Trash className={getSize(5)} />
|
|
238
|
-
</Button>
|
|
239
|
-
</Section>
|
|
240
|
-
</div>
|
|
241
|
-
</DensityProvider>
|
|
242
|
-
</Popover.Viewport>
|
|
243
|
-
<Popover.Arrow />
|
|
244
|
-
</Popover.Content>
|
|
245
|
-
</Popover.Portal>
|
|
246
|
-
</Popover.Root>
|
|
247
|
-
);
|
|
248
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { CaretUpDown, SortAscending, SortDescending } from '@phosphor-icons/react';
|
|
6
|
-
import { type SortDirection } from '@tanstack/react-table';
|
|
7
|
-
import React, { type ReactElement } from 'react';
|
|
8
|
-
|
|
9
|
-
import { Button } from '@dxos/react-ui';
|
|
10
|
-
import { getSize } from '@dxos/react-ui-theme';
|
|
11
|
-
|
|
12
|
-
type SortButtonProps = { sortDirection: false | SortDirection; onClick: (e: any) => void };
|
|
13
|
-
|
|
14
|
-
export const SortButton = ({ sortDirection, onClick }: SortButtonProps) => {
|
|
15
|
-
let icon: ReactElement;
|
|
16
|
-
const size = getSize(4);
|
|
17
|
-
|
|
18
|
-
switch (sortDirection) {
|
|
19
|
-
case 'asc':
|
|
20
|
-
icon = <SortAscending className={size} />;
|
|
21
|
-
break;
|
|
22
|
-
case 'desc':
|
|
23
|
-
icon = <SortDescending className={size} />;
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
icon = <CaretUpDown className={size} />;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return (
|
|
30
|
-
<Button variant='ghost' onClick={onClick}>
|
|
31
|
-
{icon}
|
|
32
|
-
</Button>
|
|
33
|
-
);
|
|
34
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|