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

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

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 类型供我导入,因此我甚至无法猜测应该使用哪种格式。是否有人遇到过相同的问题或已解决了这个问题?

import { useMemo, useState } from 'react';
import { AutoComplete } from 'antd';

export const TagsAutocomplete = ({ onSelect, value }: { onSelect: (elem: string) => void, value: string }) => {
  const options: string[] = ['Tag 1', 'Tag 2', 'Tag 3'];
  const [inputText, setInputText] = useState('');
  const filteredOptions = useMemo(() => {
    return options.filter((option) => option.toLowerCase().includes(inputText.toLowerCase()));
  }, [options, inputText]);

  return (
    <AutoComplete
      value={value} // 类型 string 无法赋值给类型 ValueType | null 
      options={filteredOptions} // 类型 string[] 无法赋值给类型 OptionType[]
      onSelect={onSelect}
      onSearch={setInputText}
    />
  )
}

对于值/选项格式,我尝试使用了 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?

import { useMemo, useState } from &#39;react&#39;;
import { AutoComplete } from &#39;antd&#39;;

export const TagsAutocomplete = ({onSelect, value}: {onSelect: (elem: string) =&gt; void, value: string}) =&gt; {
  const options: string[] = [&#39;Tag 1&#39;, &#39;Tag 2&#39;, &#39;Tag 3&#39;];
  const [inputText, setInputText] = useState(&#39;&#39;);
  const filteredOptions = useMemo(() =&gt; {
    return options.filter((option) =&gt; option.toLowerCase().includes(inputText.toLowerCase()));
  }, [options, inputText]);

  return (
    &lt;AutoComplete
      value={value} // Type string is not assignable to type ValueType | null 
      options={filteredOptions} // Type string[] is not assignable to type OptionType[]
      onSelect={onSelect}
      onSearch={setInputText}
    /&gt;
  )
}

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)来查看定义。

否则,请参考官方 文档

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

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

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

演示

英文:

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:

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:

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

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:

确定