@longhongguo/form-create-ant-design-vue 3.3.35 → 3.3.36

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.
@@ -1,264 +1,255 @@
1
- export default {
2
- name: 'flex',
3
- mergeProp(ctx) {
4
- // 从 props 中读取 flex 布局配置
5
- const props = ctx.rule.props || {}
6
- const flexDirection = props.flexDirection || ctx.rule.flexDirection || 'row'
7
- const flexWrap = props.flexWrap || ctx.rule.flexWrap || 'nowrap'
8
- const justifyContent =
9
- props.justifyContent || ctx.rule.justifyContent || 'flex-start'
10
- const alignItems = props.alignItems || ctx.rule.alignItems || 'flex-start'
11
- const alignContent =
12
- props.alignContent || ctx.rule.alignContent || 'flex-start'
13
-
14
- // 映射 CSS flexbox 值到 Ant Design Vue Flex 组件的 props
15
- const vertical =
16
- flexDirection === 'column' || flexDirection === 'column-reverse'
17
- const wrap = flexWrap === 'wrap'
18
-
19
- // 映射 justifyContent 到 justify
20
- const justifyMap = {
21
- 'flex-start': 'start',
22
- 'flex-end': 'end',
23
- center: 'center',
24
- 'space-between': 'space-between',
25
- 'space-around': 'space-around',
26
- 'space-evenly': 'space-evenly'
27
- }
28
- const justify = justifyMap[justifyContent] || 'start'
29
-
30
- // 映射 alignItems 到 align
31
- const alignMap = {
32
- 'flex-start': 'start',
33
- 'flex-end': 'end',
34
- center: 'center',
35
- baseline: 'baseline',
36
- stretch: 'stretch'
37
- }
38
- const align = alignMap[alignItems] || 'start'
39
-
40
- // 设置到 prop.props,用于传递给 a-flex 组件
41
- if (!ctx.prop.props) {
42
- ctx.prop.props = {}
43
- }
44
-
45
- // 映射 Ant Design Vue Flex 组件的 props
46
- ctx.prop.props.vertical = vertical
47
- // 注意:不设置 wrap prop,因为 Ant Design Vue 的 Flex 组件的 wrap prop 类型可能不匹配
48
- // 换行功能完全通过 CSS 样式来实现
49
- ctx.prop.props.justify = justify
50
- ctx.prop.props.align = align
51
-
52
- // 如果设置了 gap,也传递
53
- if (props.gap !== undefined) {
54
- ctx.prop.props.gap = props.gap
55
- }
56
-
57
- // 合并样式(不要直接修改 ctx.rule.style,避免响应式循环)
58
- const existingStyle = ctx.rule.style || {}
59
- // 排除已经被 Flex 组件处理的样式
60
- const {
61
- display: userDisplay,
62
- flexDirection: _fd,
63
- flexWrap: _fw,
64
- justifyContent: _jc,
65
- alignItems: _ai,
66
- alignContent: _ac,
67
- ...otherStyles
68
- } = existingStyle
69
-
70
- // 构建样式对象,确保从 props 读取的配置能够应用到样式中
71
- const flexStyles = {
72
- // 保留用户设置的 display,如果没有则设置为 'flex'
73
- display: userDisplay || 'flex',
74
- // 确保 flex 容器占满整行
75
- // 在样式中设置 flexWrap,确保从 props 读取的配置能够生效
76
- // 这是关键的:将 props.flexWrap 的值('wrap' 或 'nowrap')应用到样式中
77
- flexWrap: flexWrap,
78
- // 合并其他用户自定义样式
79
- ...otherStyles
80
- }
81
-
82
- // 如果设置了 alignContent,添加到样式中
83
- if (alignContent && alignContent !== 'flex-start') {
84
- flexStyles.alignContent = alignContent
85
- }
86
-
87
- // 同时设置到 ctx.prop.style 和 ctx.prop.props.style
88
- // form-create 会将 prop.style 应用到组件上,但有时也需要 prop.props.style
89
- // 保留已有的 prop.style 和 prop.props.style,然后合并新的样式
90
- const existingPropStyle = ctx.prop.style || {}
91
- const existingPropsStyle = ctx.prop.props?.style || {}
92
-
93
- // 合并样式到 ctx.prop.style(保留已有样式,新的样式优先)
94
- ctx.prop.style = {
95
- ...existingPropStyle,
96
- ...flexStyles
97
- }
98
-
99
- // 同时也合并到 props.style,确保样式生效
100
- if (!ctx.prop.props) {
101
- ctx.prop.props = {}
102
- }
103
- ctx.prop.props.style = {
104
- ...existingPropsStyle,
105
- ...flexStyles
106
- }
107
-
108
- // componentStyle 应用到 prop.style(包裹容器),最后合并确保优先级最高
109
- if (ctx.rule.componentStyle && typeof ctx.rule.componentStyle === 'object') {
110
- ctx.prop.style = {
111
- ...ctx.prop.style,
112
- ...ctx.rule.componentStyle
113
- }
114
- }
115
-
116
- // 确保 children 存在
117
- if (!ctx.rule.children) {
118
- ctx.rule.children = []
119
- }
120
-
121
- // 为 flex 容器的所有子组件设置 col: false,避免被 a-col 包装
122
- // 这样可以确保子组件直接作为 flex 子项,而不是占满整行
123
- if (ctx.rule.children && Array.isArray(ctx.rule.children)) {
124
- ctx.rule.children.forEach((child) => {
125
- if (
126
- child &&
127
- typeof child === 'object' &&
128
- !child.type === 'DragTool' &&
129
- !child.type === 'DragBox'
130
- ) {
131
- // 只有当 col 未设置或为默认值时才设置
132
- if (child.col === undefined || child.col === null) {
133
- child.col = false
134
- } else if (
135
- child.col &&
136
- typeof child.col === 'object' &&
137
- child.col.show !== false
138
- ) {
139
- child.col.show = false
140
- }
141
- }
142
- })
143
- }
144
-
145
- // 读取 resetMarginBottom 配置,用于重置包裹 flex 的 ant-form-item 的 margin-bottom
146
- const resetMarginBottom =
147
- props.resetMarginBottom ?? ctx.rule.resetMarginBottom ?? false
148
- if (resetMarginBottom) {
149
- // 将 class 添加到 wrap 配置中,使其应用到包裹 flex 的 ant-form-item 上
150
- if (!ctx.rule.wrap) {
151
- ctx.rule.wrap = {}
152
- }
153
- if (typeof ctx.rule.wrap === 'object' && !ctx.rule.wrap.class) {
154
- ctx.rule.wrap.class = ''
155
- }
156
- const wrapClass =
157
- typeof ctx.rule.wrap === 'object'
158
- ? (ctx.rule.wrap.class || '').split(' ').filter(Boolean)
159
- : []
160
- if (!wrapClass.includes('_fc-reset-margin-bottom')) {
161
- wrapClass.push('_fc-reset-margin-bottom')
162
- if (typeof ctx.rule.wrap === 'object') {
163
- ctx.rule.wrap.class = wrapClass.join(' ')
164
- }
165
- }
166
- }
167
- },
168
- render(children, ctx) {
169
- // 使用 Ant Design Vue 的 Flex 组件
170
- const prop = { ...ctx.prop }
171
-
172
- // type 设置为 'a-flex',form-create 会自动识别
173
- if (prop.type === 'flex') {
174
- prop.type = 'a-flex'
175
- }
176
-
177
- // 读取 flexDirection 来判断是否是垂直方向
178
- const props = ctx.rule.props || {}
179
- const flexDirection = props.flexDirection || ctx.rule.flexDirection || 'row'
180
- const isVertical =
181
- flexDirection === 'column' || flexDirection === 'column-reverse'
182
-
183
- // 初始化 prop.props 和 prop.class(如果需要的话)
184
- if (!prop.props) {
185
- prop.props = {}
186
- }
187
-
188
- // 添加自定义 class 的辅助函数
189
- const ensureClass = () => {
190
- if (!prop.class) {
191
- prop.class = []
192
- }
193
- if (!Array.isArray(prop.class)) {
194
- prop.class = [prop.class]
195
- }
196
- }
197
-
198
- // 读取 childFlex 配置
199
- const childFlex = ctx.rule.props?.childFlex ?? ctx.rule.childFlex
200
- if (childFlex !== undefined && childFlex !== null && childFlex !== '') {
201
- ensureClass()
202
- if (!prop.class.includes('_fc-flex-container')) {
203
- prop.class.push('_fc-flex-container')
204
- }
205
-
206
- // 通过 CSS 变量传递 flex 值
207
- const flexValue = String(childFlex).trim()
208
- // 合并到样式中,设置 CSS 变量
209
- if (!prop.props.style) {
210
- prop.props.style = {}
211
- }
212
- prop.props.style['--fc-child-flex'] = flexValue
213
-
214
- // 同时也设置到 prop.style
215
- if (!prop.style) {
216
- prop.style = {}
217
- }
218
- prop.style['--fc-child-flex'] = flexValue
219
- }
220
-
221
- // 如果是垂直方向,读取 childWidth 配置(默认 100%)
222
- if (isVertical) {
223
- const childWidth =
224
- ctx.rule.props?.childWidth ?? ctx.rule.childWidth ?? '100%'
225
-
226
- ensureClass()
227
- if (!prop.class.includes('_fc-flex-container')) {
228
- prop.class.push('_fc-flex-container')
229
- }
230
- if (!prop.class.includes('_fc-flex-vertical')) {
231
- prop.class.push('_fc-flex-vertical')
232
- }
233
-
234
- // 通过 CSS 变量传递宽度值
235
- const widthValue = String(childWidth).trim()
236
- if (!prop.props.style) {
237
- prop.props.style = {}
238
- }
239
- prop.props.style['--fc-child-width'] = widthValue
240
-
241
- if (!prop.style) {
242
- prop.style = {}
243
- }
244
- prop.style['--fc-child-width'] = widthValue
245
- }
246
-
247
- // children 会通过 form-create 的机制自动渲染
248
- const childrenNodes = children || []
249
-
250
- // 将 flex 容器包装在 col 中,确保它占满整行(span: 24)
251
- // 这样 flex 容器才能有足够的宽度,子项才能自适应展示
252
- // 应用 componentStyle 到 col 容器(如果存在)
253
- const componentStyle = ctx.rule.componentStyle || {}
254
- return ctx.vNode.col(
255
- {
256
- props: { span: 24 },
257
- style: componentStyle
258
- },
259
- {
260
- default: () => [ctx.vNode.make('a-flex', prop, childrenNodes)]
261
- }
262
- )
263
- }
264
- }
1
+ export default {
2
+ name: 'flex',
3
+ mergeProp(ctx) {
4
+ // 从 props 中读取 flex 布局配置
5
+ const props = ctx.rule.props || {}
6
+ const flexDirection = props.flexDirection || ctx.rule.flexDirection || 'row'
7
+ const flexWrap = props.flexWrap || ctx.rule.flexWrap || 'nowrap'
8
+ const justifyContent =
9
+ props.justifyContent || ctx.rule.justifyContent || 'flex-start'
10
+ const alignItems = props.alignItems || ctx.rule.alignItems || 'flex-start'
11
+ const alignContent =
12
+ props.alignContent || ctx.rule.alignContent || 'flex-start'
13
+
14
+ // 映射 CSS flexbox 值到 Ant Design Vue Flex 组件的 props
15
+ const vertical =
16
+ flexDirection === 'column' || flexDirection === 'column-reverse'
17
+ const wrap = flexWrap === 'wrap'
18
+
19
+ // 映射 justifyContent 到 justify
20
+ const justifyMap = {
21
+ 'flex-start': 'start',
22
+ 'flex-end': 'end',
23
+ center: 'center',
24
+ 'space-between': 'space-between',
25
+ 'space-around': 'space-around',
26
+ 'space-evenly': 'space-evenly'
27
+ }
28
+ const justify = justifyMap[justifyContent] || 'start'
29
+
30
+ // 映射 alignItems 到 align
31
+ const alignMap = {
32
+ 'flex-start': 'start',
33
+ 'flex-end': 'end',
34
+ center: 'center',
35
+ baseline: 'baseline',
36
+ stretch: 'stretch'
37
+ }
38
+ const align = alignMap[alignItems] || 'start'
39
+
40
+ // 设置到 prop.props,用于传递给 a-flex 组件
41
+ if (!ctx.prop.props) {
42
+ ctx.prop.props = {}
43
+ }
44
+
45
+ // 映射 Ant Design Vue Flex 组件的 props
46
+ ctx.prop.props.vertical = vertical
47
+ // 注意:不设置 wrap prop,因为 Ant Design Vue 的 Flex 组件的 wrap prop 类型可能不匹配
48
+ // 换行功能完全通过 CSS 样式来实现
49
+ ctx.prop.props.justify = justify
50
+ ctx.prop.props.align = align
51
+
52
+ // 如果设置了 gap,也传递
53
+ if (props.gap !== undefined) {
54
+ ctx.prop.props.gap = props.gap
55
+ }
56
+
57
+ // 合并样式(不要直接修改 ctx.rule.style,避免响应式循环)
58
+ const existingStyle = ctx.rule.style || {}
59
+ // 排除已经被 Flex 组件处理的样式
60
+ const {
61
+ display: userDisplay,
62
+ flexDirection: _fd,
63
+ flexWrap: _fw,
64
+ justifyContent: _jc,
65
+ alignItems: _ai,
66
+ alignContent: _ac,
67
+ ...otherStyles
68
+ } = existingStyle
69
+
70
+ // 构建样式对象,确保从 props 读取的配置能够应用到样式中
71
+ const flexStyles = {
72
+ // 保留用户设置的 display,如果没有则设置为 'flex'
73
+ display: userDisplay || 'flex',
74
+ // 确保 flex 容器占满整行
75
+ // 在样式中设置 flexWrap,确保从 props 读取的配置能够生效
76
+ // 这是关键的:将 props.flexWrap 的值('wrap' 或 'nowrap')应用到样式中
77
+ flexWrap: flexWrap,
78
+ // 合并其他用户自定义样式
79
+ ...otherStyles
80
+ }
81
+
82
+ // 如果设置了 alignContent,添加到样式中
83
+ if (alignContent && alignContent !== 'flex-start') {
84
+ flexStyles.alignContent = alignContent
85
+ }
86
+
87
+ // 同时设置到 ctx.prop.style 和 ctx.prop.props.style
88
+ // form-create 会将 prop.style 应用到组件上,但有时也需要 prop.props.style
89
+ // 保留已有的 prop.style 和 prop.props.style,然后合并新的样式
90
+ const existingPropStyle = ctx.prop.style || {}
91
+ const existingPropsStyle = ctx.prop.props?.style || {}
92
+
93
+ // 合并样式到 ctx.prop.style(保留已有样式,新的样式优先)
94
+ // 注意:这里不合并 componentStyle,componentStyle 是独立的,用于顶级容器
95
+ ctx.prop.style = {
96
+ ...existingPropStyle,
97
+ ...flexStyles
98
+ }
99
+
100
+ // componentStyle 独立应用到顶级容器,不在这里合并,在 manager.js 中处理
101
+
102
+ // 同时也合并到 props.style,确保样式生效
103
+ if (!ctx.prop.props) {
104
+ ctx.prop.props = {}
105
+ }
106
+ ctx.prop.props.style = {
107
+ ...existingPropsStyle,
108
+ ...flexStyles
109
+ }
110
+
111
+ // 确保 children 存在
112
+ if (!ctx.rule.children) {
113
+ ctx.rule.children = []
114
+ }
115
+
116
+ // flex 容器的所有子组件设置 col: false,避免被 a-col 包装
117
+ // 这样可以确保子组件直接作为 flex 子项,而不是占满整行
118
+ if (ctx.rule.children && Array.isArray(ctx.rule.children)) {
119
+ ctx.rule.children.forEach((child) => {
120
+ if (
121
+ child &&
122
+ typeof child === 'object' &&
123
+ !child.type === 'DragTool' &&
124
+ !child.type === 'DragBox'
125
+ ) {
126
+ // 只有当 col 未设置或为默认值时才设置
127
+ if (child.col === undefined || child.col === null) {
128
+ child.col = false
129
+ } else if (
130
+ child.col &&
131
+ typeof child.col === 'object' &&
132
+ child.col.show !== false
133
+ ) {
134
+ child.col.show = false
135
+ }
136
+ }
137
+ })
138
+ }
139
+
140
+ // 读取 resetMarginBottom 配置,用于重置包裹 flex 的 ant-form-item 的 margin-bottom
141
+ const resetMarginBottom =
142
+ props.resetMarginBottom ?? ctx.rule.resetMarginBottom ?? false
143
+ if (resetMarginBottom) {
144
+ // 将 class 添加到 wrap 配置中,使其应用到包裹 flex 的 ant-form-item 上
145
+ if (!ctx.rule.wrap) {
146
+ ctx.rule.wrap = {}
147
+ }
148
+ if (typeof ctx.rule.wrap === 'object' && !ctx.rule.wrap.class) {
149
+ ctx.rule.wrap.class = ''
150
+ }
151
+ const wrapClass =
152
+ typeof ctx.rule.wrap === 'object'
153
+ ? (ctx.rule.wrap.class || '').split(' ').filter(Boolean)
154
+ : []
155
+ if (!wrapClass.includes('_fc-reset-margin-bottom')) {
156
+ wrapClass.push('_fc-reset-margin-bottom')
157
+ if (typeof ctx.rule.wrap === 'object') {
158
+ ctx.rule.wrap.class = wrapClass.join(' ')
159
+ }
160
+ }
161
+ }
162
+ },
163
+ render(children, ctx) {
164
+ // 使用 Ant Design Vue 的 Flex 组件
165
+ const prop = { ...ctx.prop }
166
+
167
+ // 将 type 设置为 'a-flex',form-create 会自动识别
168
+ if (prop.type === 'flex') {
169
+ prop.type = 'a-flex'
170
+ }
171
+
172
+ // 读取 flexDirection 来判断是否是垂直方向
173
+ const props = ctx.rule.props || {}
174
+ const flexDirection = props.flexDirection || ctx.rule.flexDirection || 'row'
175
+ const isVertical =
176
+ flexDirection === 'column' || flexDirection === 'column-reverse'
177
+
178
+ // 初始化 prop.props prop.class(如果需要的话)
179
+ if (!prop.props) {
180
+ prop.props = {}
181
+ }
182
+
183
+ // 添加自定义 class 的辅助函数
184
+ const ensureClass = () => {
185
+ if (!prop.class) {
186
+ prop.class = []
187
+ }
188
+ if (!Array.isArray(prop.class)) {
189
+ prop.class = [prop.class]
190
+ }
191
+ }
192
+
193
+ // 读取 childFlex 配置
194
+ const childFlex = ctx.rule.props?.childFlex ?? ctx.rule.childFlex
195
+ if (childFlex !== undefined && childFlex !== null && childFlex !== '') {
196
+ ensureClass()
197
+ if (!prop.class.includes('_fc-flex-container')) {
198
+ prop.class.push('_fc-flex-container')
199
+ }
200
+
201
+ // 通过 CSS 变量传递 flex 值
202
+ const flexValue = String(childFlex).trim()
203
+ // 合并到样式中,设置 CSS 变量
204
+ if (!prop.props.style) {
205
+ prop.props.style = {}
206
+ }
207
+ prop.props.style['--fc-child-flex'] = flexValue
208
+
209
+ // 同时也设置到 prop.style
210
+ if (!prop.style) {
211
+ prop.style = {}
212
+ }
213
+ prop.style['--fc-child-flex'] = flexValue
214
+ }
215
+
216
+ // 如果是垂直方向,读取 childWidth 配置(默认 100%)
217
+ if (isVertical) {
218
+ const childWidth =
219
+ ctx.rule.props?.childWidth ?? ctx.rule.childWidth ?? '100%'
220
+
221
+ ensureClass()
222
+ if (!prop.class.includes('_fc-flex-container')) {
223
+ prop.class.push('_fc-flex-container')
224
+ }
225
+ if (!prop.class.includes('_fc-flex-vertical')) {
226
+ prop.class.push('_fc-flex-vertical')
227
+ }
228
+
229
+ // 通过 CSS 变量传递宽度值
230
+ const widthValue = String(childWidth).trim()
231
+ if (!prop.props.style) {
232
+ prop.props.style = {}
233
+ }
234
+ prop.props.style['--fc-child-width'] = widthValue
235
+
236
+ if (!prop.style) {
237
+ prop.style = {}
238
+ }
239
+ prop.style['--fc-child-width'] = widthValue
240
+ }
241
+
242
+ // children 会通过 form-create 的机制自动渲染
243
+ const childrenNodes = children || []
244
+
245
+ // 将 flex 容器包装在 col 中,确保它占满整行(span: 24)
246
+ // 这样 flex 容器才能有足够的宽度,子项才能自适应展示
247
+ // componentStyle 会在 manager.js 中应用到 col 容器
248
+ return ctx.vNode.col(
249
+ { props: { span: 24 } },
250
+ {
251
+ default: () => [ctx.vNode.make('a-flex', prop, childrenNodes)]
252
+ }
253
+ )
254
+ }
255
+ }