alibaba / lowcode-engine

An enterprise-class low-code technology stack with scale-out design / 一套面向扩展设计的企业级低代码技术体系
https://lowcode-engine.cn
MIT License
14.67k stars 2.55k forks source link

[Question]setter condtion的作用 #2115

Open eightHundreds opened 1 year ago

eightHundreds commented 1 year ago
image

这个condition函数的作用是什么? 它只用在MixedSetter吗?

eightHundreds commented 1 year ago

还有recommend

eightHundreds commented 1 year ago

这个我自问自答

  /**
   * for MixedSetter to check this setter if available
   */
  condition?: (field: any) => boolean;

  /**
   * for MixedSetter to manual change to this setter
   */
  initialValue?: any | ((field: any) => any);
eightHundreds commented 1 year ago

顺便分享一个跟condtion相关, 看起来像bug的case

{
        title: {
          label: 'children',
          tip: '子节点',
        },
        name: 'children',
        description: '子节点',
        setter: {
          componentName: 'MixedSetter',
          props: {
            setters: [
              'SlotSetter',
              'StringSetter',
              'VariableSetter',
            ],
          },
        },
      },

默认情况优先展示SlotSetter, 其表现是一个开关(switch),然后把它关闭后会自动切换成StringSetter 2023-05-31 20 33 42

是因为关闭后, SlotSetter会把这个属性值变成null, 然后他就不能满足SlotSetter默认的condtion判断, 所以MixedSetter选择了第二个StringSetter

解决办法是

{
        title: {
          label: 'children',
          tip: '子节点',
        },
        name: 'children',
        description: '子节点',
        setter: {
          componentName: 'MixedSetter',
          props: {
            setters: [
              {
                componentName: 'SlotSetter',
+                condition: () => true,
              },
              'StringSetter',
              'VariableSetter',
            ],
          },
        },
      },