-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
styled MUI data grid #17
Open
binhttran
wants to merge
5
commits into
main
Choose a base branch
from
lexicon-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { GetAllLexEntriesQuery, useGetAllLexEntriesQuery } from '../graphql/lexicon/lexicon.ts'; | ||
import { DataGrid, GridColDef,DataGridProps } from '@mui/x-data-grid'; | ||
import { useState } from 'react'; | ||
import { Box, Stack, Button } from '@mui/material'; | ||
import { alpha, createTheme, styled } from '@mui/material/styles'; | ||
|
||
|
||
|
||
const tableColumns: GridColDef<GetAllLexEntriesQuery['lexiconAllEntries'][number]>[] = [ | ||
{ | ||
field: 'primary', | ||
headerName: 'Primary', | ||
width: 200, | ||
headerAlign: 'center', | ||
}, | ||
{ | ||
field: 'associates', | ||
headerName: 'Associates', | ||
width: 200, | ||
headerAlign: 'center', | ||
}, | ||
{ | ||
field: 'fields', | ||
headerName: 'Fields', | ||
width: 200, | ||
headerAlign: 'center', | ||
|
||
}, | ||
{ | ||
field: 'key', | ||
headerName: 'Key', | ||
width: 200, | ||
headerAlign: 'center', | ||
|
||
}, | ||
{ | ||
field: 'video', | ||
headerName: 'Video', | ||
width: 200, | ||
headerAlign: 'center', | ||
|
||
} | ||
]; | ||
|
||
|
||
const LexiconTable = () => { | ||
//manage rows | ||
const [rows, setRows] = useState<GetAllLexEntriesQuery['lexiconAllEntries']>([]); | ||
|
||
//fetch data | ||
const { data } = useGetAllLexEntriesQuery({ variables: { lexicon: '64b15233e535bc69dc95b92f' } }); | ||
const lexiconEntries = data?.lexiconAllEntries || []; | ||
|
||
//inital state of number of rows -> temp = 3 | ||
const [nRows, setNRows] = useState(3); | ||
const removeRow = () => setNRows((x) => Math.max(0, x - 1)); | ||
|
||
//needs to be be fixed and updated with actual data | ||
const addRow = () => { | ||
const newRow = { | ||
id: rows.length + 1, | ||
primary: 'new primary', | ||
associates: ['new associates'], | ||
fields: 'new fields', | ||
key: 'new key', | ||
video: 'new video', | ||
}; | ||
|
||
setRows((prevRows) => [...prevRows, newRow]); | ||
} | ||
|
||
return ( | ||
<Box sx={{ width: '100%' }}> | ||
<Stack direction="row" spacing={2}> | ||
<Button size="small" onClick={removeRow} disabled={nRows <= 0}> | ||
Remove Row | ||
</Button> | ||
<Button size="small" onClick={addRow}> | ||
Add Row | ||
</Button> | ||
</Stack> | ||
|
||
<div style={{display:'flex', flexDirection: 'column', width: '100%'}}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a div, I recommend using a Box or similar. Then the container will follow the theme provided settings at the global level. |
||
<DataGrid | ||
columns={tableColumns} | ||
rows={lexiconEntries} | ||
getRowId={({ key }) => key} | ||
/> | ||
</div> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default LexiconTable; | ||
|
||
|
242 changes: 242 additions & 0 deletions
242
packages/admin_view/src/components/LexiconTableTemp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import Button from '@mui/material/Button'; | ||
import AddIcon from '@mui/icons-material/Add'; | ||
import EditIcon from '@mui/icons-material/Edit'; | ||
import DeleteIcon from '@mui/icons-material/DeleteOutlined'; | ||
import SaveIcon from '@mui/icons-material/Save'; | ||
import CancelIcon from '@mui/icons-material/Close'; | ||
import { | ||
GridRowsProp, | ||
GridRowModesModel, | ||
GridRowModes, | ||
DataGrid, | ||
GridColDef, | ||
GridToolbarContainer, | ||
GridActionsCellItem, | ||
GridEventListener, | ||
GridRowId, | ||
GridRowEditStopReasons, | ||
GridSlots | ||
} from '@mui/x-data-grid'; | ||
import { randomId } from '@mui/x-data-grid-generator'; | ||
import { GetAllLexEntriesQuery, useGetAllLexEntriesQuery } from '../graphql/lexicon/lexicon.ts'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { TextField } from '@mui/material'; | ||
|
||
interface EditToolbarProps { | ||
setRows: (newRows: (oldRows: GridRowsProp) => GridRowsProp) => void; | ||
setRowModesModel: (newModel: (oldModel: GridRowModesModel) => GridRowModesModel) => void; | ||
} | ||
|
||
function EditToolbar(props: EditToolbarProps) { | ||
const { setRows, setRowModesModel } = props; | ||
|
||
const handleClick = () => { | ||
const id = randomId(); | ||
setRows((oldRows) => [ | ||
{ id, primary: '', associates: '', fields: '', key: '', video: '', isNew: true }, | ||
...oldRows | ||
]); | ||
setRowModesModel((oldModel) => ({ | ||
[id]: { mode: GridRowModes.Edit, fieldToFocus: 'key' }, | ||
...oldModel | ||
})); | ||
}; | ||
|
||
return ( | ||
<GridToolbarContainer> | ||
<Button color="primary" startIcon={<AddIcon />} onClick={handleClick}> | ||
Add record | ||
</Button> | ||
</GridToolbarContainer> | ||
); | ||
} | ||
|
||
type LexiconEntriesStatus = GetAllLexEntriesQuery['lexiconAllEntries'][number] & { | ||
isNew: boolean; | ||
id: string; | ||
}; | ||
|
||
export default function FullFeaturedCrudGrid() { | ||
const [rowModesModel, setRowModesModel] = React.useState<GridRowModesModel>({}); | ||
|
||
const { data } = useGetAllLexEntriesQuery({ variables: { lexicon: '64b15233e535bc69dc95b92f' } }); | ||
const lexiconEntries = useMemo( | ||
(): LexiconEntriesStatus[] => | ||
data?.lexiconAllEntries.slice(0, 10).map((entry) => ({ ...entry, id: entry.key, isNew: false })) || [], | ||
[data] | ||
); | ||
|
||
//currently, rows are empty | ||
const [rows, setRows] = React.useState<LexiconEntriesStatus[]>(lexiconEntries); | ||
|
||
useEffect(() => { | ||
setRows(lexiconEntries); | ||
}, [lexiconEntries]); | ||
|
||
useEffect(() => { | ||
console.log(rows); | ||
}, [rows]); | ||
|
||
const handleRowEditStop: GridEventListener<'rowEditStop'> = (params, event) => { | ||
if (params.reason === GridRowEditStopReasons.rowFocusOut) { | ||
event.defaultMuiPrevented = true; | ||
} | ||
}; | ||
|
||
const handleEditClick = (id: GridRowId) => () => { | ||
setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.Edit } }); | ||
}; | ||
|
||
const handleSaveClick = (id: GridRowId) => () => { | ||
setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.View } }); | ||
}; | ||
|
||
const handleDeleteClick = (id: GridRowId) => () => { | ||
setRows(rows.filter((row) => row.id !== id)); | ||
}; | ||
|
||
const handleCancelClick = (id: GridRowId) => () => { | ||
setRowModesModel({ | ||
...rowModesModel, | ||
[id]: { mode: GridRowModes.View, ignoreModifications: true } | ||
}); | ||
|
||
const editedRow = rows.find((row) => row.key === id); | ||
if (editedRow!.isNew) { | ||
setRows(rows.filter((row) => row.key !== id)); | ||
} | ||
}; | ||
|
||
const processRowUpdate = (newRow: LexiconEntriesStatus) => { | ||
const updatedRow = { ...newRow, isNew: false }; | ||
setRows(rows.map((row) => (row.key === newRow.key ? updatedRow : row))); | ||
return updatedRow; | ||
}; | ||
|
||
const handleRowModesModelChange = (newRowModesModel: GridRowModesModel) => { | ||
setRowModesModel(newRowModesModel); | ||
}; | ||
|
||
const columns: GridColDef<LexiconEntriesStatus>[] = [ | ||
{ | ||
field: 'primary', | ||
headerName: 'Primary', | ||
width: 200, | ||
headerAlign: 'center', | ||
editable: true | ||
}, | ||
{ | ||
field: 'associates', | ||
headerName: 'Associates', | ||
width: 200, | ||
headerAlign: 'center', | ||
editable: true | ||
}, | ||
{ | ||
field: 'fields', | ||
headerName: 'Fields', | ||
width: 200, | ||
headerAlign: 'center', | ||
editable: true, | ||
renderCell: ({ row }) => JSON.stringify(row.fields), | ||
renderEditCell: (params) => ( | ||
<TextField | ||
value={JSON.stringify(params.row.fields)} | ||
onChange={(e) => { | ||
const updatedValue = JSON.parse(e.target.value); | ||
params.api.setEditCellValue({ id: params.id, field: params.field, value: updatedValue }); | ||
}} | ||
variant="standard" | ||
/> | ||
) | ||
}, | ||
{ | ||
field: 'key', | ||
headerName: 'Key', | ||
width: 200, | ||
headerAlign: 'center', | ||
editable: true | ||
}, | ||
{ | ||
field: 'video', | ||
headerName: 'Video', | ||
width: 200, | ||
headerAlign: 'center', | ||
editable: true | ||
}, | ||
{ | ||
field: 'actions', | ||
type: 'actions', | ||
headerName: 'Actions', | ||
width: 100, | ||
cellClassName: 'actions', | ||
getActions: ({ id }) => { | ||
const isInEditMode = rowModesModel[id]?.mode === GridRowModes.Edit; | ||
|
||
if (isInEditMode) { | ||
return [ | ||
<GridActionsCellItem | ||
icon={<SaveIcon />} | ||
label="Save" | ||
sx={{ | ||
color: 'primary.main' | ||
}} | ||
onClick={handleSaveClick(id)} | ||
/>, | ||
<GridActionsCellItem | ||
icon={<CancelIcon />} | ||
label="Cancel" | ||
className="textPrimary" | ||
onClick={handleCancelClick(id)} | ||
color="inherit" | ||
/> | ||
]; | ||
} | ||
|
||
return [ | ||
<GridActionsCellItem | ||
icon={<EditIcon />} | ||
label="Edit" | ||
className="textPrimary" | ||
onClick={handleEditClick(id)} | ||
color="inherit" | ||
/>, | ||
<GridActionsCellItem icon={<DeleteIcon />} label="Delete" onClick={handleDeleteClick(id)} color="inherit" /> | ||
]; | ||
} | ||
} | ||
]; | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
height: 500, | ||
width: '100%', | ||
'& .actions': { | ||
color: 'text.secondary' | ||
}, | ||
'& .textPrimary': { | ||
color: 'text.primary' | ||
} | ||
}} | ||
> | ||
<DataGrid | ||
rows={rows} | ||
columns={columns} | ||
editMode="row" | ||
rowModesModel={rowModesModel} | ||
onRowModesModelChange={handleRowModesModelChange} | ||
onRowEditStop={handleRowEditStop} | ||
processRowUpdate={processRowUpdate} | ||
slots={{ | ||
toolbar: EditToolbar as GridSlots['toolbar'] | ||
}} | ||
slotProps={{ | ||
toolbar: { setRows, setRowModesModel } | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are on the right track here but I think it will be much easier to adapt the crud datagrid example from the MUI docs to our needs, rather than trying to code our own solution from scratch. I would recommend starting with the example at https://mui.com/x/react-data-grid/editing/ and editing it where necessary to handle our data. I think it should be as simple as just changing the
GridColDef
and adding in the query for the lexicon data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea having the implementation follow the CRUD sample will make life easier. This is pretty close.
Just to add some pointed areas to have this implementation match the sample.
DataGrid
slot
property like in the demo and removing the "Remove Row` button