Skip to content

Commit 5ab12f1

Browse files
committed
form to edit a section
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent d2e5294 commit 5ab12f1

File tree

3 files changed

+101
-27
lines changed

3 files changed

+101
-27
lines changed

diagram-editor/frontend/add-operation.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ScopeIcon,
2626
SectionBufferIcon,
2727
type SectionBufferNode,
28+
SectionIcon,
2829
SectionInputIcon,
2930
type SectionInputNode,
3031
SectionOutputIcon,
@@ -322,6 +323,19 @@ function AddOperation({ parentId, newNodePosition, onAdd }: AddOperationProps) {
322323
>
323324
Scope
324325
</StyledOperationButton>
326+
<StyledOperationButton
327+
startIcon={<SectionIcon />}
328+
onClick={() =>
329+
onAdd?.(
330+
createNodeChange(namespace, parentId, newNodePosition, {
331+
type: 'section',
332+
template: 'new_section',
333+
}),
334+
)
335+
}
336+
>
337+
Scope
338+
</StyledOperationButton>
325339
</ButtonGroup>
326340
);
327341
}

diagram-editor/frontend/forms/edit-node-form.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import BufferForm, { type BufferFormProps } from './buffer-form';
1111
import EditScopeForm, { type ScopeFormProps } from './edit-scope-form';
1212
import NodeForm, { type NodeFormProps } from './node-form';
1313
import {
14-
EditSectionBufferForm,
15-
EditSectionInputForm,
16-
EditSectionOutputForm,
14+
SectionBufferForm,
15+
SectionForm,
16+
type SectionFormProps,
17+
SectionInputForm,
18+
SectionOutputForm,
1719
} from './section-form';
1820
import TransformForm, { type TransformFormProps } from './transform-form';
1921

