1
1
import {
2
+ Autocomplete ,
2
3
Card ,
3
4
CardContent ,
4
5
CardHeader ,
@@ -15,17 +16,22 @@ import {
15
16
type SectionInterfaceNode ,
16
17
type SectionOutputNode ,
17
18
} 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' ;
18
24
19
- export interface BaseEditSectionInterfaceFormProps {
25
+ export interface BaseSectionInterfaceFormProps {
20
26
node : SectionInterfaceNode ;
21
27
onDelete ?: ( change : NodeRemoveChange ) => void ;
22
28
}
23
29
24
- function BaseEditSectionInterfaceForm ( {
30
+ function BaseSectionInterfaceForm ( {
25
31
node,
26
32
onDelete,
27
33
children,
28
- } : PropsWithChildren < BaseEditSectionInterfaceFormProps > ) {
34
+ } : PropsWithChildren < BaseSectionInterfaceFormProps > ) {
29
35
return (
30
36
< Card >
31
37
< CardHeader
@@ -44,19 +50,18 @@ function BaseEditSectionInterfaceForm({
44
50
) ;
45
51
}
46
52
47
- export interface EditSectionInputFormProps
48
- extends BaseEditSectionInterfaceFormProps {
53
+ export interface SectionInputFormProps extends BaseSectionInterfaceFormProps {
49
54
node : SectionInputNode ;
50
55
onChange ?: ( change : NodeChange < SectionInputNode > ) => void ;
51
56
}
52
57
53
- export function EditSectionInputForm ( {
58
+ export function SectionInputForm ( {
54
59
node,
55
60
onChange,
56
61
onDelete,
57
- } : EditSectionInputFormProps ) {
62
+ } : SectionInputFormProps ) {
58
63
return (
59
- < BaseEditSectionInterfaceForm node = { node } onDelete = { onDelete } >
64
+ < BaseSectionInterfaceForm node = { node } onDelete = { onDelete } >
60
65
< Stack spacing = { 2 } >
61
66
< TextField
62
67
required
@@ -73,23 +78,22 @@ export function EditSectionInputForm({
73
78
} }
74
79
/>
75
80
</ Stack >
76
- </ BaseEditSectionInterfaceForm >
81
+ </ BaseSectionInterfaceForm >
77
82
) ;
78
83
}
79
84
80
- export interface EditSectionBufferFormProps
81
- extends BaseEditSectionInterfaceFormProps {
85
+ export interface SectionBufferFormProps extends BaseSectionInterfaceFormProps {
82
86
node : SectionBufferNode ;
83
87
onChange ?: ( change : NodeChange < SectionBufferNode > ) => void ;
84
88
}
85
89
86
- export function EditSectionBufferForm ( {
90
+ export function SectionBufferForm ( {
87
91
node,
88
92
onChange,
89
93
onDelete,
90
- } : EditSectionBufferFormProps ) {
94
+ } : SectionBufferFormProps ) {
91
95
return (
92
- < BaseEditSectionInterfaceForm node = { node } onDelete = { onDelete } >
96
+ < BaseSectionInterfaceForm node = { node } onDelete = { onDelete } >
93
97
< Stack spacing = { 2 } >
94
98
< TextField
95
99
required
@@ -106,23 +110,22 @@ export function EditSectionBufferForm({
106
110
} }
107
111
/>
108
112
</ Stack >
109
- </ BaseEditSectionInterfaceForm >
113
+ </ BaseSectionInterfaceForm >
110
114
) ;
111
115
}
112
116
113
- export interface EditSectionOutputFormProps
114
- extends BaseEditSectionInterfaceFormProps {
117
+ export interface SectionOutputFormProps extends BaseSectionInterfaceFormProps {
115
118
node : SectionOutputNode ;
116
119
onChange ?: ( change : NodeChange < SectionOutputNode > ) => void ;
117
120
}
118
121
119
- export function EditSectionOutputForm ( {
122
+ export function SectionOutputForm ( {
120
123
node,
121
124
onChange,
122
125
onDelete,
123
- } : EditSectionOutputFormProps ) {
126
+ } : SectionOutputFormProps ) {
124
127
return (
125
- < BaseEditSectionInterfaceForm node = { node } onDelete = { onDelete } >
128
+ < BaseSectionInterfaceForm node = { node } onDelete = { onDelete } >
126
129
< Stack spacing = { 2 } >
127
130
< TextField
128
131
required
@@ -139,6 +142,58 @@ export function EditSectionOutputForm({
139
142
} }
140
143
/>
141
144
</ 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 >
143
198
) ;
144
199
}
0 commit comments