skynin

Untitled

Jul 30th, 2025
233
0
177 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 1.58 KB | Source Code | 0 0
  1. function UserList() {
  2.   const [isDialogOpen, setIsDialogOpen] = useState(false)
  3.  
  4.   return (
  5.     <>
  6.       <Box sx={{ mb: 2 }}>
  7.         {!isDialogOpen && (
  8.           <Button
  9.             variant="contained"
  10.             color="primary"
  11.             onClick={() => {
  12.               setSelectedUser(null)
  13.               setIsDialogOpen(true)
  14.             }}
  15.           >
  16.             Add User
  17.           </Button>
  18.         )}
  19.       </Box>
  20.       {isDialogOpen && (
  21.         <UserForm
  22.           user={selectedUser}
  23.           onSave={(updatedUser) => {
  24.             handleUserSave(updatedUser)
  25.             // Reset selected user
  26.             setSelectedUser(null)
  27.             // Close the dialog
  28.             setIsDialogOpen(false)
  29.           }}
  30.           onCancel={() => setIsDialogOpen(false)}
  31.         />
  32.       )}
  33.       <Table>
  34.         <TableHead>
  35. ...
  36.  
  37. ...
  38. function UserForm({
  39.   user,
  40.   onSave,
  41.   onCancel,
  42. }:
  43. ...
  44. const handleSubmit = (e: React.FormEvent) => {
  45. ...
  46.  
  47.  return (
  48.     <Paper elevation={8}>
  49.       <Box sx={{ p: 2 }}>
  50.         <Typography variant="h6" component="h2" sx={{ mb: 2 }}>
  51.           {user ? 'Edit User' : 'Add User'}
  52.         </Typography>
  53.         <form onSubmit={handleSubmit}>
  54.           <TextField
  55. ...
  56.           <Box sx={{ display: 'flex', justifyContent: 'flex-end', gap: 1, mt: 2 }}>
  57.             <Button type="button" onClick={onCancel}>
  58.               Cancel
  59.             </Button>
  60.             <Button type="submit" color="primary" variant="contained">
  61.               Save
  62.             </Button>
  63.           </Box>
  64.         </form>
  65.       </Box>
  66.     </Paper>
  67.  
Advertisement
Add Comment
Please, Sign In to add comment