crud-hook
Version:
Make CRUD operation easier.
136 lines (135 loc) • 3.91 kB
TypeScript
import { ActionType, ProColumns, ProCoreActionType, ProDescriptionsItemProps, ProDescriptionsProps, ProFormColumnsType, ProFormProps, ProTableProps } from '@ant-design/pro-components';
import { DrawerProps, ModalProps } from 'antd';
/**
* Schema
*/
export type CRUDSchema<T = Record<string, any>, ValueType = 'text'> = ProColumns<T, ValueType> & Pick<ProFormColumnsType<T, ValueType>, 'convertValue' | 'transform' | 'name' | 'defaultKeyWords' | 'columns'> & Pick<ProDescriptionsItemProps<T, ValueType>, 'hide' | 'mode' | 'label' | 'labelStyle' | 'contentStyle' | 'span'>;
/**
* Table
*/
export type UseProTable<T> = {
schemas: CRUDSchema<T>[];
apis: {
/**
* List Api
*/
list: (params: any, ...rest: any) => Promise<ListResult<T>>;
/**
* Transform list api result to table data
*/
listTransformer?: (data: ListResult<T>) => ListResult<T>;
/**
* Create Api
*/
create?: (values: any) => Promise<void>;
/**
* Update Api
*/
update?: (values: any, record: Record<string, any>) => Promise<void>;
/**
* Delete Api
*/
delete?: (record: any) => Promise<void>;
/**
* Detail Api
*/
detail?: (record: any) => Promise<T>;
};
actionRef?: React.MutableRefObject<ActionType | undefined>;
rowKey?: ProTableProps<T, Record<string, any>>['rowKey'];
action?: {
actionProps?: ProColumns<T>;
render?: (options: {
record: T;
dom: React.ReactNode[];
action?: ProCoreActionType;
}) => React.ReactNode | React.ReactNode[];
prepend?: (options: {
record: T;
action?: ProCoreActionType;
}) => React.ReactNode[];
append?: (options: {
record: T;
action?: ProCoreActionType;
}) => React.ReactNode[];
/**
* Transform record to form values
*/
formTransform?: (config: {
record: T;
isEdit: boolean;
action: ProCoreActionType;
}) => Promise<Partial<T>> | Partial<T>;
detailText?: string;
editText?: string;
deleteText?: string;
createText?: string;
};
/**
* Common config
*/
config?: {
/**
* hide default api message after success
*/
hideApiMessage?: boolean;
/**
* Auto open create form when list is empty
*/
openFormWhenEmpty?: boolean;
};
/**
* @emits
*/
columnEvents?: {};
/**
* @extends ProTableProps
*/
tableProps?: Omit<ProTableProps<T, Record<string, any>>, 'actionRef'>;
/**
* @extends ProFormProps
*/
formProps?: UseProFormProps<T>;
/**
* @extends ProDescriptionsProps
*/
descriptionProps?: DrawerDescriptionProps<T>;
};
export type ListResult<T> = {
data?: T[];
list?: T[];
total?: number;
[key: string]: any;
};
/**
* Pro Form
*/
export type UseProForm<T> = {
schemas: CRUDSchema<T>[];
/**
* @default 'modal'
*/
onFinish: (formData: Partial<T>, isEdit: boolean, record?: Partial<T>) => Promise<boolean | void>;
formProps?: UseProFormProps<T>;
};
export type UseProFormProps<T> = {
formType?: 'modal' | 'drawer';
modalProps?: ModalProps;
drawerProps?: DrawerProps;
/**
* only feed for typescript
* @deprecated
*/
columns?: CRUDSchema<T>[];
title?: string | ((isEdit: boolean, record?: Partial<T>) => string);
} & Omit<ProFormProps<T>, 'title'>;
/**
* Description
*/
export type UseDrawerDescription<T> = {
schemas: CRUDSchema<T>[];
descriptionProps?: DrawerDescriptionProps<T>;
};
export type DrawerDescriptionProps<T> = {
drawerProps?: DrawerProps;
} & Partial<ProDescriptionsProps<T>>;