Skip to content

自定义 setter

Setter 定义

import { ConfigProvider, Switch } from 'antd';
import { CSetter, CSetterProps } from '@chamn/engine';
type CustomSetterProps = {
a?: string;
};
export const CustomSetter: CSetter<CustomSetterProps> = ({
onValueChange,
setterContext,
...props
}: CSetterProps<CustomSetterProps>) => {
return (
<ConfigProvider
theme={{
token: {
borderRadius: 4,
},
}}
>
<Switch
{...props}
checked={props.value as boolean}
onChange={(open, e) => {
// 将数据同步给 engine
onValueChange?.(open);
}}
/>
</ConfigProvider>
);
};
CustomSetter.setterName = '自定义设置器';

注册

return (
<Engine
plugins={DEFAULT_PLUGIN_LIST}
schema={page as any}
// 传入组件物料
material={[...InnerComponentMeta, ...Material]}
// 组件物料对应的 js 运行库,只能使用 umd 模式的 js
assetPackagesList={assetPackagesList}
beforePluginRun={({ pluginManager }) => {
pluginManager.customPlugin('RightPanel', (pluginInstance) => {
pluginInstance.ctx.config.customPropertySetterMap = {
CustomSetter: CustomSetter,
};
return pluginInstance;
});
}}
onReady={onReady}
/>
);

使用

在物料协议中即可使用该设置器:

export const ButtonMeta: CMaterialType = {
componentName: 'Button',
title: '按钮',
props: [
{
name: 'text',
title: '文本',
valueType: 'string',
setters: ['StringSetter', 'CustomSetter'],
},
],
npm: {
package: __PACKAGE_NAME__ || '',
version: __PACKAGE_VERSION__,
destructuring: true,
exportName: 'Button',
},
snippets: [],
};