@macrostrat/data-sheet 4.8.0 → 4.9.1
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/CHANGELOG.md +42 -0
- package/README.md +60 -19
- package/dist/actions/context.cjs +10 -1
- package/dist/actions/context.cjs.map +1 -1
- package/dist/actions/context.js +10 -1
- package/dist/actions/context.js.map +1 -1
- package/dist/actions/types.d.ts +4 -0
- package/dist/components/data-editor.cjs +527 -0
- package/dist/components/data-editor.cjs.map +1 -0
- package/dist/components/data-editor.d.ts +142 -0
- package/dist/components/data-editor.js +524 -0
- package/dist/components/data-editor.js.map +1 -0
- package/dist/components/editor-actions.cjs +167 -0
- package/dist/components/editor-actions.cjs.map +1 -0
- package/dist/components/editor-actions.d.ts +37 -0
- package/dist/components/editor-actions.js +165 -0
- package/dist/components/editor-actions.js.map +1 -0
- package/dist/components/editor-popup.cjs +11 -1
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +11 -1
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +3 -0
- package/dist/components/indicators/selection.cjs +10 -4
- package/dist/components/indicators/selection.cjs.map +1 -1
- package/dist/components/indicators/selection.js +10 -4
- package/dist/components/indicators/selection.js.map +1 -1
- package/dist/components/row-editor.cjs +351 -0
- package/dist/components/row-editor.cjs.map +1 -0
- package/dist/components/row-editor.d.ts +181 -0
- package/dist/components/row-editor.js +349 -0
- package/dist/components/row-editor.js.map +1 -0
- package/dist/components/row-editor.module.sass.cjs +51 -0
- package/dist/components/row-editor.module.sass.cjs.map +1 -0
- package/dist/components/row-editor.module.sass.js +49 -0
- package/dist/components/row-editor.module.sass.js.map +1 -0
- package/dist/components/toolbars/actions-toolbar.cjs +1 -1
- package/dist/components/toolbars/actions-toolbar.cjs.map +1 -1
- package/dist/components/toolbars/actions-toolbar.js +1 -1
- package/dist/components/toolbars/actions-toolbar.js.map +1 -1
- package/dist/data-sheet.css +208 -20
- package/dist/index.cjs +27 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +29 -2
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +14 -13
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/provider/column-spec.cjs +8 -1
- package/dist/provider/column-spec.cjs.map +1 -1
- package/dist/provider/column-spec.d.ts +105 -1
- package/dist/provider/column-spec.js +8 -1
- package/dist/provider/column-spec.js.map +1 -1
- package/dist/provider/core.cjs +16 -4
- package/dist/provider/core.cjs.map +1 -1
- package/dist/provider/core.d.ts +4 -0
- package/dist/provider/core.js +16 -4
- package/dist/provider/core.js.map +1 -1
- package/dist/provider/types.cjs.map +1 -1
- package/dist/provider/types.d.ts +6 -1
- package/dist/provider/types.js.map +1 -1
- package/dist/provider/zustand-store.cjs +55 -33
- package/dist/provider/zustand-store.cjs.map +1 -1
- package/dist/provider/zustand-store.js +44 -22
- package/dist/provider/zustand-store.js.map +1 -1
- package/dist/renderers/cell-renderer.cjs +15 -4
- package/dist/renderers/cell-renderer.cjs.map +1 -1
- package/dist/renderers/cell-renderer.js +13 -4
- package/dist/renderers/cell-renderer.js.map +1 -1
- package/dist/renderers/column-header-cell.cjs +12 -1
- package/dist/renderers/column-header-cell.cjs.map +1 -1
- package/dist/renderers/column-header-cell.js +11 -1
- package/dist/renderers/column-header-cell.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/actions/context.ts +13 -2
- package/src/actions/types.ts +5 -0
- package/src/components/data-editor.ts +741 -0
- package/src/components/editor-actions.ts +228 -0
- package/src/components/editor-popup.ts +16 -3
- package/src/components/index.ts +3 -0
- package/src/components/indicators/selection.ts +12 -4
- package/src/components/row-editor.module.sass +161 -0
- package/src/components/row-editor.ts +620 -0
- package/src/components/toolbars/actions-toolbar.ts +3 -2
- package/src/main.module.sass +19 -0
- package/src/provider/column-spec.ts +94 -16
- package/src/provider/core.ts +19 -4
- package/src/provider/types.ts +6 -1
- package/src/provider/zustand-store.ts +78 -26
- package/src/renderers/cell-renderer.ts +18 -3
- package/src/renderers/column-header-cell.ts +10 -0
- package/src/types.ts +5 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { CellDetailContext, ColumnSpec } from '../provider';
|
|
3
|
+
import { TableAction } from '../actions';
|
|
4
|
+
/** A field of a data spec: a column spec's field, read the same way. */
|
|
5
|
+
export type FieldSpec = ColumnSpec;
|
|
6
|
+
export interface DataEditorProps<T = any> {
|
|
7
|
+
/** The fields, in order. */
|
|
8
|
+
dataSpec: FieldSpec[];
|
|
9
|
+
/** The record as loaded. A new record clears pending edits (uncontrolled). */
|
|
10
|
+
data: T;
|
|
11
|
+
/** Pending edits, when the caller keeps them (controlled). */
|
|
12
|
+
edits?: Partial<T>;
|
|
13
|
+
/** Pending edits changed — every field change, reset and save. */
|
|
14
|
+
onEditsChange?: (edits: Partial<T>) => void;
|
|
15
|
+
/** Persist the edited record. Absent, the editor has no Save. A returned
|
|
16
|
+
* promise holds the Save button busy; the edits clear once it resolves. */
|
|
17
|
+
onSave?: (value: T, edits: Partial<T>) => void | Promise<void>;
|
|
18
|
+
/** Whether the form takes edits (default). Off, it is a viewer. */
|
|
19
|
+
editable?: boolean;
|
|
20
|
+
/** A sheet's actions, scoped to the form (see `editor-actions.ts`): those
|
|
21
|
+
* for a row as the record's, in the footer before Reset and Save; those
|
|
22
|
+
* for a cell or a column, and a field's own column `actions`, beside each
|
|
23
|
+
* field's label. */
|
|
24
|
+
actions?: TableAction<T>[];
|
|
25
|
+
/** Whether to show the actions (default). A sheet's own editors turn them
|
|
26
|
+
* off, where its toolbar already carries them. */
|
|
27
|
+
showActions?: boolean;
|
|
28
|
+
/** Draw the form as a panel: bordered, with a title bar and footer. */
|
|
29
|
+
panel?: boolean;
|
|
30
|
+
title?: ReactNode;
|
|
31
|
+
/** Close the editor — a ✕ in the title bar. */
|
|
32
|
+
onClose?: () => void;
|
|
33
|
+
closeLabel?: string;
|
|
34
|
+
saveLabel?: string;
|
|
35
|
+
showHidden?: boolean;
|
|
36
|
+
hideEmpty?: boolean;
|
|
37
|
+
inline?: boolean;
|
|
38
|
+
className?: string;
|
|
39
|
+
/** Rendered above the fields. */
|
|
40
|
+
header?: ReactNode;
|
|
41
|
+
children?: ReactNode;
|
|
42
|
+
}
|
|
43
|
+
export declare function DataEditor<T = any>(props: DataEditorProps<T>): import('react').FunctionComponentElement<{
|
|
44
|
+
panel?: boolean;
|
|
45
|
+
title?: ReactNode;
|
|
46
|
+
onClose?: () => void;
|
|
47
|
+
closeLabel?: string;
|
|
48
|
+
footer?: ReactNode;
|
|
49
|
+
className?: string;
|
|
50
|
+
children?: ReactNode;
|
|
51
|
+
}>;
|
|
52
|
+
export interface DataEditorFieldsProps<T = any> {
|
|
53
|
+
/** The fields, in order; hidden ones are left out unless `showHidden`. */
|
|
54
|
+
dataSpec: FieldSpec[];
|
|
55
|
+
/** The records the form stands for — one, or several at once. */
|
|
56
|
+
rows: (T | null | undefined)[];
|
|
57
|
+
/** Edit overlays aligned with `rows`. */
|
|
58
|
+
rowEdits?: (Partial<T> | null | undefined)[];
|
|
59
|
+
/** The rows' data indices, aligned with `rows`, handed to `validate` and
|
|
60
|
+
* `cellDetail`. */
|
|
61
|
+
rowIndices?: number[];
|
|
62
|
+
/** Where the form is drawn, handed to each `cellDetail` as `ctx.surface`. */
|
|
63
|
+
surface: CellDetailContext["surface"];
|
|
64
|
+
/** Whether the form takes edits. */
|
|
65
|
+
editable?: boolean;
|
|
66
|
+
/** A field changed — for every row the form stands for. */
|
|
67
|
+
onChange?: (key: string, value: any) => void;
|
|
68
|
+
/** A field changed row by row, a value per row. */
|
|
69
|
+
onChangeCells?: (key: string, values: any[]) => void;
|
|
70
|
+
/** Revert one field in every row. */
|
|
71
|
+
onResetField?: (key: string) => void;
|
|
72
|
+
/** The only fields that take edits (the rest shown for context); `null`
|
|
73
|
+
* for all. A sheet's cell selection. */
|
|
74
|
+
focusFields?: string[] | null;
|
|
75
|
+
/** A field to mark without narrowing what is editable. */
|
|
76
|
+
activeField?: string | null;
|
|
77
|
+
/** A field was clicked. */
|
|
78
|
+
onFieldClick?: (key: string) => void;
|
|
79
|
+
/** Controls beside a field's label — its actions. */
|
|
80
|
+
fieldActions?: (key: string) => ReactNode;
|
|
81
|
+
showHidden?: boolean;
|
|
82
|
+
hideEmpty?: boolean;
|
|
83
|
+
inline?: boolean;
|
|
84
|
+
}
|
|
85
|
+
/** A form over one record or several, writing through callbacks. Over
|
|
86
|
+
* several, each field stands for that key in every record: a shared value is
|
|
87
|
+
* shown and edited as one, differing ones read "Multiple values", and a
|
|
88
|
+
* field's own `cellDetail` edits them only when it declares `multiCell`. */
|
|
89
|
+
export declare function DataEditorFields<T = any>(props: DataEditorFieldsProps<T>): import('react').ReactElement<{
|
|
90
|
+
className: string;
|
|
91
|
+
}, string | import('react').JSXElementConstructor<any>>;
|
|
92
|
+
/** The form's frame: as a panel, a title bar over a scrolling body and an
|
|
93
|
+
* optional footer; otherwise the body (and footer) alone. The title bar is
|
|
94
|
+
* the sheet toolbar's selection tag at full width — minimal, large, primary —
|
|
95
|
+
* and its ✕ is the tag's own. */
|
|
96
|
+
export declare function DataEditorFrame({ panel, title, onClose, closeLabel, footer, className, children, }: {
|
|
97
|
+
panel?: boolean;
|
|
98
|
+
title?: ReactNode;
|
|
99
|
+
onClose?: () => void;
|
|
100
|
+
closeLabel?: string;
|
|
101
|
+
footer?: ReactNode;
|
|
102
|
+
className?: string;
|
|
103
|
+
children?: ReactNode;
|
|
104
|
+
}): import('react').ReactElement<{
|
|
105
|
+
className: string;
|
|
106
|
+
}, string | import('react').JSXElementConstructor<any>>;
|
|
107
|
+
/** The value a set of cells share, or that they differ. Structured values
|
|
108
|
+
* compare by their JSON form, so two equal lithology arrays count as one. */
|
|
109
|
+
export declare function sharedValue(values: any[]): {
|
|
110
|
+
value: any;
|
|
111
|
+
mixed: boolean;
|
|
112
|
+
};
|
|
113
|
+
export declare function valueToken(value: any): string;
|
|
114
|
+
interface DataEditorFieldProps {
|
|
115
|
+
ctx: CellDetailContext;
|
|
116
|
+
inline?: boolean;
|
|
117
|
+
/** Controls after the label: the field's actions. */
|
|
118
|
+
actions?: ReactNode;
|
|
119
|
+
/** A click anywhere in the field. */
|
|
120
|
+
onClick?: () => void;
|
|
121
|
+
/** Part of the cell selection the form is focused on. */
|
|
122
|
+
selected?: boolean;
|
|
123
|
+
onReset?: (() => void) | null;
|
|
124
|
+
}
|
|
125
|
+
/** One field: the column's name, a marker for what kind of value it is, the
|
|
126
|
+
* value or its editor, and any validation message. */
|
|
127
|
+
export declare function DataEditorField({ ctx, inline, selected, onReset, onClick, actions, }: DataEditorFieldProps): import('react').DOMElement<{
|
|
128
|
+
onClick: (() => void) | undefined;
|
|
129
|
+
}, Element>;
|
|
130
|
+
/** The read-only rendering of a value, through the column's renderer. */
|
|
131
|
+
export declare function FieldValue({ ctx }: {
|
|
132
|
+
ctx: CellDetailContext;
|
|
133
|
+
}): import('react').DOMElement<import('react').DOMAttributes<Element>, Element> | import('react').FunctionComponentElement<{
|
|
134
|
+
ctx: CellDetailContext;
|
|
135
|
+
}>;
|
|
136
|
+
/** "Multiple values", naming how many distinct ones the cells hold. */
|
|
137
|
+
export declare function MixedValues({ ctx }: {
|
|
138
|
+
ctx: CellDetailContext;
|
|
139
|
+
}): import('react').ReactElement<{
|
|
140
|
+
title: string;
|
|
141
|
+
}, string | import('react').JSXElementConstructor<any>>;
|
|
142
|
+
export {};
|
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import h$1 from "@macrostrat/hyper";
|
|
2
|
+
import classNames from "classnames";
|
|
3
|
+
import { Button, Tag, FormGroup, Switch, TextArea, InputGroup } from "@blueprintjs/core";
|
|
4
|
+
import { useState, useEffect, useRef, useMemo } from "react";
|
|
5
|
+
import { validateCell } from "../utils/validation.js";
|
|
6
|
+
import { editorActionsFor, EditorActionButton } from "./editor-actions.js";
|
|
7
|
+
import h$2 from "./row-editor.module.sass.js";
|
|
8
|
+
import { isColumnWritable } from "../provider/column-spec.js";
|
|
9
|
+
const h = h$1.styled(h$2);
|
|
10
|
+
function DataEditor(props) {
|
|
11
|
+
const {
|
|
12
|
+
dataSpec,
|
|
13
|
+
data,
|
|
14
|
+
onEditsChange,
|
|
15
|
+
onSave,
|
|
16
|
+
editable = true,
|
|
17
|
+
actions = [],
|
|
18
|
+
showActions = true,
|
|
19
|
+
panel = false,
|
|
20
|
+
title,
|
|
21
|
+
onClose,
|
|
22
|
+
closeLabel,
|
|
23
|
+
saveLabel = "Save",
|
|
24
|
+
showHidden,
|
|
25
|
+
hideEmpty,
|
|
26
|
+
inline,
|
|
27
|
+
className,
|
|
28
|
+
header,
|
|
29
|
+
children
|
|
30
|
+
} = props;
|
|
31
|
+
const [ownEdits, setOwnEdits] = useState({});
|
|
32
|
+
const [saving, setSaving] = useState(false);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setOwnEdits({});
|
|
35
|
+
}, [data]);
|
|
36
|
+
let edits = ownEdits;
|
|
37
|
+
if (props.edits != null) edits = props.edits;
|
|
38
|
+
const latest = useRef(edits);
|
|
39
|
+
latest.current = edits;
|
|
40
|
+
const setEdits = (next) => {
|
|
41
|
+
latest.current = next;
|
|
42
|
+
if (props.edits == null) setOwnEdits(next);
|
|
43
|
+
onEditsChange?.(next);
|
|
44
|
+
};
|
|
45
|
+
const value = useMemo(() => ({ ...data, ...edits }), [data, edits]);
|
|
46
|
+
const hasChanges = Object.keys(edits).length > 0;
|
|
47
|
+
const errors = useMemo(() => fieldErrors(dataSpec, value), [dataSpec, value]);
|
|
48
|
+
const setFields = (patch) => {
|
|
49
|
+
const updated = { ...latest.current };
|
|
50
|
+
for (const [key, next] of Object.entries(patch)) {
|
|
51
|
+
updated[key] = next;
|
|
52
|
+
if (valueToken(next) === valueToken(data?.[key])) {
|
|
53
|
+
delete updated[key];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
setEdits(updated);
|
|
57
|
+
};
|
|
58
|
+
const setField = (key, next) => setFields({ [key]: next });
|
|
59
|
+
const resetField = (key) => {
|
|
60
|
+
const updated = { ...latest.current };
|
|
61
|
+
delete updated[key];
|
|
62
|
+
setEdits(updated);
|
|
63
|
+
};
|
|
64
|
+
const reset = () => setEdits({});
|
|
65
|
+
const save = async () => {
|
|
66
|
+
if (onSave == null || !hasChanges || errors.length > 0) return;
|
|
67
|
+
setSaving(true);
|
|
68
|
+
try {
|
|
69
|
+
await onSave(value, edits);
|
|
70
|
+
setEdits({});
|
|
71
|
+
} finally {
|
|
72
|
+
setSaving(false);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const target = {
|
|
76
|
+
dataSpec,
|
|
77
|
+
data,
|
|
78
|
+
value,
|
|
79
|
+
edits,
|
|
80
|
+
editable,
|
|
81
|
+
setFields,
|
|
82
|
+
reset,
|
|
83
|
+
onSave
|
|
84
|
+
};
|
|
85
|
+
let usedActions = [];
|
|
86
|
+
if (showActions) usedActions = actions;
|
|
87
|
+
const record = editorActionsFor(usedActions, target, { kind: "record" });
|
|
88
|
+
let fieldActions;
|
|
89
|
+
if (usedActions.length > 0 || dataSpec.some((d) => d.actions != null)) {
|
|
90
|
+
fieldActions = (columnKey) => {
|
|
91
|
+
if (!showActions) return null;
|
|
92
|
+
const scoped = editorActionsFor(usedActions, target, {
|
|
93
|
+
kind: "field",
|
|
94
|
+
columnKey
|
|
95
|
+
});
|
|
96
|
+
return scoped.actions.map(
|
|
97
|
+
(action) => h(EditorActionButton, {
|
|
98
|
+
key: action.id,
|
|
99
|
+
action,
|
|
100
|
+
ctx: scoped.ctx,
|
|
101
|
+
iconOnly: true
|
|
102
|
+
})
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
let footer = null;
|
|
107
|
+
if (editable && (onSave != null || record.actions.length > 0)) {
|
|
108
|
+
let errorsTitle;
|
|
109
|
+
if (errors.length > 0) {
|
|
110
|
+
errorsTitle = errors.map((d) => d.message).join("; ");
|
|
111
|
+
}
|
|
112
|
+
footer = h("div.data-editor-footer", [
|
|
113
|
+
record.actions.map(
|
|
114
|
+
(action) => h(EditorActionButton, { key: action.id, action, ctx: record.ctx })
|
|
115
|
+
),
|
|
116
|
+
h("span.data-editor-footer-spacer"),
|
|
117
|
+
h(Button, {
|
|
118
|
+
small: true,
|
|
119
|
+
minimal: true,
|
|
120
|
+
icon: "undo",
|
|
121
|
+
text: "Reset",
|
|
122
|
+
disabled: !hasChanges || saving,
|
|
123
|
+
onClick: reset
|
|
124
|
+
}),
|
|
125
|
+
h.if(onSave != null)(Button, {
|
|
126
|
+
small: true,
|
|
127
|
+
intent: "primary",
|
|
128
|
+
icon: "floppy-disk",
|
|
129
|
+
text: saveLabel,
|
|
130
|
+
loading: saving,
|
|
131
|
+
title: errorsTitle,
|
|
132
|
+
disabled: !hasChanges || errors.length > 0,
|
|
133
|
+
onClick: save
|
|
134
|
+
})
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
return h(
|
|
138
|
+
DataEditorFrame,
|
|
139
|
+
{
|
|
140
|
+
className: classNames("data-editor", className, { editable }),
|
|
141
|
+
panel,
|
|
142
|
+
title,
|
|
143
|
+
onClose,
|
|
144
|
+
closeLabel,
|
|
145
|
+
footer
|
|
146
|
+
},
|
|
147
|
+
[
|
|
148
|
+
header,
|
|
149
|
+
h(DataEditorFields, {
|
|
150
|
+
dataSpec,
|
|
151
|
+
rows: [data],
|
|
152
|
+
rowEdits: [edits],
|
|
153
|
+
surface: "editor",
|
|
154
|
+
editable,
|
|
155
|
+
showHidden,
|
|
156
|
+
hideEmpty,
|
|
157
|
+
inline,
|
|
158
|
+
onChange: setField,
|
|
159
|
+
onResetField: resetField,
|
|
160
|
+
fieldActions
|
|
161
|
+
}),
|
|
162
|
+
children
|
|
163
|
+
]
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
function fieldErrors(dataSpec, value) {
|
|
167
|
+
const errors = [];
|
|
168
|
+
for (const field of dataSpec) {
|
|
169
|
+
if (field.hidden || !isColumnWritable(field, true)) continue;
|
|
170
|
+
const result = validateCell(field, value?.[field.key], value, -1);
|
|
171
|
+
if (result?.severity === "error") {
|
|
172
|
+
errors.push({ key: field.key, message: result.message ?? field.name });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return errors;
|
|
176
|
+
}
|
|
177
|
+
function DataEditorFields(props) {
|
|
178
|
+
const {
|
|
179
|
+
dataSpec,
|
|
180
|
+
rows,
|
|
181
|
+
rowEdits = [],
|
|
182
|
+
rowIndices = [],
|
|
183
|
+
surface,
|
|
184
|
+
editable = true,
|
|
185
|
+
onChange,
|
|
186
|
+
onChangeCells,
|
|
187
|
+
onResetField,
|
|
188
|
+
focusFields = null,
|
|
189
|
+
activeField = null,
|
|
190
|
+
onFieldClick,
|
|
191
|
+
fieldActions,
|
|
192
|
+
showHidden = false,
|
|
193
|
+
hideEmpty = false,
|
|
194
|
+
inline = false
|
|
195
|
+
} = props;
|
|
196
|
+
const merged = useMemo(
|
|
197
|
+
() => rows.map((row, i) => ({ ...row ?? {}, ...rowEdits[i] ?? {} })),
|
|
198
|
+
[rows, rowEdits]
|
|
199
|
+
);
|
|
200
|
+
const several = rows.length > 1;
|
|
201
|
+
const fieldsShown = useMemo(
|
|
202
|
+
() => dataSpec.filter((field) => showHidden || !field.hidden),
|
|
203
|
+
[dataSpec, showHidden]
|
|
204
|
+
);
|
|
205
|
+
const canEdit = editable && onChange != null;
|
|
206
|
+
let focus = null;
|
|
207
|
+
if (focusFields != null) focus = new Set(focusFields);
|
|
208
|
+
const fields = fieldsShown.map((col, colIndex) => {
|
|
209
|
+
const values = merged.map((row) => row[col.key]);
|
|
210
|
+
const shared = sharedValue(values);
|
|
211
|
+
let value = shared.value;
|
|
212
|
+
if (shared.mixed) value = void 0;
|
|
213
|
+
const focused = focus == null || focus.has(col.key);
|
|
214
|
+
const canEditSeveral = !several || col.cellDetail == null || (col.multiCell ?? false);
|
|
215
|
+
const writable = canEdit && focused && canEditSeveral && isColumnWritable(col, true);
|
|
216
|
+
const isEmpty = value == null || value === "";
|
|
217
|
+
if (hideEmpty && isEmpty && !shared.mixed && !writable) return null;
|
|
218
|
+
const isEdited = rowEdits.some((e) => e != null && col.key in e);
|
|
219
|
+
let validation = null;
|
|
220
|
+
if (!shared.mixed) {
|
|
221
|
+
validation = validateCell(col, value, merged[0], rowIndices[0] ?? -1);
|
|
222
|
+
}
|
|
223
|
+
const ctx = {
|
|
224
|
+
surface,
|
|
225
|
+
value,
|
|
226
|
+
rowIndex: rowIndices[0] ?? -1,
|
|
227
|
+
colIndex,
|
|
228
|
+
column: col,
|
|
229
|
+
row: merged[0],
|
|
230
|
+
isEdited,
|
|
231
|
+
isDeleted: false,
|
|
232
|
+
status: void 0,
|
|
233
|
+
validation,
|
|
234
|
+
editable: writable,
|
|
235
|
+
onChange(next) {
|
|
236
|
+
if (!writable) return;
|
|
237
|
+
onChange?.(col.key, next);
|
|
238
|
+
},
|
|
239
|
+
resetValue() {
|
|
240
|
+
onResetField?.(col.key);
|
|
241
|
+
},
|
|
242
|
+
close() {
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
if (several) {
|
|
246
|
+
ctx.cells = rows.map((row, i) => ({
|
|
247
|
+
rowIndex: rowIndices[i] ?? -1,
|
|
248
|
+
row: merged[i],
|
|
249
|
+
value: values[i]
|
|
250
|
+
}));
|
|
251
|
+
ctx.mixed = shared.mixed;
|
|
252
|
+
if (onChangeCells != null) {
|
|
253
|
+
ctx.onChangeCells = (next) => {
|
|
254
|
+
if (!writable) return;
|
|
255
|
+
onChangeCells(col.key, next);
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
let onClick;
|
|
260
|
+
if (onFieldClick != null) onClick = () => onFieldClick(col.key);
|
|
261
|
+
let onReset = null;
|
|
262
|
+
if (isEdited && onResetField != null) onReset = ctx.resetValue;
|
|
263
|
+
return h(DataEditorField, {
|
|
264
|
+
key: col.key,
|
|
265
|
+
ctx,
|
|
266
|
+
inline,
|
|
267
|
+
onClick,
|
|
268
|
+
actions: fieldActions?.(col.key),
|
|
269
|
+
selected: focus != null && focus.has(col.key) || activeField === col.key,
|
|
270
|
+
onReset
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
return h(
|
|
274
|
+
"div.row-editor-fields",
|
|
275
|
+
{ className: classNames({ editable: canEdit, inline, several }) },
|
|
276
|
+
fields
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
function DataEditorFrame({
|
|
280
|
+
panel = false,
|
|
281
|
+
title,
|
|
282
|
+
onClose,
|
|
283
|
+
closeLabel = "Close",
|
|
284
|
+
footer,
|
|
285
|
+
className,
|
|
286
|
+
children
|
|
287
|
+
}) {
|
|
288
|
+
let titleBar = null;
|
|
289
|
+
if (panel && (title != null || onClose != null)) {
|
|
290
|
+
titleBar = h(
|
|
291
|
+
Tag,
|
|
292
|
+
{
|
|
293
|
+
minimal: true,
|
|
294
|
+
large: true,
|
|
295
|
+
fill: true,
|
|
296
|
+
intent: "primary",
|
|
297
|
+
className: "row-editor-title-bar",
|
|
298
|
+
onRemove: onClose,
|
|
299
|
+
removeButtonProps: { title: closeLabel, "aria-label": closeLabel }
|
|
300
|
+
},
|
|
301
|
+
title
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
return h("div.row-editor", { className: classNames(className, { panel }) }, [
|
|
305
|
+
titleBar,
|
|
306
|
+
h("div.row-editor-body", children),
|
|
307
|
+
footer
|
|
308
|
+
]);
|
|
309
|
+
}
|
|
310
|
+
function sharedValue(values) {
|
|
311
|
+
if (values.length === 0) return { value: void 0, mixed: false };
|
|
312
|
+
const first = values[0];
|
|
313
|
+
const key = valueToken(first);
|
|
314
|
+
for (const v of values.slice(1)) {
|
|
315
|
+
if (valueToken(v) !== key) return { value: void 0, mixed: true };
|
|
316
|
+
}
|
|
317
|
+
return { value: first, mixed: false };
|
|
318
|
+
}
|
|
319
|
+
function valueToken(value) {
|
|
320
|
+
if (value == null || value === "") return "";
|
|
321
|
+
if (typeof value === "object") return JSON.stringify(value);
|
|
322
|
+
return String(value);
|
|
323
|
+
}
|
|
324
|
+
function DataEditorField({
|
|
325
|
+
ctx,
|
|
326
|
+
inline,
|
|
327
|
+
selected = false,
|
|
328
|
+
onReset,
|
|
329
|
+
onClick,
|
|
330
|
+
actions
|
|
331
|
+
}) {
|
|
332
|
+
const { column: col, validation, isEdited, editable, mixed } = ctx;
|
|
333
|
+
let intent;
|
|
334
|
+
if (validation?.severity === "error") {
|
|
335
|
+
intent = "danger";
|
|
336
|
+
} else if (validation?.severity === "warning") {
|
|
337
|
+
intent = "warning";
|
|
338
|
+
}
|
|
339
|
+
let marker = null;
|
|
340
|
+
if (col.derived) {
|
|
341
|
+
marker = h(
|
|
342
|
+
Tag,
|
|
343
|
+
{
|
|
344
|
+
minimal: true,
|
|
345
|
+
icon: "function",
|
|
346
|
+
className: "field-marker",
|
|
347
|
+
title: "Derived — computed from other values"
|
|
348
|
+
},
|
|
349
|
+
"derived"
|
|
350
|
+
);
|
|
351
|
+
} else if (col.required && editable) {
|
|
352
|
+
marker = h("span.required-marker", { title: "Required" }, "*");
|
|
353
|
+
}
|
|
354
|
+
let reset = null;
|
|
355
|
+
if (onReset != null) {
|
|
356
|
+
reset = h(Button, {
|
|
357
|
+
icon: "undo",
|
|
358
|
+
minimal: true,
|
|
359
|
+
small: true,
|
|
360
|
+
className: "reset-field",
|
|
361
|
+
title: "Revert to the loaded value",
|
|
362
|
+
onClick: onReset
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
const label = h("span.field-label", [
|
|
366
|
+
h("span.field-name", col.name),
|
|
367
|
+
marker,
|
|
368
|
+
reset,
|
|
369
|
+
h.if(actions != null)("span.field-actions", actions)
|
|
370
|
+
]);
|
|
371
|
+
return h(
|
|
372
|
+
"div.row-editor-field-target",
|
|
373
|
+
{ onClick },
|
|
374
|
+
h(
|
|
375
|
+
FormGroup,
|
|
376
|
+
{
|
|
377
|
+
label,
|
|
378
|
+
inline,
|
|
379
|
+
intent,
|
|
380
|
+
helperText: validation?.message,
|
|
381
|
+
className: classNames("row-editor-field", {
|
|
382
|
+
edited: isEdited,
|
|
383
|
+
derived: col.derived,
|
|
384
|
+
selected,
|
|
385
|
+
mixed,
|
|
386
|
+
"read-only": !editable,
|
|
387
|
+
[`data-type-${col.dataType ?? "string"}`]: true
|
|
388
|
+
})
|
|
389
|
+
},
|
|
390
|
+
h(FieldSurface, { ctx })
|
|
391
|
+
)
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
function FieldSurface({ ctx }) {
|
|
395
|
+
const { column: col, editable } = ctx;
|
|
396
|
+
const several = ctx.cells != null;
|
|
397
|
+
if (col.cellDetail != null && (!several || col.multiCell)) {
|
|
398
|
+
return h("div.field-surface.cell-detail", col.cellDetail(ctx));
|
|
399
|
+
}
|
|
400
|
+
if (!editable) {
|
|
401
|
+
return h(FieldValue, { ctx });
|
|
402
|
+
}
|
|
403
|
+
return h(DefaultFieldEditor, { ctx });
|
|
404
|
+
}
|
|
405
|
+
function FieldValue({ ctx }) {
|
|
406
|
+
const { value, column: col } = ctx;
|
|
407
|
+
if (ctx.mixed) {
|
|
408
|
+
return h(MixedValues, { ctx });
|
|
409
|
+
}
|
|
410
|
+
if (value == null || value === "") {
|
|
411
|
+
return h("span.field-value.empty", "—");
|
|
412
|
+
}
|
|
413
|
+
const rendered = col.valueRenderer?.(value, ctx) ?? String(value);
|
|
414
|
+
return h("span.field-value", rendered);
|
|
415
|
+
}
|
|
416
|
+
function MixedValues({ ctx }) {
|
|
417
|
+
const distinct = new Set((ctx.cells ?? []).map((c) => valueToken(c.value)));
|
|
418
|
+
return h(
|
|
419
|
+
"span.field-value.mixed-values",
|
|
420
|
+
{
|
|
421
|
+
title: `${distinct.size} distinct values across ${ctx.cells?.length} rows`
|
|
422
|
+
},
|
|
423
|
+
"Multiple values"
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
function DefaultFieldEditor({ ctx }) {
|
|
427
|
+
const { value, column: col, onChange, mixed } = ctx;
|
|
428
|
+
const type = col.dataType ?? "string";
|
|
429
|
+
const placeholder = mixed ? "Multiple values" : void 0;
|
|
430
|
+
if (type === "boolean") {
|
|
431
|
+
return h(Switch, {
|
|
432
|
+
checked: Boolean(value),
|
|
433
|
+
// An indeterminate switch has no Blueprint form; say so beside it.
|
|
434
|
+
label: mixed ? "Multiple values" : void 0,
|
|
435
|
+
onChange: (evt) => onChange(evt.target.checked),
|
|
436
|
+
className: "field-switch"
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
if (type === "text") {
|
|
440
|
+
return h(CommittedTextArea, { value, placeholder, onCommit: onChange });
|
|
441
|
+
}
|
|
442
|
+
if (type === "number" || type === "integer") {
|
|
443
|
+
return h(CommittedInput, {
|
|
444
|
+
value,
|
|
445
|
+
placeholder,
|
|
446
|
+
onCommit: (text) => onChange(parseNumber(text, type)),
|
|
447
|
+
type: "number",
|
|
448
|
+
step: type === "integer" ? 1 : "any"
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
if (type === "object" || type === "array") {
|
|
452
|
+
return h(FieldValue, { ctx });
|
|
453
|
+
}
|
|
454
|
+
return h(CommittedInput, { value, placeholder, onCommit: onChange });
|
|
455
|
+
}
|
|
456
|
+
function parseNumber(text, type) {
|
|
457
|
+
if (text === "" || text == null) return null;
|
|
458
|
+
const n = type === "integer" ? parseInt(text, 10) : parseFloat(text);
|
|
459
|
+
if (isNaN(n)) return text;
|
|
460
|
+
return n;
|
|
461
|
+
}
|
|
462
|
+
function CommittedInput({
|
|
463
|
+
value,
|
|
464
|
+
onCommit,
|
|
465
|
+
...rest
|
|
466
|
+
}) {
|
|
467
|
+
const [text, setText] = useState(toText(value));
|
|
468
|
+
useEffect(() => {
|
|
469
|
+
setText(toText(value));
|
|
470
|
+
}, [value]);
|
|
471
|
+
const commit = () => {
|
|
472
|
+
if (text === toText(value)) return;
|
|
473
|
+
onCommit(text);
|
|
474
|
+
};
|
|
475
|
+
return h(InputGroup, {
|
|
476
|
+
small: true,
|
|
477
|
+
fill: true,
|
|
478
|
+
value: text,
|
|
479
|
+
onValueChange: setText,
|
|
480
|
+
onBlur: commit,
|
|
481
|
+
onKeyDown(evt) {
|
|
482
|
+
if (evt.key === "Enter") commit();
|
|
483
|
+
if (evt.key === "Escape") setText(toText(value));
|
|
484
|
+
},
|
|
485
|
+
...rest
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
function CommittedTextArea({
|
|
489
|
+
value,
|
|
490
|
+
onCommit,
|
|
491
|
+
placeholder
|
|
492
|
+
}) {
|
|
493
|
+
const [text, setText] = useState(toText(value));
|
|
494
|
+
useEffect(() => {
|
|
495
|
+
setText(toText(value));
|
|
496
|
+
}, [value]);
|
|
497
|
+
return h(TextArea, {
|
|
498
|
+
small: true,
|
|
499
|
+
fill: true,
|
|
500
|
+
autoResize: true,
|
|
501
|
+
placeholder,
|
|
502
|
+
value: text,
|
|
503
|
+
onChange: (evt) => setText(evt.target.value),
|
|
504
|
+
onBlur() {
|
|
505
|
+
if (text === toText(value)) return;
|
|
506
|
+
onCommit(text);
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
function toText(value) {
|
|
511
|
+
if (value == null) return "";
|
|
512
|
+
return String(value);
|
|
513
|
+
}
|
|
514
|
+
export {
|
|
515
|
+
DataEditor,
|
|
516
|
+
DataEditorField,
|
|
517
|
+
DataEditorFields,
|
|
518
|
+
DataEditorFrame,
|
|
519
|
+
FieldValue,
|
|
520
|
+
MixedValues,
|
|
521
|
+
sharedValue,
|
|
522
|
+
valueToken
|
|
523
|
+
};
|
|
524
|
+
//# sourceMappingURL=data-editor.js.map
|