2019-06-11 17:00:02 +08:00
|
|
|
|
import React from 'react';
|
2022-06-01 21:35:49 +08:00
|
|
|
|
import {FormItem, Renderer} from 'amis-core';
|
2019-04-30 11:11:25 +08:00
|
|
|
|
|
|
|
|
|
@FormItem({
|
2020-12-25 00:18:45 +08:00
|
|
|
|
type: 'my-custom'
|
2019-04-30 11:11:25 +08:00
|
|
|
|
})
|
|
|
|
|
class MyFormItem extends React.Component {
|
2019-11-07 10:41:14 +08:00
|
|
|
|
render() {
|
|
|
|
|
const {value, onChange} = this.props;
|
2019-04-30 11:11:25 +08:00
|
|
|
|
|
2019-11-07 10:41:14 +08:00
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<p>这个是个自定义组件。通过注册渲染器的方式实现。</p>
|
2019-04-30 11:11:25 +08:00
|
|
|
|
|
2021-06-15 21:43:39 +08:00
|
|
|
|
<p>当前值:{JSON.stringify(value)}</p>
|
2019-04-30 11:11:25 +08:00
|
|
|
|
|
2019-11-07 10:41:14 +08:00
|
|
|
|
<a
|
|
|
|
|
className="btn btn-default"
|
|
|
|
|
onClick={() => onChange(Math.round(Math.random() * 10000))}
|
|
|
|
|
>
|
|
|
|
|
随机修改
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-30 11:11:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Renderer({
|
2022-03-31 14:33:07 +08:00
|
|
|
|
test: /(^|\/)my\-renderer$/,
|
|
|
|
|
autoVar: true
|
2019-04-30 11:11:25 +08:00
|
|
|
|
})
|
|
|
|
|
class CustomRenderer extends React.Component {
|
2019-11-07 10:41:14 +08:00
|
|
|
|
render() {
|
2022-03-31 14:33:07 +08:00
|
|
|
|
const {tip, source} = this.props;
|
|
|
|
|
return source ? (
|
|
|
|
|
<div>{`非 FormItem 类型的渲染器注册,通过变量获取 x 的值为:${source}`}</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div>{tip}</div>
|
2019-11-07 10:41:14 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-30 11:11:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2019-11-07 10:41:14 +08:00
|
|
|
|
title: '自定义组件示例',
|
|
|
|
|
body: [
|
|
|
|
|
{
|
|
|
|
|
type: 'form',
|
|
|
|
|
mode: 'horizontal',
|
|
|
|
|
api: '/api/mock2/form/saveForm?waitSeconds=2',
|
2021-06-07 10:09:55 +08:00
|
|
|
|
debug: true,
|
2019-11-07 10:41:14 +08:00
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
type: 'submit',
|
|
|
|
|
label: '提交',
|
|
|
|
|
primary: true
|
|
|
|
|
}
|
|
|
|
|
],
|
2021-06-07 10:09:55 +08:00
|
|
|
|
body: [
|
2020-12-25 00:18:45 +08:00
|
|
|
|
{
|
|
|
|
|
label: '姓名',
|
|
|
|
|
type: 'text',
|
|
|
|
|
name: 'name'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
type: 'divider'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
label: '使用 custom 组件',
|
|
|
|
|
name: 'name',
|
|
|
|
|
type: 'custom',
|
|
|
|
|
onMount: (dom, data, onChange) => {
|
|
|
|
|
const button = document.createElement('button');
|
|
|
|
|
button.innerText = '点击修改姓名';
|
|
|
|
|
button.onclick = event => {
|
2022-07-13 18:30:28 +08:00
|
|
|
|
onChange('new name', 'name');
|
2020-12-25 00:18:45 +08:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
};
|
|
|
|
|
dom.appendChild(button);
|
|
|
|
|
},
|
|
|
|
|
onUpdate: (dom, data) => {
|
|
|
|
|
console.log('数据有变化', data);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
type: 'divider'
|
|
|
|
|
},
|
|
|
|
|
|
2019-04-30 11:11:25 +08:00
|
|
|
|
{
|
2019-11-07 10:41:14 +08:00
|
|
|
|
name: 'a',
|
2021-07-14 16:37:54 +08:00
|
|
|
|
asFormItem: true,
|
2019-11-07 10:41:14 +08:00
|
|
|
|
children: ({value, onChange}) => (
|
|
|
|
|
<div>
|
2020-12-25 00:18:45 +08:00
|
|
|
|
<p>这是使用 children 的方式,也无需注册。</p>
|
2019-11-07 10:41:14 +08:00
|
|
|
|
|
|
|
|
|
<p>当前值:{value}</p>
|
|
|
|
|
|
|
|
|
|
<a
|
|
|
|
|
className="btn btn-default"
|
|
|
|
|
onClick={() => onChange(Math.round(Math.random() * 10000))}
|
|
|
|
|
>
|
|
|
|
|
随机修改
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
2019-04-30 11:11:25 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
2019-11-07 10:41:14 +08:00
|
|
|
|
type: 'divider'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
name: 'b',
|
2020-12-25 00:18:45 +08:00
|
|
|
|
type: 'my-custom',
|
2019-11-07 10:41:14 +08:00
|
|
|
|
label: '自定义FormItem'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
type: 'divider'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
2021-06-07 10:09:55 +08:00
|
|
|
|
type: 'control',
|
|
|
|
|
body: {
|
2022-03-31 14:33:07 +08:00
|
|
|
|
type: 'my-renderer',
|
|
|
|
|
source: '${x}'
|
2021-06-07 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
type: 'divider'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: '',
|
2021-07-14 16:37:54 +08:00
|
|
|
|
asFormItem: true,
|
2021-06-07 10:09:55 +08:00
|
|
|
|
children: ({render}) => (
|
|
|
|
|
<div>
|
|
|
|
|
<p>组合现有组件</p>
|
|
|
|
|
|
|
|
|
|
{render(
|
|
|
|
|
'formitem',
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
type: 'input-text',
|
|
|
|
|
name: 'x',
|
2021-06-15 21:43:39 +08:00
|
|
|
|
label: 'X',
|
|
|
|
|
value: '1'
|
2021-06-07 10:09:55 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'input-text',
|
|
|
|
|
name: 'y',
|
|
|
|
|
label: 'Y'
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
formMode: 'normal'
|
|
|
|
|
}
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-06-15 21:43:39 +08:00
|
|
|
|
name: 'c',
|
2021-06-07 10:09:55 +08:00
|
|
|
|
label: '',
|
2021-07-14 16:37:54 +08:00
|
|
|
|
asFormItem: true,
|
2021-06-07 10:09:55 +08:00
|
|
|
|
component: ({render, value, onChange, name}) => {
|
|
|
|
|
function handleXChange(x) {
|
|
|
|
|
value = {
|
|
|
|
|
...value,
|
|
|
|
|
x
|
|
|
|
|
};
|
|
|
|
|
onChange(value);
|
|
|
|
|
|
|
|
|
|
// 一定要 return false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleYChange(y) {
|
|
|
|
|
value = {
|
|
|
|
|
...value,
|
|
|
|
|
y
|
|
|
|
|
};
|
|
|
|
|
onChange(value);
|
|
|
|
|
|
|
|
|
|
// 一定要 return false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<p>组合现有组件并控制数据</p>
|
|
|
|
|
|
|
|
|
|
{render(
|
2021-06-15 21:43:39 +08:00
|
|
|
|
'item1',
|
|
|
|
|
{
|
|
|
|
|
type: 'input-text',
|
|
|
|
|
name: 'x',
|
|
|
|
|
label: 'X'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: value?.x || '',
|
|
|
|
|
onChange: handleXChange
|
|
|
|
|
}
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{render(
|
|
|
|
|
'item2',
|
|
|
|
|
{
|
|
|
|
|
type: 'input-text',
|
|
|
|
|
name: 'y',
|
|
|
|
|
label: 'Y'
|
|
|
|
|
},
|
2021-06-07 10:09:55 +08:00
|
|
|
|
{
|
2021-06-15 21:43:39 +08:00
|
|
|
|
value: value?.y || '',
|
|
|
|
|
onChange: handleYChange
|
2021-06-07 10:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-30 11:11:25 +08:00
|
|
|
|
}
|
2019-11-07 10:41:14 +08:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
type: 'my-renderer',
|
2022-03-31 14:33:07 +08:00
|
|
|
|
tip: '放表单外的情况'
|
2019-11-07 10:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
2019-04-30 11:11:25 +08:00
|
|
|
|
};
|