赞
踩
何时使用
类型
interface Option {
value: string;
label: string;
children?: Option[];
}
props: CascaderProps<any>
就会报错
import { useMemo } from 'react'; import { Cascader, CascaderProps } from 'antd'; import { useModel } from 'umi'; interface Option { value: string | number; label: string; children?: Option[]; } const CascaderSelect = (props: CascaderProps<any>) => { ... ... ... return ( <Cascader placeholder="请选择" options={options || []} multiple allowClear {...props} /> ); }; export default XXXCascaderSelect;
看源码
SingleCascaderProps.ts
export { BaseOptionType, DefaultOptionType }; export declare type FieldNamesType = FieldNames; export declare type FilledFieldNamesType = Required<FieldNamesType>; declare const SHOW_CHILD: "SHOW_CHILD", SHOW_PARENT: "SHOW_PARENT"; declare type SingleCascaderProps = Omit<RcSingleCascaderProps, 'checkable' | 'options'> & { multiple?: false; }; declare type MultipleCascaderProps = Omit<RcMultipleCascaderProps, 'checkable' | 'options'> & { multiple: true; }; declare type UnionCascaderProps = SingleCascaderProps | MultipleCascaderProps; export declare type CascaderProps<DataNodeType> = UnionCascaderProps & { multiple?: boolean; size?: SizeType; disabled?: boolean; bordered?: boolean; placement?: SelectCommonPlacement; suffixIcon?: React.ReactNode; options?: DataNodeType[]; status?: InputStatus; /** * @deprecated `dropdownClassName` is deprecated which will be removed in next major * version.Please use `popupClassName` instead. */ dropdownClassName?: string; }; export interface CascaderRef { focus: () => void; blur: () => void; } declare const Cascader: (<OptionType extends DefaultOptionType | BaseOptionType = DefaultOptionType>(props: React.PropsWithChildren<CascaderProps<OptionType>> & { ref?: React.Ref<CascaderRef> | undefined; }) => React.ReactElement) & { displayName: string; SHOW_PARENT: typeof SHOW_PARENT; SHOW_CHILD: typeof SHOW_CHILD; }; export default Cascader;
MultipleCascaderProp.ts
onChange 类型应该是复选但是传单选
严格符合这个 typescript 接口格式
建议写一个函数专门处理后端接口返回的数据结构转换成下面的样式
由于数据结构带有 children 用递归很好
interface Option {
value: string | number;
label: string;
children?: Option[];
}
例子:
const flatToTree = (data?: any[]): any => {
const formattedDepartment = data?.map(
({ name, id, children }) => ({
label: name,
value: id,
children: flatToTree(children),
}),
);
return formattedDepartment;
}
吐槽今天还遇到的一个坑,后端接口文字段名给错了 文档给的字段名和实际浏览器抓包给的不一样 应该是基于 yapi 做的自用的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。