@@ -34,6 +36,9 @@ function EditOperationNodeForm(props: EditOperationNodeFormProps) {
3436
case 'scope': {
3537
return <EditScopeForm {...(props as ScopeFormProps)} />;
3638
}
39+
case 'section': {
40+
return <SectionForm {...(props as SectionFormProps)} />;
41+
}
3742
case 'transform': {
3843
return <TransformForm {...(props as TransformFormProps)} />;
3944
}
@@ -55,11 +60,11 @@ function EditNodeForm(props: EditNodeFormProps) {
5560
} else if (isSectionInterfaceNode(props.node)) {
5661
switch (props.node.type) {
5762
case 'sectionInput':
58-
return <EditSectionInputForm {...props} node={props.node} />;
63+
return <SectionInputForm {...props} node={props.node} />;
5964
case 'sectionOutput':
60-
return <EditSectionOutputForm {...props} node={props.node} />;
65+
return <SectionOutputForm {...props} node={props.node} />;
6166
case 'sectionBuffer':
62-
return <EditSectionBufferForm {...props} node={props.node} />;
67+
return <SectionBufferForm {...props} node={props.node} />;
6368
default:
6469
exhaustiveCheck(props.node);
6570
throw new Error('unknown node type');

diagram-editor/frontend/forms/section-form.tsx

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Autocomplete,
23
Card,
34
CardContent,
45
CardHeader,
@@ -15,17 +16,22 @@ import {
1516
type SectionInterfaceNode,
1617
type SectionOutputNode,
1718
} from '../nodes';
19+
import { useRegistry } from '../registry-provider';
20+
import { useTemplates } from '../templates-provider';
21+
import BaseEditOperationForm, {
22+
type BaseEditOperationFormProps,
23+
} from './base-edit-operation-form';
1824

19-
export interface BaseEditSectionInterfaceFormProps {
25+
export interface BaseSectionInterfaceFormProps {
2026
node: SectionInterfaceNode;
2127
onDelete?: (change: NodeRemoveChange) => void;
2228
}
2329

24-
function BaseEditSectionInterfaceForm({
30+
function BaseSectionInterfaceForm({
2531
node,
2632
onDelete,
2733
children,
28-
}: PropsWithChildren<BaseEditSectionInterfaceFormProps>) {
34+
}: PropsWithChildren<BaseSectionInterfaceFormProps>) {
2935
return (
3036
<Card>
3137
<CardHeader
@@ -44,19 +50,18 @@ function BaseEditSectionInterfaceForm({
4450
);
4551
}
4652

47-
export interface EditSectionInputFormProps
48-
extends BaseEditSectionInterfaceFormProps {
53+
export interface SectionInputFormProps extends BaseSectionInterfaceFormProps {
4954
node: SectionInputNode;
5055
onChange?: (change: NodeChange<SectionInputNode>) => void;
5156
}
5257

53-
export function EditSectionInputForm({
58+
export function SectionInputForm({
5459
node,
5560
onChange,
5661
onDelete,
57-
}: EditSectionInputFormProps) {
62+
}: SectionInputFormProps) {
5863
return (
59-
<BaseEditSectionInterfaceForm node={node} onDelete={onDelete}>
64+
<BaseSectionInterfaceForm node={node} onDelete={onDelete}>
6065
<Stack spacing={2}>
6166
<TextField
6267
required
@@ -73,23 +78,22 @@ export function EditSectionInputForm({
7378
}}
7479
/>
7580
</Stack>
76-
</BaseEditSectionInterfaceForm>
81+
</BaseSectionInterfaceForm>
7782
);
7883
}
7984

80-
export interface EditSectionBufferFormProps
81-
extends BaseEditSectionInterfaceFormProps {
85+
export interface SectionBufferFormProps extends BaseSectionInterfaceFormProps {
8286
node: SectionBufferNode;
8387
onChange?: (change: NodeChange<SectionBufferNode>) => void;
8488
}
8589

86-
export function EditSectionBufferForm({
90+
export function SectionBufferForm({
8791
node,
8892
onChange,
8993
onDelete,
90-
}: EditSectionBufferFormProps) {
94+
}: SectionBufferFormProps) {
9195
return (
92-
<BaseEditSectionInterfaceForm node={node} onDelete={onDelete}>
96+
<BaseSectionInterfaceForm node={node} onDelete={onDelete}>
9397
<Stack spacing={2}>
9498
<TextField
9599
required
@@ -106,23 +110,22 @@ export function EditSectionBufferForm({
106110
}}
107111
/>
108112
</Stack>
109-
</BaseEditSectionInterfaceForm>
113+
</BaseSectionInterfaceForm>
110114
);
111115
}
112116

113-
export interface EditSectionOutputFormProps
114-
extends BaseEditSectionInterfaceFormProps {
117+
export interface SectionOutputFormProps extends BaseSectionInterfaceFormProps {
115118
node: SectionOutputNode;
116119
onChange?: (change: NodeChange<SectionOutputNode>) => void;
117120
}
118121

119-
export function EditSectionOutputForm({
122+
export function SectionOutputForm({
120123
node,
121124
onChange,
122125
onDelete,
123-
}: EditSectionOutputFormProps) {
126+
}: SectionOutputFormProps) {
124127
return (
125-
<BaseEditSectionInterfaceForm node={node} onDelete={onDelete}>
128+
<BaseSectionInterfaceForm node={node} onDelete={onDelete}>
126129
<Stack spacing={2}>
127130
<TextField
128131
required
@@ -139,6 +142,58 @@ export function EditSectionOutputForm({
139142
}}
140143
/>
141144
</Stack>
142-
</BaseEditSectionInterfaceForm>
145+
</BaseSectionInterfaceForm>
146+
);
147+
}
148+
149+
export type SectionFormProps = BaseEditOperationFormProps<'section'>;
150+
151+
export function SectionForm(props: SectionFormProps) {
152+
const registry = useRegistry();
153+
const [templates, _setTemplates] = useTemplates();
154+
const sectionBuilders = Object.keys(registry.sections);
155+
const sectionTemplates = Object.keys(templates);
156+
const sections = [...sectionBuilders, ...sectionTemplates].sort();
157+
158+
return (
159+
<BaseEditOperationForm {...props}>
160+
<Autocomplete
161+
freeSolo
162+
autoSelect
163+
options={sections}
164+
getOptionLabel={(option) => option}
165+
value={
166+
(props.node.data.op.builder ||
167+
props.node.data.op.template ||
168+
'') as string
169+
}
170+
onChange={(_, value) => {
171+
if (value === null) {
172+
return;
173+
}
174+
175+
const updatedNode = { ...props.node };
176+
if (sectionBuilders.includes(value)) {
177+
updatedNode.data.op.builder = value;
178+
delete updatedNode.data.op.template;
179+
} else if (sectionTemplates.includes(value)) {
180+
updatedNode.data.op.template = value;
181+
delete updatedNode.data.op.builder;
182+
} else {
183+
// unable to determine if selected option is a builder or template, assume template.
184+
updatedNode.data.op.template = value;
185+
delete updatedNode.data.op.builder;
186+
}
187+
props.onChange?.({
188+
type: 'replace',
189+
id: props.node.id,
190+
item: updatedNode,
191+
});
192+
}}
193+
renderInput={(params) => (
194+
<TextField {...params} required label="builder/template" />
195+
)}
196+
/>
197+
</BaseEditOperationForm>
143198
);
144199
}

0 commit comments

Comments
 (0)