如何在React-Select的onChange事件中为选定选项添加自定义类型

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

How to add custom type to selected option for onChange event in React-Select

问题

我创建了一个使用react-select的SelectBox组件,并为其添加了onChange事件。
我想在onChange事件中为选定的选项添加一个类型"SelectedItemType"。
然而,onChange事件的类型是:
(property) onChange?: ((newValue: unknown, actionMeta: ActionMeta) => void) | undefined
因此,选项类型是unknown,它阻止我添加除unknown以外的类型,这使得从选项中提取对象属性变得困难。

在上面的代码中,我想将SelectedItemType添加到onChange的newValue中。然而,它抛出了一个错误,说
Type '(newValue: SelectedItemType) => void' is not assignable to type '(newValue: unknown, actionMeta: ActionMeta) => void'.
Types of parameters 'newValue' and 'newValue' are incompatible.
Type 'unknown' is not assignable to type 'SelectedItemType'.
我如何将此自定义类型添加到选项中,因为我想从选项中提取值和标签?

英文:

I created a SelectBox component using react-select and added onChange event for it.
I would like to add a type "SelectedItemType" to the selected option in onChange event.
However the onChnage event is of type
(property) onChange?: ((newValue: unknown, actionMeta: ActionMeta<unknown>) => void) | undefined
Hence the option type is unknown and its preventing me from adding types other than unknown which makes it difficult to extract the object attributes from option

import { ForwardedRef, forwardRef } from &quot;react&quot;;
import Select, { SelectInstance } from &quot;react-select&quot;;

export type SelectedItemType = {
value: string;
label: string;
};

export type SelectProps = {
options: SelectedItemType[];
ref?: ForwardedRef&lt;SelectInstance&gt;;
onChange: (selectedItem: unknown) =&gt; void;
};

const SelectBox: React.FC&lt;SelectProps&gt; = forwardRef&lt;
SelectInstance,
SelectProps
&gt;(({ options, onChange, ...props }, ref) =&gt; {
return (
&lt;Select
options={options}
ref={ref}
{...props}
onChange={(newValue: unknown) =&gt; onChange(newValue)}
/&gt;
);
});

export default SelectBox;

In the above code I would like to add SelectedItemType to the newValue for onChange. However its throwing an error saying
Type '(newValue: SelectedItemType) => void' is not assignable to type '(newValue: unknown, actionMeta: ActionMeta<unknown>) => void'.
Types of parameters 'newValue' and 'newValue' are incompatible.
Type 'unknown' is not assignable to type 'SelectedItemType'.
How do I add this custom Type to the option as I want to extract the value and label from the option.

答案1

得分: 1

以下是翻译好的部分:

如果您想将 newValue 分配类型为 SelectedItemType,以便在 onChange 函数中可以轻松访问其属性,那么可以通过进行两个主要更改来实现。

  1. 更改 SelectPropsonChange 属性的类型如下:
export type SelectProps = {
  options: SelectedItemType[];
  ref?: ForwardedRef<SelectInstance>;
  onChange: (selectedItem: SelectedItemType) => void;
};
  1. 然后,在 select 组件中,它的 onChange 将被修改为:
<Select
  options={options}
  ref={ref}
  {...props}
  onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
/>

然后像这样使用您的 SelectBox 组件:

<SelectBox
  options={[
    { label: "A", value: "1" },
    { label: "B", value: "2" }
  ]}
  onChange={function (selectedItem: SelectedItemType): void {
    console.log(selectedItem.value);
  }}
/>

以下是文件 App.tsx 的完整代码:

import { ForwardedRef, forwardRef } from "react";
import Select, { SelectInstance } from "react-select";

export type SelectedItemType = {
  value: string;
  label: string;
};

export type SelectProps = {
  options: SelectedItemType[];
  ref?: ForwardedRef<SelectInstance>;
  onChange: (selectedItem: SelectedItemType) => void;
};

const SelectBox: React.FC<SelectProps> = forwardRef<
  SelectInstance,
  SelectProps
>(({ options, onChange, ...props }, ref) => {
  return (
    <Select
      options={options}
      ref={ref}
      {...props}
      onChange={(newValue: unknown) => onChange(newValue as SelectedItemType)}
    />
  );
});

export default function App() {
  return (
    <div className="App">
      <SelectBox
        options={[
          { label: "A", value: "1" },
          { label: "B", value: "2" }
        ]}
        onChange={function (selectedItem: SelectedItemType): void {
          console.log(selectedItem.value);
        }}
      />
    </div>
  );
}
英文:

If you would like to assign type to newValue to SelectedItemType so that its properties can be easily accessed in onChange function then it can be done by doing two main changes.

  1. Change type of onChange property of SelectProps as
export type SelectProps = {
  options: SelectedItemType[];
  ref?: ForwardedRef&lt;SelectInstance&gt;;
  onChange: (selectedItem: SelectedItemType) =&gt; void;
};
  1. Then in select component its onChange would be modified as
&lt;Select
      options={options}
      ref={ref}
      {...props}
      onChange={(newValue: unknown) =&gt; onChange(newValue as SelectedItemType)}
    /&gt;

Then use your SelectBox component like:

&lt;SelectBox
        options={[
          { label: &quot;A&quot;, value: &quot;1&quot; },
          { label: &quot;B&quot;, value: &quot;2&quot; }
        ]}
        onChange={function (selectedItem: SelectedItemType): void {
          console.log(selectedItem.value);
        }}
      /&gt;

Here is full code for file App.tsx

import { ForwardedRef, forwardRef } from &quot;react&quot;;
import Select, { SelectInstance } from &quot;react-select&quot;;

export type SelectedItemType = {
  value: string;
  label: string;
};

export type SelectProps = {
  options: SelectedItemType[];
  ref?: ForwardedRef&lt;SelectInstance&gt;;
  onChange: (selectedItem: SelectedItemType) =&gt; void;
};

const SelectBox: React.FC&lt;SelectProps&gt; = forwardRef&lt;
  SelectInstance,
  SelectProps
&gt;(({ options, onChange, ...props }, ref) =&gt; {
  return (
    &lt;Select
      options={options}
      ref={ref}
      {...props}
      onChange={(newValue: unknown) =&gt; onChange(newValue as SelectedItemType)}
    /&gt;
  );
});

export default function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;SelectBox
        options={[
          { label: &quot;A&quot;, value: &quot;1&quot; },
          { label: &quot;B&quot;, value: &quot;2&quot; }
        ]}
        onChange={function (selectedItem: SelectedItemType): void {
          console.log(selectedItem.value);
        }}
      /&gt;
    &lt;/div&gt;
  );
}

huangapple
  • 本文由 发表于 2023年6月22日 16:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530067.html
匿名

发表评论

匿名网友

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

确定