// src/components/BulkUpdateButton.js
|
import React, { useState } from 'react';
|
import { useUpdateMany, useRefresh, useNotify, useUnselectAll, Button as RaButton } from 'react-admin';
|
import { Dialog, DialogActions, DialogContent, DialogTitle, TextField, Select, MenuItem } from '@mui/material';
|
import UpdateIcon from '@mui/icons-material/Update';
|
|
const BulkUpdateButton = ({ selectedIds, resource, label }) => {
|
console.log('BulkUpdateButton rendered with selectedIds:', selectedIds);
|
|
const [open, setOpen] = useState(false);
|
const [formData, setFormData] = useState({});
|
|
// Hooks 调用顺序调整
|
const refresh = useRefresh();
|
const notify = useNotify();
|
const unselectAll = useUnselectAll(resource);
|
|
const [updateMany, { loading }] = useUpdateMany(
|
resource,
|
{ ids: selectedIds, data: formData },
|
{
|
onSuccess: () => {
|
setOpen(false);
|
refresh();
|
notify('批量更新成功', { type: 'success' });
|
unselectAll();
|
},
|
onError: (error) => {
|
notify(`批量更新失败: ${error.message}`, { type: 'warning' });
|
}
|
}
|
);
|
|
const handleOpen = () => setOpen(true);
|
const handleClose = () => setOpen(false);
|
|
const handleChange = (e) => {
|
const { name, value } = e.target;
|
setFormData({
|
...formData,
|
[name]: value,
|
});
|
};
|
|
const handleSubmit = () => {
|
updateMany();
|
};
|
|
return (
|
<>
|
<RaButton onClick={handleOpen} startIcon={<UpdateIcon />} label={label || '批量修改'}>
|
{/* 如果使用 React-Admin 的 Button,可以省略子节点 */}
|
</RaButton>
|
<Dialog open={open} onClose={handleClose}>
|
<DialogTitle>批量修改</DialogTitle>
|
<DialogContent>
|
{/* 使用 Select 组件限制状态值 */}
|
<Select
|
margin="dense"
|
name="status"
|
label="状态"
|
fullWidth
|
variant="standard"
|
onChange={handleChange}
|
defaultValue=""
|
>
|
<MenuItem value="">
|
<em>无</em>
|
</MenuItem>
|
<MenuItem value="1">启用</MenuItem>
|
<MenuItem value="0">禁用</MenuItem>
|
</Select>
|
<TextField
|
margin="dense"
|
name="memo"
|
label="备注"
|
type="text"
|
fullWidth
|
variant="standard"
|
onChange={handleChange}
|
/>
|
</DialogContent>
|
<DialogActions>
|
<RaButton onClick={handleClose} color="primary">
|
取消
|
</RaButton>
|
<RaButton onClick={handleSubmit} color="primary" disabled={loading}>
|
确认
|
</RaButton>
|
</DialogActions>
|
</Dialog>
|
</>
|
);
|
};
|
|
export default BulkUpdateButton;
|