Ant-design自动完成组件的ValueType/OptionType问题

huangapple go评论162阅读模式
英文:

Ant-design autocomplete ValueType/OptionType issue

问题

I'm trying to use antd AutoComplete with TypeScript with value as a string, but I'm ending up with TypeScript errors for both options and value.

For the value I have - 类型 string 无法赋值给类型 ValueType | null

For the options - 类型 string[] 无法赋值给类型 OptionType[]

然而,antd 没有提供任何 ValueTypeOptionType 类型供我导入,因此我甚至无法猜测应该使用哪种格式。是否有人遇到过相同的问题或已解决了这个问题?

  1. import { useMemo, useState } from 'react';
  2. import { AutoComplete } from 'antd';
  3. export const TagsAutocomplete = ({ onSelect, value }: { onSelect: (elem: string) => void, value: string }) => {
  4. const options: string[] = ['Tag 1', 'Tag 2', 'Tag 3'];
  5. const [inputText, setInputText] = useState('');
  6. const filteredOptions = useMemo(() => {
  7. return options.filter((option) => option.toLowerCase().includes(inputText.toLowerCase()));
  8. }, [options, inputText]);
  9. return (
  10. <AutoComplete
  11. value={value} // 类型 string 无法赋值给类型 ValueType | null
  12. options={filteredOptions} // 类型 string[] 无法赋值给类型 OptionType[]
  13. onSelect={onSelect}
  14. onSearch={setInputText}
  15. />
  16. )
  17. }

对于值/选项格式,我尝试使用了 string 和对象格式 {value: string}。这两种方法都导致了类型错误。

英文:

I'm trying to use antd AutoComplete with typscript with value as a string, but I'm ending up with typescript errors for both options and value.

For the value I have - Type string is not assignable to type ValueType | null

For the options - Type string[] is not assignable to type OptionType[]

However antd doesn't distrubute any ValueType of OptionType types for me to import, so I can't even guess the format that I should be using. Have anyone had or maybe solved the same issue?

  1. import { useMemo, useState } from &#39;react&#39;;
  2. import { AutoComplete } from &#39;antd&#39;;
  3. export const TagsAutocomplete = ({onSelect, value}: {onSelect: (elem: string) =&gt; void, value: string}) =&gt; {
  4. const options: string[] = [&#39;Tag 1&#39;, &#39;Tag 2&#39;, &#39;Tag 3&#39;];
  5. const [inputText, setInputText] = useState(&#39;&#39;);
  6. const filteredOptions = useMemo(() =&gt; {
  7. return options.filter((option) =&gt; option.toLowerCase().includes(inputText.toLowerCase()));
  8. }, [options, inputText]);
  9. return (
  10. &lt;AutoComplete
  11. value={value} // Type string is not assignable to type ValueType | null
  12. options={filteredOptions} // Type string[] is not assignable to type OptionType[]
  13. onSelect={onSelect}
  14. onSearch={setInputText}
  15. /&gt;
  16. )
  17. }

For value/option format I tried to use string and object in format {value: string}. Both approaches gave me the types error

答案1

得分: 1

你可以通过点击 options 属性并同时按住 ctrl(对于Windows/Linux)或 command(对于Mac)来查看定义。

否则,请参考官方 文档

  1. options 选择选项。性能比jsx定义更好 { label, value }[]

因此,将 options 重构为 {label: string, value: string}[] 并从组件中提取它,因为它是一个常量,不需要将它保留在组件中:

  1. const options = [
  2. { label: '标签1', value: '标签1' },
  3. { label: '标签2', value: '标签2' },
  4. { label: '标签3', value: '标签3' },
  5. ];

演示

英文:

You can check the definitions by clicking on the options prop while holding ctrl for windows/linux and command for Mac.

Otherwise, refer to the official documentation:

  1. options Select options. Will get better perf than jsx definition { label, value }[]

Thus, refactor options to {label: string, value: string}[] and extract it from the component, since it is a constant and no need to keep it in the component:

  1. const options = [
  2. { label: &#39;Tag 1&#39;, value: &#39;Tag1&#39; },
  3. { label: &#39;Tag 2&#39;, value: &#39;Tag2&#39; },
  4. { label: &#39;Tag 3&#39;, value: &#39;Tag3&#39; },
  5. ];

playground

huangapple
  • 本文由 发表于 2023年6月11日 21:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定