Created
July 17, 2020 15:41
-
-
Save Jon20111/f35a65e8575a18fef0a58cd2b400f880 to your computer and use it in GitHub Desktop.
Material-Table example
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
import React, { FC, useState } from "react"; | |
import MaterialTable from "material-table"; | |
import DeleteIcon from "@material-ui/icons/Delete"; | |
import SearchIcon from "@material-ui/icons/Search"; | |
import SaveIcon from "@material-ui/icons/Save"; | |
import { Button } from "@material-ui/core"; | |
import { useStyles } from './material-table-wrapper.styles'; | |
interface Column { | |
name: string; | |
occupation: string; | |
age: number; | |
} | |
export const MaterialTableWrapper: FC = () => { | |
const classes = useStyles(); | |
const [dataStore, setDataStore] = useState([ | |
{ name: "Jon", job: "Software Dev", age: 29 } | |
]); | |
return ( | |
<div style={{ maxWidth: '100%' }}> | |
<MaterialTable | |
columns={[ | |
{ | |
title: "Name", | |
field: "name" | |
}, | |
{ | |
title: "Occupation", | |
field: "job" | |
}, | |
{ | |
title: "Age", | |
field: "age", | |
type: "numeric" | |
} | |
]} | |
data={dataStore} | |
title="Material-Table Demo" | |
icons={{ | |
Clear: (() => <DeleteIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>>, | |
Search: (() => <SearchIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>>, | |
ResetSearch: (() => <DeleteIcon />) as unknown as React.ForwardRefExoticComponent<React.RefAttributes<SVGSVGElement>> | |
}} | |
actions={[ | |
{ | |
icon: () => <SaveIcon />, | |
tooltip: "Save User", | |
onClick: (event, rowData) => alert("You saved " + (rowData as unknown as Column).name) | |
} | |
]} | |
components={{ | |
Action: props => ( | |
<Button | |
onClick={event => props.action.onClick(event, props.data)} | |
color="primary" | |
variant="text" | |
style={{ textTransform: "none" }} | |
size="small" | |
> | |
Save | |
</Button> | |
) | |
}} | |
options={{ | |
headerStyle: { | |
backgroundColor: "#01579b", | |
color: "#FFF" | |
} | |
}} | |
/> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See a video of coding a material-table for react here:
https://youtu.be/3bg5w8JNXII