React DataTable customization - reactjs

Is it possible to style the headers of the react-data-table-component? I tried this
columns: [
{
name: "Name",
selector: "name",
sortable: true,
style: {
background: "orange",
},
},
],
But it styles all the cells underneath that header, not the header itself.
Please let me know if there is documentation that I study this component API from.

I didn't find any mechanism to customize only the header cells, but you could style them with CSS selectors by passing an id in each column object and using the rdt_TableCol class
columns: [
{
name: "Name",
selector: "name",
id: "name",
sortable: true,
style: {
background: "orange",
},
},
],
and in a CSS file
[data-column-id="name"].rdt_TableCol {
background-color: orange;
}
https://codesandbox.io/s/style-header-cell-rdt-c1y35
of course, this method is susceptible to internal changes in the lib, make sure to fix the version and revisit the code if you upgrade the version

In the DataTable props you can assign a style to the customStyles property like so:
import { tableCustomStyles } from './tableStyle.jsx';
<DataTable
customStyles={tableCustomStyles}
/>
And then in the tableStyle.jsx file you can customize all properties of the table like this:
const tableCustomStyles = {
headCells: {
style: {
fontSize: '20px',
fontWeight: 'bold',
paddingLeft: '0 8px',
justifyContent: 'center',
backgroundColor: '#FFA500'
},
},
}
export { tableCustomStyles };
Here is the API reference: https://react-data-table-component.netlify.app/?path=/docs/api-custom-styles--page
And here is the styles.ts file from the repo that shows some of the properties you can manipulate: https://github.com/jbetancur/react-data-table-component/blob/master/src/DataTable/styles.ts

Related

MUISelect style overwrite in global theme

I want to overwrite a specific style for multiple components. For now it works for all components, but not the the Select.
What I'm doing is:
MuiSelect: {
styleOverrides: {
select: {
background: themePalette.palette.background.paper,
marginLeft: '0rem',
borderRadius: '10rem',
},
iconOutlined: {
background: themePalette.palette.background.default,
color: themePalette.palette.primary.main,
borderRadius: '10rem',
},
},
variants: [
{
props: { size: 'small' },
style: {
borderRadius: '0.4rem',
select: {
borderRadius: '0.4rem',
},
iconOutlined: {
borderRadius: '0.4rem',
},
},
},
],
},
The style in the "styleOverrides" section get overwritten like expected. But the variant never is applied.
For other elements like TextField, ToggleButtonGroups, etc. it works. But somehow it does not work for the Select.
Also when I directly overwrite the same properties in the Select control via the sx prop, it's not applied.
sx={{
input: {
borderRadius: '0.4rem',
},
select: {
borderRadius: '0.4rem',
backgroundColor: theme.palette.grey[300],
},
borderRadius: '0.4rem !important',
}}
Here I also don't have any effect. The border radius is still unchanged at '10rem' like it's defined in the stylesOverrides.
Is this a bug, or am I doing something wrong for the Select?
style in theme props and sx work with css selectors. Not with pre-defined slots like styleOverrides
sx={{
'& .MuiSelect-select': {
...styles here,
...
}
}}

Material-ui DateRangePicker custom styling with Typescript

I am using material ui DateRangePicker and want to customize MuiPickersDesktopDateRangeCalendar style. But I am unable to override this with global theme as this is not recognized in ThemeOptions with typescript.
I tried to override from this reference (https://next.material-ui-pickers.dev/guides/css-overrides), but I am getting below error "..is missing the following properties from type 'ThemeOptions': MuiPickersDesktopDateRangeCalendar, MuiPickersDay"
Option 1:
interface Theme {
//for typescript usage, I think it should go inside overrides, but there is no overrides option supported
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: React.CSSProperties['padding']
},
calendar: {
minWidth: React.CSSProperties['minWidth']
minHeight: React.CSSProperties['minHeight']
},
},
MuiPickersDay: {
root: {
width: React.CSSProperties['width']
height: React.CSSProperties['height']
},
},
}
interface ThemeOptions {
//for typescript usage, I think it should go inside overrides, but there is no overrides option supported
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: React.CSSProperties['padding']
},
calendar: {
minWidth: React.CSSProperties['minWidth']
minHeight: React.CSSProperties['minHeight']
},
},
MuiPickersDay: {
root: {
width: React.CSSProperties['width']
height: React.CSSProperties['height']
},
},
}
const theme = createMuiTheme({
overrides: {
MuiPickersDesktopDateRangeCalendar: {
arrowSwitcher: {
padding: '0.8rem 0.8rem 0.4rem 0.8rem',
},
calendar: {
minWidth: '20rem',
minHeight: '19rem',
},
},
MuiPickersDay: {
root: {
width: '1.4rem',
height: '1.4rem',
},
},
},
})
Option 2: There is no need to declare in theme interface, and upgrade #material-ui/core/styles to any specific version. I tried that as well, but it is not working.
Please let me know if there is any compatible version of "#material-ui/core/styles" with "#material-ui/pickers": "^4.0.0-alpha.12".

Mui-datatables fixed column on scroll - (left or right)

