setters - 设置器 API

模块简介

负责注册设置器、管理设置器的 API。注册自定义设置器之后可以在物料中进行使用。

方法签名(functions)

getSetter

获取指定 setter

类型定义

  1. function getSetter(type: string): RegisteredSetter;

getSettersMap

获取已注册的所有 settersMap

类型定义

  1. function getSettersMap(): Map<string, RegisteredSetter>

registerSetter

注册一个 setter

类型定义

  1. function registerSetter(
  2. typeOrMaps: string | { [key: string]: CustomView | RegisteredSetter },
  3. setter?: CustomView | RegisteredSetter | undefined,
  4. ): void;

使用示例

注册官方内置 Setter 到设计器中

  1. import { setters, skeleton } from '@alilc/lowcode-engine';
  2. import { setterMap, pluginMap } from '@alilc/lowcode-engine-ext';
  3. const setterRegistry = (ctx: ILowCodePluginContext) => {
  4. return {
  5. name: 'ext-setters-registry',
  6. async init() {
  7. // 注册 setterMap
  8. setters.registerSetter(setterMap);
  9. // 注册插件
  10. // 注册事件绑定面板
  11. skeleton.add({
  12. area: 'centerArea',
  13. type: 'Widget',
  14. content: pluginMap.EventBindDialog,
  15. name: 'eventBindDialog',
  16. props: {},
  17. });
  18. // 注册变量绑定面板
  19. skeleton.add({
  20. area: 'centerArea',
  21. type: 'Widget',
  22. content: pluginMap.VariableBindDialog,
  23. name: 'variableBindDialog',
  24. props: {},
  25. });
  26. },
  27. };
  28. }
  29. setterRegistry.pluginName = 'setterRegistry';
  30. await plugins.register(setterRegistry);

开发自定义 Setter

AltStringSetter 代码如下:

  1. import * as React from "react";
  2. import { Input } from "@alifd/next";
  3. import "./index.scss";
  4. interface AltStringSetterProps {
  5. // 当前值
  6. value: string;
  7. // 默认值
  8. initialValue: string;
  9. // setter唯一输出
  10. onChange: (val: string) => void;
  11. // AltStringSetter 特殊配置
  12. placeholder: string;
  13. }
  14. export default class AltStringSetter extends React.PureComponent<AltStringSetterProps> {
  15. componentDidMount() {
  16. const { onChange, value, defaultValue } = this.props;
  17. if (value == undefined && defaultValue) {
  18. onChange(defaultValue);
  19. }
  20. }
  21. // 声明 Setter 的 title
  22. static displayName = 'AltStringSetter';
  23. render() {
  24. const { onChange, value, placeholder } = this.props;
  25. return (
  26. <Input
  27. value={value}
  28. placeholder={placeholder || ""}
  29. onChange={(val: any) => onChange(val)}
  30. ></Input>
  31. );
  32. }
  33. }

开发完毕之后,注册 AltStringSetter 到设计器中:

  1. import AltStringSetter from './AltStringSetter';
  2. const registerSetter = window.AliLowCodeEngine.setters.registerSetter;
  3. registerSetter('AltStringSetter', AltStringSetter);

注册之后,我们就可以在物料中使用了,其中核心配置如下:

  1. {
  2. "props": {
  3. "isExtends": true,
  4. "override": [
  5. {
  6. "name": "type",
  7. "setter": "AltStringSetter"
  8. }
  9. ]
  10. }
  11. }

完整配置如下:

  1. {
  2. "componentName": "Message",
  3. "title": "Message",
  4. "props": [
  5. {
  6. "name": "title",
  7. "propType": "string",
  8. "description": "标题",
  9. "defaultValue": "标题"
  10. },
  11. {
  12. "name": "type",
  13. "propType": {
  14. "type": "oneOf",
  15. "value": [
  16. "success",
  17. "warning",
  18. "error",
  19. "notice",
  20. "help",
  21. "loading"
  22. ]
  23. },
  24. "description": "反馈类型",
  25. "defaultValue": "success"
  26. }
  27. ],
  28. "configure": {
  29. "props": {
  30. "isExtends": true,
  31. "override": [
  32. {
  33. "name": "type",
  34. "setter": "AltStringSetter"
  35. }
  36. ]
  37. }
  38. }
  39. }