antd的Input TextArea的showCount自定义写法

1,912 阅读1分钟

对于antd大家都肯定用到了,有很多设计需要自定义的地方,antd的案例中也没有给出这些自定义的写法。从源码TextArea.d.ts文件中可以看到showCount是可以自定义的

image.png

自定义的写法也比较简单,看网上很多人放弃了antd的组件使用,自己手写实现,这样浪费了很多时间 具体写法如下:

对比一下两段代码

<TextArea
  rows={4}
  placeholder="请输入"
  maxLength={500}
  showCount={{ formatter: ({ count, maxLength }) => `${appendMessage} ${count + 4}/${maxLength}` }}
/>

限制只输入496个字符,自动计数补全4个字符

<TextArea
  rows={4}
  placeholder="请输入"
  maxLength={496}
  showCount={{ formatter: ({ count }) => `${appendMessage} ${count + 4}/500` }}
/>

20376E7E-9848-4309-9E52-38E8BD473B40.png