index.tsx 1.95 KB
import React from 'react';
import Default from './select/Default';
import type { DataType } from './select/datas';
import datas from './select/datas';

/**
 * 全剧API
 */
export interface globalProps {
    onReady?: (list: any[]) => void;
    value?: string;
    onChange?: (id: string | string[] | undefined, props: any) => void;
    onChildError?: (id: string | string[], list: Record<string, any>[]) => void;
    placeholder?: string;
    pageSize?: number;       // 每页获取条数
    style?: React.CSSProperties;
    className?: string;
    multiple?: boolean;      // 是否开启多选模式
    disableds?: string[];
    param?: Record<string, any>;  // 动态查询参数
}

export interface subGlobalProps extends globalProps {
    mode?: 'multiple' | 'tags';         // 模式
    notServer?: boolean;
    storage?: string;                   // 是否使用 storage 库
}

export interface IndexSelectProps extends globalProps {
    type: DataType;
}
const gathers = datas;

const Index = ({
    onReady,
    value,
    onChange,
    onChildError,
    placeholder,
    pageSize,
    style,
    className,
    type,
    multiple,
    disableds,
    param,
    ...props
}: IndexSelectProps) => {
    const globalProps: subGlobalProps = {
        onReady,
        value,
        onChange,
        onChildError,
        placeholder,
        pageSize,
        style,
        className,
        disableds,
        mode: multiple ? 'multiple' : undefined,
    }
    const key = JSON.stringify(param);

    
    if (gathers[type]) {
        const { storage, pageSize:gatPageSize } = gathers[type];

        return <Default
            {...props}
            key={key}
            findParam={param}
            rule={gathers[type]}
            globalProps={{
                storage: storage ? type : undefined,
                notServer: gatPageSize === 0 ? true : undefined,
                ...globalProps,
            }}
        />
    }

    return null;
}


export default Index;