45 lines
818 B
TypeScript
45 lines
818 B
TypeScript
import { useState } from "react";
|
|
|
|
|
|
export type BasicCrudType = 'add' | 'edit' | 'view';
|
|
|
|
export const CRUD_NAMES = {
|
|
add: '新增',
|
|
edit: '编辑'
|
|
}
|
|
|
|
export type CrudContext<modes = any> = {
|
|
mode: modes | null;
|
|
record: any;
|
|
goto: (mode: modes | null, r: any) => void;
|
|
loading: boolean;
|
|
setLoading: (val: boolean) => void;
|
|
}
|
|
|
|
|
|
export default function useCrud<modes = any>(params?: {
|
|
mode?: modes;
|
|
record?: any;
|
|
}): CrudContext<modes> {
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [state, setState] = useState<{
|
|
mode: modes | null;
|
|
record: any;
|
|
}>({
|
|
mode: params?.mode || null,
|
|
record: params?.record,
|
|
})
|
|
|
|
const goto = (mode: modes | null, record: any) => {
|
|
setState({ mode, record })
|
|
};
|
|
|
|
return {
|
|
...state,
|
|
goto,
|
|
loading,
|
|
setLoading,
|
|
}
|
|
}
|