Skip to content

Commit 1b72d11

Browse files
authored
feat(designable-antd): support restrictSiblingsComponents (#1984)
1 parent 6162639 commit 1b72d11

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

designable/antd/src/components/DesignableField/index.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export const createDesignableField = (
209209
return base
210210
}
211211

212-
const calculateRestricts = (target: TreeNode, source: TreeNode[]) => {
212+
const calculateChildrenRestricts = (target: TreeNode, source: TreeNode[]) => {
213213
const targetComponent = target.props['x-component']
214214
const restrictChildrenComponents =
215215
realOptions.restrictChildrenComponents?.[targetComponent]
@@ -226,6 +226,23 @@ export const createDesignableField = (
226226
return true
227227
}
228228

229+
const calculateSiblingsRestricts = (target: TreeNode, source: TreeNode[]) => {
230+
const targetComponent = target.props['x-component']
231+
const restrictSiblingComponents =
232+
realOptions.restrictSiblingComponents?.[targetComponent]
233+
if (restrictSiblingComponents?.length) {
234+
if (
235+
source.every((node) =>
236+
includesComponent(node, restrictSiblingComponents, target)
237+
)
238+
) {
239+
return true
240+
}
241+
return false
242+
}
243+
return true
244+
}
245+
229246
if (!realOptions.registryName) throw new Error('Can not found registryName')
230247

231248
GlobalRegistry.registerDesignerProps({
@@ -260,12 +277,15 @@ export const createDesignableField = (
260277
node,
261278
realOptions.inlineChildrenLayoutComponents
262279
),
280+
allowSiblings(target, source) {
281+
return calculateSiblingsRestricts(target, source)
282+
},
263283
allowAppend(target, source) {
264284
return (
265285
(target.props.type === 'void' ||
266286
target.props.type === 'array' ||
267287
target.props.type === 'object') &&
268-
calculateRestricts(target, source)
288+
calculateChildrenRestricts(target, source)
269289
)
270290
},
271291
propsSchema: getFieldPropsSchema(node),

designable/antd/src/components/DesignableField/options.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,20 @@ export const createOptions = (
101101
isArrayTableInlineChildren,
102102
isArrayCardsInlineChildren,
103103
],
104-
105104
restrictChildrenComponents: {
106105
FormTab: [allowDropWithEmpty, 'FormTab.TabPane'],
107106
FormCollapse: [allowDropWithEmpty, 'FormCollapse.CollapsePanel'],
108107
ArrayTable: [allowDropWithEmpty, isObjectNode, 'ArrayTable.Addition'],
109108
'ArrayTable.Column': [isNotArrayColumn],
110109
},
110+
restrictSiblingComponents: {
111+
'FormTab.TabPane': ['FormTab.TabPane'],
112+
'FormCollapse.CollapsePanel': [
113+
allowDropWithEmpty,
114+
'FormCollapse.CollapsePanel',
115+
],
116+
'ArrayTable.Column': ['ArrayTable.Column'],
117+
},
111118
components: {
112119
...options.components,
113120
Space: createDesignableContainer(Space),

designable/antd/src/components/DesignableField/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface IDesignableFieldFactoryProps {
1010
inlineChildrenLayoutComponents?: ComponentNameMatcher[]
1111
inlineLayoutComponents?: ComponentNameMatcher[]
1212
restrictChildrenComponents?: Record<string, ComponentNameMatcher[]>
13+
restrictSiblingComponents?: Record<string, ComponentNameMatcher[]>
1314
}

docs/guide/form-builder.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ interface IDesignableFieldFactoryProps {
213213
inlineChildrenLayoutComponents?: ComponentNameMatcher[] //Identify which canvas component's child component layout mode is inline mode
214214
inlineLayoutComponents?: ComponentNameMatcher[] //Identify which canvas components are in inline mode
215215
restrictChildrenComponents?: Record<string, ComponentNameMatcher[]> //Node constraints, identify the upper and lower constraints between canvas components, for example, the child nodes of A component can only be B/C components
216+
restrictSiblingComponents?: Record<string, ComponentNameMatcher[]> //Node constraint, identifies the adjacent constraint relationship of canvas components, for example, adjacent nodes of component A can only be B/C components
216217
}
217218

218219
interface createDesignableField {

docs/guide/form-builder.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ interface IDesignableFieldFactoryProps {
213213
inlineChildrenLayoutComponents?: ComponentNameMatcher[] //标识哪些画布组件的子组件布局模式是内联模式
214214
inlineLayoutComponents?: ComponentNameMatcher[] //标识哪些画布组件本身是内联模式
215215
restrictChildrenComponents?: Record<string, ComponentNameMatcher[]> //节点约束,标识画布组件之间的上下级约束关系,比如A组件的子节点只能是B/C组件
216+
restrictSiblingComponents?: Record<string, ComponentNameMatcher[]> //节点约束,标识画布组件相邻约束关系,比如A组件的相邻节点只能是B/C组件
216217
}
217218

218219
interface createDesignableField {

0 commit comments

Comments
 (0)