|
@@ -1,10 +1,10 @@
|
|
|
import {AutoColumn, isNullOrTrue, Message} from "auto-antd";
|
|
|
-import {Button, Table} from "antd";
|
|
|
+import {Button, Drawer, Table} from "antd";
|
|
|
import React, {useEffect, useMemo, useRef, useState} from "react";
|
|
|
import {ColumnsType} from "antd/lib/table";
|
|
|
import {CurdForm, ICurdForm, IProps as IFormProps} from "./Form.tsx";
|
|
|
import {LDAP} from "../../api/ldap.ts";
|
|
|
-import {EditOutlined, PlusOutlined, SyncOutlined} from "@ant-design/icons";
|
|
|
+import {EditOutlined, LoadingOutlined, PlusOutlined, SyncOutlined} from "@ant-design/icons";
|
|
|
import './index.less'
|
|
|
import {ICurdConfig, ICurdData} from "./types.ts";
|
|
|
|
|
@@ -13,6 +13,7 @@ export type CurdColumn = AutoColumn & {
|
|
|
search?: boolean
|
|
|
editable?: boolean
|
|
|
hidden?: boolean
|
|
|
+ addable?: boolean // 添加时可编辑
|
|
|
}
|
|
|
|
|
|
|
|
@@ -31,15 +32,46 @@ type IProps<T extends ICurdData> = IFormProps<T> & {
|
|
|
* 一个完成的增删查改界面
|
|
|
* @constructor
|
|
|
*/
|
|
|
-export function CurdPage<T extends ICurdData>({columns, getList, config, operationRender, onSuccess,...props}: IProps<T>) {
|
|
|
+export function CurdPage<T extends ICurdData>({
|
|
|
+ columns,
|
|
|
+ getList,
|
|
|
+ config,
|
|
|
+ operationRender,
|
|
|
+ onSuccess,
|
|
|
+ ...props
|
|
|
+ }: IProps<T>) {
|
|
|
|
|
|
const [list, setList] = useState<T[]>([]);
|
|
|
const [total, setTotal] = useState<number>(0)
|
|
|
const [query, setQuery] = useState<any>({current: 1, pageSize: 10});
|
|
|
- const [loading,setLoading] = useState<boolean>(false);
|
|
|
+ const [loading, setLoading] = useState<boolean>(false);
|
|
|
+ // edit or add
|
|
|
+ const [visible, setVisible] = useState<boolean>(false);
|
|
|
+ const [editData, setEditData] = useState<Partial<T>>()
|
|
|
|
|
|
const formRef = useRef<ICurdForm<LDAP.Server>>()
|
|
|
|
|
|
+ const onAddOrEdit = (edit: Partial<T>) => {
|
|
|
+ setVisible(true)
|
|
|
+ if (props.getDetail) {
|
|
|
+ setLoading(true)
|
|
|
+ props.getDetail?.(edit)
|
|
|
+ .then(res => {
|
|
|
+ setEditData(res)
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ Message.warning(err.message)
|
|
|
+ setVisible(false)
|
|
|
+ })
|
|
|
+ .finally(() => {
|
|
|
+ setLoading(false)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ setEditData({...edit})
|
|
|
+ setVisible(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
const tableColumns = useMemo(() => {
|
|
|
const cols = columns.filter(item => !item.hidden)
|
|
@@ -59,11 +91,11 @@ export function CurdPage<T extends ICurdData>({columns, getList, config, operati
|
|
|
return (<div className="operation-list">
|
|
|
{
|
|
|
isNullOrTrue(config?.editable) ?
|
|
|
- (<Button size="small" type="primary" icon={<EditOutlined />}
|
|
|
- onClick={() => formRef.current?.show({...record})} />) : null
|
|
|
+ (<Button size="small" type="primary" icon={<EditOutlined/>}
|
|
|
+ onClick={() => onAddOrEdit(record)}/>) : null
|
|
|
}
|
|
|
- {config?.operationRender?.(record,index)}
|
|
|
- </div>)
|
|
|
+ {config?.operationRender?.(record, index)}
|
|
|
+ </div>)
|
|
|
}
|
|
|
})
|
|
|
|
|
@@ -71,17 +103,22 @@ export function CurdPage<T extends ICurdData>({columns, getList, config, operati
|
|
|
|
|
|
}, [columns]);
|
|
|
|
|
|
+ const onClose = () => {
|
|
|
+ setVisible(false)
|
|
|
+ setEditData(undefined)
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
useEffect(() => {
|
|
|
setLoading(true)
|
|
|
getList(query).then(res => {
|
|
|
- if (res){
|
|
|
+ if (res) {
|
|
|
setList(res.list)
|
|
|
setTotal(res.total)
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
Message.warning('无数据!')
|
|
|
}
|
|
|
- }).finally(()=>{
|
|
|
+ }).finally(() => {
|
|
|
setLoading(false)
|
|
|
})
|
|
|
}, [getList, query]);
|
|
@@ -97,10 +134,10 @@ export function CurdPage<T extends ICurdData>({columns, getList, config, operati
|
|
|
|
|
|
return <div className="curd">
|
|
|
<div className="curd-header">
|
|
|
- <Button onClick={() => formRef.current?.show({})} icon={<PlusOutlined />} />
|
|
|
+ <Button onClick={() => onAddOrEdit({})} icon={<PlusOutlined/>}/>
|
|
|
{operationRender}
|
|
|
- <div style={{flex: 1}} />
|
|
|
- <Button loading={loading} onClick={() =>setQuery({...query})} icon={<SyncOutlined />} />
|
|
|
+ <div style={{flex: 1}}/>
|
|
|
+ <Button loading={loading} onClick={() => setQuery({...query})} icon={<SyncOutlined/>}/>
|
|
|
</div>
|
|
|
<div className="curd-body">
|
|
|
<Table columns={tableColumns as any} dataSource={list as any} pagination={{
|
|
@@ -108,17 +145,31 @@ export function CurdPage<T extends ICurdData>({columns, getList, config, operati
|
|
|
pageSize: query.pageSize,
|
|
|
current: query.current,
|
|
|
onChange: onPageChange,
|
|
|
- }}/>
|
|
|
+ }}
|
|
|
+ rowKey={config?.rowKey ?? 'id'}/>
|
|
|
</div>
|
|
|
<div className="curd-footer">
|
|
|
|
|
|
</div>
|
|
|
- <CurdForm ref={formRef as any}
|
|
|
- columns={columns}
|
|
|
- getDetail={props.getDetail as any}
|
|
|
- onSave={props.onSave as any}
|
|
|
- onSuccess={onSaveSuccess as any}
|
|
|
- config={config as any}/>
|
|
|
+
|
|
|
+ <Drawer open={visible} destroyOnClose width={config?.editDialogWidth ?? 650}
|
|
|
+ title={editData?.id ? '编辑' : '新增'}
|
|
|
+ height={400} onClose={onClose}>
|
|
|
+ <div className="curd-edit-dialog">
|
|
|
+ {
|
|
|
+ loading ? (<LoadingOutlined/>) : (
|
|
|
+ <CurdForm ref={formRef as any}
|
|
|
+ initialData={editData}
|
|
|
+ columns={columns}
|
|
|
+ onSave={props.onSave as any}
|
|
|
+ onSuccess={onSaveSuccess as any}
|
|
|
+ onClose={onClose}
|
|
|
+ config={config as any}/>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ </div>
|
|
|
+ </Drawer>
|
|
|
+
|
|
|
</div>
|
|
|
|
|
|
}
|