Skip to content

如何实现支持Form表单的组件?

笨木头 edited this page Jul 13, 2020 · 1 revision
  1. 继承AntInputComponentBase,需要指定类型,如: public partial class Checkbox : AntInputComponentBase<bool>

  2. 如果组件需要支持多种类型的值,可以使用泛型实现,具体可参考Input组件: public class Input<TValue> : AntInputComponentBase<TValue>

  3. 继承AntInputComponentBase后,组件的值通过Value来保存,但为了能支持Form表单的逻辑,不要直接给Value赋值,应该给CurrentValue赋值,这样在值变更时才能触发表单验证等操作。

  4. 什么时候给CurrentValue赋值?在组件原本需要给Value赋值的时候,改为给CurrentValue赋值即可:如CurrentValue = inputValue; 如果右侧值是字符串类型,则可以直接给CurrentValueAsString赋值,如:CurrentValueAsString = strValue;

  5. 组件原本的值使用了OneOf,包含多种类型,如OneOf<string, int>,怎么办?

涉及到这种情况的,要把组件改为支持Form表单会比较麻烦,如果打算这么用:AntInputComponentBase<OneOf<string, int>>

那么恭喜你,你想多了,不可以这样用。

我暂时没有试过,目前的思路是,使用第2步的方式来实现,希望有经验的朋友补充一下。