Skip to content

Commit c6756e1

Browse files
authored
Merge pull request #1546 from alibaba/widget-extra
feat: 支持配置自定义功能槽
2 parents 8653583 + 938d8bf commit c6756e1

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

docs/form-render/api-schema.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ url: {
5959

6060
### type
6161
- 描述:表单字段的类型
62-
- 类型:`'string' | 'number' | 'boolean' | 'array' | 'range' | 'html' | 'block'`
62+
- 类型:`'string' | 'number' | 'boolean' | 'array' | 'range' | 'html' | 'void'`
6363

6464
### title
6565
- 描述:表单字段的标签
@@ -159,14 +159,12 @@ rules: [
159159
### props
160160
配置额外属性,如果使用的是 antd 组件 对应的就是 antd 组件的其他属性。例如:
161161

162+
163+
### action
164+
- 描述:输入控件支持配置自定义功能槽,显示在输入控件右边,通过 actions 值和 widgets 字段间映射,渲染自定义自定义功能槽。
165+
- 类型
162166
```js
163-
props: {
164-
options: [
165-
lable: '',
166-
value: '',
167-
// disabled: '',
168-
]
169-
}
167+
actions: 'toolWidget' | { widget: 'toolWidget' } // toolWidget 通过 widgets 透传
170168
```
171169

172170
## 三、嵌套控件配置项

packages/form-render/src/render-core/FieldItem/field.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useUpdateEffect } from 'ahooks';
44
import { _get, translation, isFunction } from '../../utils';
55

66
export const FieldWrapperStatus = (props: any) => {
7-
const { Field, fieldProps, maxWidth, initialValue, ...otherProps } = props;
7+
const { Field, fieldProps, maxWidth, initialValue, acitonRender, ...otherProps } = props;
88
const { onStatusChange, addons, ...otherFieldProps } = fieldProps;
99
const style = maxWidth ? { maxWidth, ...fieldProps?.style } : { ...fieldProps?.style } ;
1010

@@ -20,17 +20,24 @@ export const FieldWrapperStatus = (props: any) => {
2020
}, [JSON.stringify(initialValue)]);
2121

2222
return (
23-
<Field
24-
{...otherProps}
25-
{...otherFieldProps}
26-
style={style}
27-
addons={addons}
28-
/>
23+
<>
24+
<Field
25+
{...otherProps}
26+
{...otherFieldProps}
27+
style={style}
28+
addons={addons}
29+
/>
30+
{acitonRender && (
31+
<span className='ant-form-item-actions'>
32+
{acitonRender()}
33+
</span>
34+
)}
35+
</>
2936
);
3037
};
3138

3239
export const FieldWrapper = (props: any) => {
33-
const { Field, fieldProps, maxWidth, initialValue, ...otherProps } = props;
40+
const { Field, fieldProps, maxWidth, initialValue, acitonRender, ...otherProps } = props;
3441
const { addons, schema } = fieldProps;
3542

3643
const _style = maxWidth ? { maxWidth, ...fieldProps?.style }: { ...fieldProps?.style }
@@ -55,7 +62,7 @@ export const FieldWrapper = (props: any) => {
5562

5663
return (
5764
<>
58-
<Field
65+
<Field
5966
{...otherProps}
6067
{...fieldProps}
6168
style={_style}
@@ -70,6 +77,11 @@ export const FieldWrapper = (props: any) => {
7077
{removeBtn?.text || t('delete')}
7178
</Button>
7279
)}
80+
{acitonRender && (
81+
<span className='fr-item-actions'>
82+
{acitonRender()}
83+
</span>
84+
)}
7385
</>
7486
);
7587
}

packages/form-render/src/render-core/FieldItem/main.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ export default (props: any) => {
3939
const upperCtx: any = useContext(UpperContext);
4040

4141
const { form, widgets, methods, globalProps } = configCtx;
42-
const { reserveLabel, hidden, properties, dependencies, inlineMode: _inlineMode, remove, removeText, visible = true, ...otherSchema } = schema;
42+
const {
43+
reserveLabel,
44+
hidden,
45+
properties,
46+
dependencies,
47+
inlineMode: _inlineMode,
48+
remove,
49+
removeText,
50+
visible = true,
51+
...otherSchema
52+
} = schema;
53+
4354
const getValueFromKey = getParamValue(formCtx, upperCtx, schema);
4455

4556
const widgetName = getWidgetName(schema);
@@ -130,8 +141,9 @@ export default (props: any) => {
130141
let noStyle = getValueFromKey('noStyle');
131142

132143
const span = getColSpan(formCtx, upperCtx, schema);
133-
const extra = getExtraView('extra', schema, widgets);
134-
const help = getExtraView('help', schema, widgets);
144+
const extra = getExtraView('extra', schema, widgets, fieldProps.addons);
145+
const help = getExtraView('help', schema, widgets, fieldProps.addons);
146+
const action = getExtraView('action', schema, widgets, fieldProps.addons);
135147

136148
const tooltip = getTooltip(schema, displayType);
137149
const ruleList = getRuleList(schema, form, methods, fieldRef);
@@ -198,13 +210,15 @@ export default (props: any) => {
198210
fieldProps={fieldProps}
199211
maxWidth={maxWidth}
200212
initialValue={initialValue}
213+
acitonRender={() => action}
201214
/>
202215
) : (
203216
<FieldWrapper
204217
Field={Widget}
205218
fieldProps={fieldProps}
206219
maxWidth={maxWidth}
207220
initialValue={initialValue}
221+
acitonRender={() => action}
208222
/>
209223
)}
210224
</Form.Item>

packages/form-render/src/render-core/FieldItem/module.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,27 +165,26 @@ export const getTooltip = (schema: any, displayType: string) => {
165165
return null;
166166
};
167167

168-
export const getExtraView = (extraKey: string, schema: any, widgets: any) => {
168+
export const getExtraView = (extraKey: string, schema: any, widgets: any, addons: any) => {
169169
const extra = schema[extraKey];
170170
if (!extra) {
171171
return;
172172
}
173-
173+
174174
// extra 自定义
175-
const widgetName = extra?.widget;
176-
if (widgetName) {
177-
const Widget = widgets[widgetName];
178-
if (!Widget) {
179-
return;
180-
}
181-
return <Widget schema={schema} />;
175+
const widgetName = extra?.widget || extra;
176+
const Widget = widgets[widgetName];
177+
if (!!Widget) {
178+
return <Widget schema={schema} addons={addons} />;
179+
} if (!Widget && extra?.widget) {
180+
return;
182181
}
183182

184-
185183
let __html = '';
186184
if (typeof extra === 'string') {
187185
__html = extra;
188186
}
187+
189188
// 内部BU使用的口子,这个api不对外,也没有必要
190189
if (extra?.text) {
191190
__html = extra.text;

packages/form-render/src/render-core/index.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@
2828
margin-left: 5px;
2929
font-weight: normal;
3030
font-size: 14px;
31+
}
32+
33+
.fr-item-actions {
34+
margin-left: 12px;
35+
display: inline-flex;
36+
align-items: center;
3137
}

0 commit comments

Comments
 (0)