Trying to highlight cell in React-data-grid - reactjs

I am trying to render the background color on certain columns a certain color, in my react-data-grid (adazzle).
The problem is that I am getting this white padding around my cells :
How do I get rid of this white padding?
I simply followed the way the formatter was used in this example :
http://adazzle.github.io/react-data-grid/examples.html#/custom-formatters
My ColumnFormatter looks like this :
const ColumnFormatter = React.createClass({
propTypes: {
value: React.PropTypes.number.isRequired
},
render() {
return (
<div style={{background: '#f2f9de'}}>
{this.props.value}
</div>);
}
});
And I set up my columns like this :
this._columns.push({
key: i,
name: "blah " + i,
sortable: true,
resizable: true,
formatter: ColumnFormatter
})

this is a simple way but may not work exactly how you want...
somefile.js
<div id="someTableId">
<ReactDataGrid
...
/>
</div>
somefile.css...
#someTableId div.react-grid-Row div.react-grid-Cell:nth-of-type(1),
#someTableId div.react-grid-Row div.react-grid-Cell:nth-of-type(2){
background-color: #f2f9de;
}
also, i like to make things simple when i do a react-data-grid cell formatter (note: with the css way above you don't need the formatter but just to show how i simplify formatters or if you do it another way with a formatter)...
this._columns.push({
key: i,
name: "blah " + i,
sortable: true,
resizable: true,
formatter: (props)=>(<div style={{background: '#f2f9de'}}>{props.value}</div>;
})
and actually i would have done a className instead of style and used a css file but that just what i prefer.

In the formatter you can give the background as you are giving right now but you can add a css like this in your custom css file:
#parentDivOfGrid div.react-grid-Row div.react-grid-Cell {
padding-left: 0px ;
padding-right: 0px;
line-height: 2.1;
text-align: center;
}
But your React data grid should be inside a div like this:
<div id="parentDivOfGrid">
<ReactDataGrid
...
/>
</div>
You might want to play around with the line height value in css according to your grid.

both of the answers here seem to apply highlighting to all of the cells. How would I go about selectively highlighting cells perhaps by specifying class names

Set the root div style to -8 margin left and right, and 8 padding left and right
return (
<div style={{
background: '#f2f9de',
marginLeft: -8,
marginRight: -8,
paddingLeft: 8,
paddingRight: 8}}>
{this.props.value}
</div>
);

Try loosing the formatter altogether, in this simple example. Create a class in the css file. Set the cellClass equal to the class you just created. The background of the column should be fully colored. Both the value and the padding.
My environment:
"react": "^17.0.2",
"react-data-grid": "^7.0.0-beta.7",
app.css
.green {
background-color: green;
}
app.js
this._columns.push({
key: i,
name: "blah " + i,
sortable: true,
resizable: true,
cellClass: 'green`
})

Related

Different colour for react-table headers

Having trouble with styling table created by react-table library.
Please find the design below.
Table is created the way it is shown in documentation. Is it possible to colour headers as shown in design?
Thank you in advance
P.S.
Here is the code snippet from documentation
To create headers I need to create an array. So i cant change style for a specific header (actually I can provide element to render, but it will be rendered inside the element)
Check this, there are class names added to the data where the data should be styled differently. I think that's what you need.
You can use any react component or JSX to display content in column headers, cells and footers.
const columns = [
{
Header: () => (
<span>
<i className="fa-tasks" /> Progress
</span>
)
}
]
kindly check on this link for further details
https://github.com/tannerlinsley/react-table/tree/v6#custom-cell-header-and-footer-rendering
Had to use a workaround.
As Taha Jamil mentioned you can any JSX to render inside of the Header. By adjusting some styles you will get something like this:
{
Header: () => {
return(
<div
style={{
backgroundColor: "red",
position: "absolute",
width: "100%",
height: "100%",
top: "0px",
left: "0px",
padding: "12px 20px",
}}
>
Title
</div>
);
}
}
Not the best solution, but works

React material-table - set row height

First of all, I have tested all possible solutions I have founded and I still didn't solve my problem.
I want to put a shorter height in my material-table rows. This is what it looks like right now.
Table
I would like my rows to have a height similar to the headers in that image. I have tried many things, one of them was the following:
options={{
headerStyle: {
whiteSpace: "nowrap",
height: 20,
maxHeight: 20,
padding: 0
},
rowStyle: {
height: 20,
maxHeight: 20,
padding: 0
},
}}
I would really appreciate it if someone can help me.
You can set options in material-table
options={{
padding: "dense",
}}
I had the same problem with React Material-Table. I fixed adding this in the global index.css of the project:
.MuiTableCell-root {
padding: 0 14px !important;
}
and then i could modify the height in the rowStyle in the options of the Material-Table component:
options = {
rowStyle: {
height: "20px"
}
}
you need to use withStyles and update the specific element class style, so it will reflect to all the elements. Check the working example as you expected here : codesandbox
import MuiTableCell from '#material-ui/core/TableCell';
const TableCell = withStyles(theme => ({
root: {
height: 10,
padding:0
}
}))(MuiTableCell);
Much more simplified, overwrite padding of 16px to smaller size...<TableCell style={{padding:5px}} ...>.
I think the link below might be helpful for you.
customizing material ui table

Move expandIcon icon to right and remove empty space from expanded table Ant Design and React.js

I need your help to solve this problem.
I am using the Ant Design table and I am stuck here. I want the expand row icon should be at the right(Next to delete in the given sandbox code) of table current it is on left and when we expand a row the expanded contents leave a small space in left, I want to remove it. i.e. it should not have any extra space in left. Please help me out, guys.
Sandbox code: https://codesandbox.io/s/ancient-mountain-ft8m1?file=/index.js
A potential solution could be to make use of expandIconColumnIndex, a prop that can be passed to an expandable antd Table component as well as adding an extra row for the expander. By doing this, you can set the expandIconColumnIndex to the index of the last (empty) row (in your case the index is 4), this way the icon will appear to the right of the Delete action. This will avoid creating space on the left and will move the icon to the right most column. This is the method that requires the least refactor given your code. Below is your updated columns.
const columns = [
{ title: "Name", dataIndex: "name", key: "name" },
{ title: "Age", dataIndex: "age", key: "age" },
{ title: "Address", dataIndex: "address", key: "address" },
{
title: "Action",
dataIndex: "",
key: "x",
render: (r) => <div>
<a>Delete</a>
<a onClick={()=>expandRows(r.key)}> ex</a>
</div>
},
{ title: "", dataIndex: "", key: "expand", width: 1},
];
And here is the updated Table.
<Table
expandIconColumnIndex={4}
columns={columns}
dataSource={data}
expandIcon={({ expanded, onExpand, record }) =>
expanded ? (
<UpOutlined style={{float: 'right'}} onClick={e => onExpand(record, e)} />
) : (
<DownOutlined onClick={e => onExpand(record, e)} />
)
}
expandable={{
expandedRowRender: record => <p style={{ margin: 0 }}>{renderTable()}</p>
}}
/>
In order to remove the left space from the nested table, you will need to overwrite Ant Design's CSS, which includes more padding for the nested table to act as indentation and differentiate it from the rest of the table. I recommend you keep it the way it is but if you really don't want to have that space you can overwrite their style by adding the className parentTable to your first table, and then nestedTable for the nested table (found in renderTable). Then add the following CSS to your style sheet.
.parentTable
table
tbody
.ant-table-expanded-row.ant-table-expanded-row-level-1
> td {
padding: 0px !important;
}
.nestedTable > div > div > div {
width: 100%;
margin-left: 0px !important;
margin-right: 0px !important;
}
Here is the working codesandbox.
For those coming to this in the future, the correct way to do this is to use the antd table props.
The expandIcon prop of the antd table takes a function that returns a react node.
customExpandIcon(props) {
if (props.expanded) {
return <a style={{ color: 'black' }} onClick={e => {
props.onExpand(props.record, e);
}}><Icon type="minus" /></a>
} else {
return <a style={{ color: 'black' }} onClick={e => {
props.onExpand(props.record, e);
}}><Icon type="plus" /></a>
}
}
Then inside of your table put:
<Table
expandIcon={(props) => this.customExpandIcon(props)}
...
/>
This will allow you to use any combination of icons from antd in place of the expand/minimize buttons on the antd table.
Hope this 'll help you .

How apply style for class child in override theme?

How apply style for class child in override theme?
I have this code
<button class="MuiButtonBase-root-359 MuiToggleButton-root-630 MuiToggleButton-selected-632" tabindex="0" type="button" value="1">
<span class="MuiToggleButton-label-633">Jan</span>
<span class="MuiTouchRipple-root-549"></span>
</button>
Here I have 3 css class (MuiButtonBase-root, MuiToggleButton-root and MuiToggleButton-selected)
So I need to apply a custom color to label inside "selected" class. I tried this override in my theme
MuiToggleButton: {
root:{
backgroundColor: '#5f5f5f'
},
selected: {
backgroundColor: '#a1b0e2a8 !important',
label: { //this not apply to "label" inside "selected" parent
color: '#0000ff !important'
}
},
label: {
selected: { //this not work too
color: '#ff0000 !important'
},
color: '#ffffff' //this apply to all MuiToggleButton-label css class
}
}
But after run "label" inside "selected" is not recognized, only "label" white. selected > label not work.
I look explication for this but only found css styles applied directly to the component or js code for this.
There any way for do this or is better apply the old css (file) way?
Using css file this work
button[class*="MuiToggleButton-selected"] span[class^="MuiToggleButton-label"]{
color: #0000ff;
}
I appreciate any resource (book, tutorial, etc) for giving me more knowledge about this.
Tks for advance
I found a similar question here
selected: {
backgroundColor: '#a1b0e2a8 !important',
'&>span': { // I need put > after &
color: '#0000ff !important'
}
},

React Select auto size width

When using react-select it is not auto sizing by option value, but using width:100% as you can see in picture:
Options are short:
getOptions() {
return [
{ value: 'AND', label: 'AND' },
{ value: 'OR', label: 'OR' }
]
}
And code which produces it:
<Select
options={this.getOptions()}
value={value}
autosize={true}
clearable={false}
simpleValue
/>
Is there any way to make react-select to show these values with auto sizing, so select box would be the same as option length, and I could, for example, center this select box in <div>?
Updated 14.11.2017
Full example can be seen in this jsFiddle
Inline styles did not work for me.
I just wrapped the Select component in a div and gave the div the width I wanted.
<div style={{width: '300px'}}>
<Select
menuPlacement="auto"
menuPosition="fixed"
etc, etc..
/>
</div>
SOLUTION 1
You can leverage React's inline styles by updating the components' width based on the length of the selected option.
Let me explain further: Say the selected value is HelloWorld. This string is of length 10. We could guess that each character accounts for say 8px each on average (total guess I have no clue at all). Thus, the width of this word is around 8*10=80px, right ? Also, there are some controls after the word (the carret and the cross) and we need some minimum padding: together they may be of 100px width. Then here you have it: your div's width should be ( 8px * 10 letters ) + 100px = 180px.
More precisely, the correct formula is something like:
(average_letter_size * selected_value.length) + other_elements_sizes
When selected_value changes, so does its length, and therefore the width of the div gets updated with the new total.
Example: if the selected value is now Lorem Ipsum dolor sit amet, the length is now 26. By applying the formula we get a larger width of : (8px * 26 letters) + 100px = 308px.
For this to work in react, here is a snippet:
<Select
style={{width: `${(8*this.state.selectedOption2.length) + 100}px`}}
className="select-custom-class"
name="form-field-name"
value={this.state.selectedOption2}
options={options2}
onChange={(value) => { this.setState({ selectedOption2: value.value }); }}
/>
As you can see I added :
style={{width: `${(8*this.state.selectedOption2.length) + 100}px`}}
to your component. Whenever the state gets updated, everything is propagated including the width of the component.
See a working example in this fiddle.
Eventually, you want to fine-tune the rules and averages to your needs. I also suggest you apply a letter size depending on the number of capital and lowercase letters in the selected value.
SOLUTION 2 (edit)
I came up with a pure CSS solution if you want. It should be better tested against your design, but this should work:
/* .Select-value comes with an absolute position to stack it below .Select-input */
/* we need to scratch that in order for us to be able to let the div grow depending on its content size */
.Select-placeholder, .Select--single > .Select-control .Select-value {
position: relative;
padding-left: 0;
}
/* All these 3 classes come with ugly "table" display...*/
.Select-control, .Select-clear-zone, .Select-arrow-zone {
display: inherit;
}
/* here is the trick: we display the wrapper as flex in order to make it fit in height*/
/* we flip positions of .Select-value and .Select-input using row-reverse in order to have a nice input to the left and no to the right */
.select-custom-class .Select-multi-value-wrapper {
display: flex;
flex-direction: row-reverse;
}
/*we put our controls back to a better center position */
.Select-clear-zone {
position: absolute;
top: 8px;
right: 20px;
}
.Select-arrow-zone {
position: absolute;
top: 8px;
right: 0px;
}
See a working fiddle (I changed some of the examples for better illustration)
Tell me what you think. :)
I borrowed this from aidan-keay on the repo thread, but adding this to the custom styles prop worked for me:
menu: (base) => ({
...base,
width: "max-content",
minWidth: "100%"
}),
if you're using react-select v3 you can use customStyles object:
const customStyles = {
container: provided => ({
...provided,
width: 150
})
};
<Select
styles={customStyles}
{...otherProps}
/>
9/2020
Hey guys :) the solution is so simple than that workarounds !
the problem in these classes __placeholder, __single-value
just add this css to both of them and you will get auto sized react-select
.CUSTOM_PREFIX__single-value,
.CUSTOM_PREFIX__placeholder {
position: static;
transform: none;
max-width: none;
}
In the above example the prop classNamePrefix will equal CUSTOM_PREFIX
classNamePrefix="CUSTOM_PREFIX"
09/2021 (react-select v4)
Adding position: static and transform: none will scale the select-container accordingly.
placeholder: (provided) => ({
...provided,
position: 'static',
transform: 'none',
}),
singleValue: (provided) => ({
...provided,
position: 'static',
transform: 'none',
}),
Update: for people who are using React-Select for a "tags autocomplete" feature but are having trouble where it sets a style width that is too narrow based on the previous tag you searched, this is what works for me:
Set a style of:
.myCustomPrefix__value-container > div {
width: auto !important;
}
Set classNamePrefix="myCustomPrefix" in the component (docs).
Old answer:
See the official docs at https://react-select.com/styles#style-object
I originally thought that setting width to "auto" for option worked for me:
const customStyles = {
option: (styles, { data, isDisabled, isFocused, isSelected }) => {
return {
...styles,
fontSize: '12px',
textAlign: 'left',
width: 'auto',
}
},
}
...
return (
//https://react-select.com/props
<AsyncSelect
components={animatedComponents}
isMulti
// isDisabled={isLoading}
// isLoading={isLoading}
onChange={handleChange}
onInputChange={handleInputChange}
cacheOptions
defaultOptions
defaultMenuIsOpen={false}
closeMenuOnSelect={closeMenuOnSelect}
placeholder={placeholder}
loadOptions={promiseOptions}
value={selectedOptions}
styles={customStyles}
formatOptionLabel={formatOptionLabel}
/>
)

Resources