How to fix the column on the screen during the scroll? Using the Mui-datatables library, like datatables on doc. doc
In the mui-datatables there is a fixedSelectColumn property, but I was unable to select the column or configure the scroll.
My options :
const options = {
filter: true,
filterType: 'multiselect',
textLabels : TextLabels,
responsive: 'scroll',
fixedHeader: true,
tableBodyHeight: '100%',
selectableRows: false,
fixedSelectColumn: true,
};
The fixedSelectColumn prop is for the "selection" elements, i.e., the check boxes. I don't think the MUI-Datatables, as of this writing has props like this feature that you have linked pertaining to the jQuery datatables.
However, upon looking at this source code, we can see that some of the "fixed" columns utilize the CSS position: sticky property. So, one way to implement fixed columns is to style the cells & header cells as such:
const columns = [
{
name: "Name",
options: {
filter: true,
setCellProps: () => ({
style: {
whiteSpace: "nowrap",
position: "sticky",
left: "0",
background: "white",
zIndex: 100
}
}),
setCellHeaderProps: () => ({
style: {
whiteSpace: "nowrap",
position: "sticky",
left: 0,
background: "white",
zIndex: 101
}
})
}
},
...

How to configure text bold for certain column in react tabulator

I am using react-tabulator for my app:
https://www.npmjs.com/package/react-tabulator
I want to format one of the column to have bold text.
I have tried formatter: "bold"
columns = () =>{
return (
[
{ title: "Title", field: "title1", headerSort: false, width: 180, formatter:"bold", hozAlign: "right" },
]}
I tried add a cssClass to the column
columns = () =>{
return (
[
{ title: "Title", field: "title1", headerSort: false, width: 180, cssClass: 'data-header, hozAlign: "right" },
]}
with definition of the class in styles.css
.data-header { font-weight: "bold" }
Nothing works.
Is it possible to also style a particular row in react-tabulator?
Thanks for the help, all the experts out there.
I found the solution, just for the benefit of others.
I need to configure the styles.css with the following for the cssCass to work:
.tabulator .data-header {
background-color: #ccc;
font-weight: bold;
}

Extending React-Table using composition - issues with props

I'm trying to create a component based on react-table using composition so that we can use a customised version everywhere without repeating boiler plate. I'm using Typescript while react-table doesn't use it. I don't see any examples around of doing this so here's what I've come up with so far:
import * as React from 'react'
import ReactTable, {TableProps} from 'react-table'
import './../components/react-table-styling.css'
import 'react-table/react-table.css'
import Pagination from './pagination'
import { Paper } from '#material-ui/core'
export interface IReactTableProps{
textCols:Array<string>;
}
export class ExtReactTable extends React.Component<IReactTableProps & TableProps> {
static defaultProps = {
}
constructor(props: IReactTableProps & TableProps) {
super(props);
}
rows = (state, rowInfo) => {//set taller height and vertical centering
return {
style: { height:40, display:'flex', alignItems: 'center'},
}
}
headerRows = (state, rowInfo, column) => {
return {
style: { height: 40, display:'flex', alignItems: 'center',
paddingLeft: (column.Header === 'Product'||column.Header ===
'Client') ? '20px':'',
justifyContent: (column.Header === 'Product'||column.Header ===
'Client') ? '': 'center' }
}
}
render() {
return(
<Paper>
<ReactTable style={{cursor:'pointer', fontSize: 13, background:'#fff', borderLeft: 0, borderRight: 0}} PaginationComponent={Pagination}
getTrProps={this.rows} getTheadThProps={this.headerRows} />
</Paper>)
}
}
Trying to get it initially working, I use it like this:
import { ExtReactTable } from '../../components/ext-react-table';
<ExtReactTable data={products} textCols={["Product"]} columns={[
{Header: "Product", accessor: "permitProductName", minWidth: 200, style: { paddingLeft:20}},
{Header: "Client",accessor: "clientName", minWidth: 300, style: { paddingLeft:20}},
{Header: "Spaces", accessor: "spaces", minWidth:80, style: { textAlign: 'right', paddingRight:'4%'}},
{Header: "Active VRMs", accessor: "activeVRMs", minWidth:80, style: { textAlign: 'right', paddingRight:'4%'}},
{Header: "Max VRMs", accessor: "maxVRMs", minWidth:80, style: { textAlign: 'right', paddingRight:'4%'}},
{Header: "Start Date", accessor: "startDate", style: { textAlign: 'center'}, Cell: (props) => { return <span>{dateTimeHelper.shortDate(props.original.startDate)}</span>}},
{Header: "End Date", accessor: "endDate", style: { textAlign: 'center'}, Cell: (props) => { return <span>{(props.original.endDate !== "0001-01-01T00:00:00" && props.original.endDate !== null) ? dateTimeHelper.shortDate(props.original.endDate) : '-'}</span>}}
]}
defaultSorted={[ { id: "permitProductName", asc: true }]}
defaultPageSize={10} className={" -highlight"}
/>
but it complains that I am not supplying the optional prop 'loading' (and if you supply that then dozens of other props of react-table for which you wouldn't want to supply values). What's the approach to solving this issue? Do you have to provide dozens of values as defaultProps or is there some other way? Thanks
It looks like loading and so forth are required properties of the TableProps interface (here), but ReactTable actually uses Partial<TableProps> as its props type (here). If you replace TableProps with Partial<TableProps> in your code to be consistent, then the errors should go away.

Resources