reactjs - 扩展 React-FC Generic Typescript-Component (Ant-Design Select)
问题描述
我正在尝试扩展 ant-design 的 Select 组件。可以像这样使用组件:
<Select<number>>
...
</Select>
现在我想创建一个自定义包装器组件,但是通用嵌套对我来说真的很难理解。
const BMSelect: React.FC<SelectProps<???>> = ({children, ...props}) => {
return <Select<???> {...props}>
{children}
</Select>
}
如何使用打字稿扩展此选择?
Ant-design 提供以下接口/道具:
import * as React from 'react';
import { Option, OptGroup, SelectProps as RcSelectProps } from 'rc-select';
import { OptionProps } from 'rc-select/lib/Option';
import { SizeType } from '../config-provider/SizeContext';
declare type RawValue = string | number;
export { OptionProps };
export declare type OptionType = typeof Option;
export interface LabeledValue {
key?: string;
value: RawValue;
label: React.ReactNode;
}
export declare type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
export interface InternalSelectProps<VT> extends Omit<RcSelectProps<VT>, 'mode'> {
suffixIcon?: React.ReactNode;
size?: SizeType;
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
bordered?: boolean;
}
export interface SelectProps<VT> extends Omit<InternalSelectProps<VT>, 'inputIcon' | 'mode' | 'getInputElement' | 'backfill'> {
mode?: 'multiple' | 'tags';
}
export interface RefSelectProps {
focus: () => void;
blur: () => void;
}
declare const SelectRef: <VT extends SelectValue = SelectValue>(props: SelectProps<VT> & {
ref?: ((instance: RefSelectProps | null) => void) | React.RefObject<RefSelectProps> | null | undefined;
}) => React.ReactElement;
declare type InternalSelectType = typeof SelectRef;
interface SelectInterface extends InternalSelectType {
SECRET_COMBOBOX_MODE_DO_NOT_USE: string;
Option: typeof Option;
OptGroup: typeof OptGroup;
}
declare const Select: SelectInterface;
export default Select;
解决方案
你的组件还需要接受一个通用参数,它可以传递给它扩展的组件。
我不知道从函数外部引用函数内部泛型参数的方法,但是您当然可以使用函数定义并显式键入道具,而不是使用React.FC
.
function BMSelect<T>({ children, ...props }: SelectProps<T>) {
return (
<Select<T> {...props}>
{children}
</Select>
);
}
如果您查看ant-design select 组件的源代码,您会看到它采用的通用参数 extends SelectValue
。
const InternalSelect = <VT extends SelectValue = SelectValue>(
您需要对您的组件应用相同的约束才能T
正确扩展其组件。
function BMSelect<T extends SelectValue>({ children, ...props }: SelectProps<T>) {
return (
<Select<T> {...props}>
{children}
</Select>
);
}
推荐阅读
- django - Ajax 使用 preventDefault() 重定向到 url
- java - 如何让驱动类继承超类的某些属性?
- clojurescript - 如何阻止 ClojureScript 编译器解析某些 `require`s?
- dependency-injection - 如何更好地在 .Net Core 中注册服务依赖注入
- python - 散景嵌套分类条形图中的 Javascript 错误
- datatables - DataTables 设置默认排序,而 ordering 设置为 false
- boolean - 如何使用 NAND 门表达 (A*B) + (~A*~B)?
- android - 为什么当用户拒绝权限时我的应用程序关闭?
- java - 运行带有依赖项的 jar 文件:java 找不到或加载主类 OSMImportTool
- shell - 存在同名别名时定义函数的位置