CSS for colouring one row of antd table in react - css

This is the CSS for the antd style I'm using
style.css
.ant-table-tbody > tr > td, .ant-table-thead > tr > th
{
padding:4px;
}
tr:nth-child(odd){
background: #f1e6ff;
}
tr:nth-child(even){
background: white;
}
thead[class*="ant-table-thead"] th{
background-color:#000 !important;
color: white;
font-weight: bold;
border-color: #000;
text-align: center;
}
.table_btn
{
margin:0 !important;
}
.ant-btn
{
margin:0;
}
.ant-table-tbody > tr:hover > td {
color: #fff;
}
index.less
#import "callout";
#import 'e-commerce';
#import "pricing-tables";
#import "login";
#import "dashboard";
#import "error";
#import "editor";
#import "testimonials";
tr:nth-child(odd){
background:inherit !important;
}
.ant-modal-content {
.ant-modal-close{
color: #fff !important;
}
.ant-modal-header {
background-color: #000000;
.ant-modal-title {
color: #fff !important;
}
}
}
.table-wrapper {
.ant-btn {
padding: 0 10px;
height: 30px;
font-size: 13px;
> .anticon {
+ span {
margin-left: 5px;
}
}
&.ant-btn-success {
color: #3d8918;
border-color: #d9d9d9;
&:hover {
background-color:#3d8918;
color: #fff;
}
}
&.ant-btn-danger {
color: #c70d17;
background-color:#fff;
&:hover{
background-color:#c70d17;
color: #fff;
}
}
}
.actions {
text-align: right;
.ant-input {
border-radius: 2px;
padding:0 10px;
font-size: 13px;
height: 30px;
}
}
.table-layout {
.ant-table-small{
> .ant-table-content{
> .ant-table-body {
margin: 0 !important;
> table {
> .ant-table-tbody{
> tr{
> td{
padding: 2px 8px !important;
font-size: 13px !important;
text-align:center;
min-width: 80px;
.ant-btn {
width:100px;
}
}
}
}
}
}
}
index.js
<Table
className="table-layout"
columns={this.state.columns}
dataSource={filteredData}
rowClassName='data-row'
bordered={true}
size={"small"}
onRowDoubleClick={ (record, index, event) => this.handleEditModal(record) }
onRowClick={(record, index, event) => this.handleRowClick(record)}
loading={this.state.loading}
pagination={{ pageSize: 14 }}
/>
This is how Table is used in the index page. style.css and index.less are the pages for CSS.
Can anybody help me to write one CSS in this page for making one row green color ?
I want to make one row green based on condition.
I need the CSS
I need to call the CSS in the page where code is

I found two ways to do this as of now:
One way is to use rowClassName prop of Table:
.table-row-light {
background-color: #ffffff;
}
.table-row-dark {
background-color: #fbfbfb;
}
<Table
rowClassName={(record, index) => index % 2 === 0 ? 'table-row-light' : 'table-row-dark'}
columns={columns}
dataSource={dataSource}
loading={loading}
/>
Second way is to use plain CSS
.table-striped-rows tr:nth-child(2n) td {
background-color: #fbfbfb;
}
.table-striped-rows thead {
background-color: #f1f1f1;
}
<Table
className="table-striped-rows"
columns={columns}
dataSource={dataSource}
loading={loading}
/>
Note that rowClassName works only for rows, so for CSS on table headers we can only use plain CSS like above.

See if these Jsfiddle links helps you. They show 2 ways to change backgroung-color your rows.
https://jsfiddle.net/2b2376a4/
https://jsfiddle.net/2b2376a4/
data: [
{id:1, name: 'one', color: '#fff'},
{id:2, name: 'two', color: '#eee'},
{id:3, name: 'three', color: '#ddd'}
]
for first link
render(text, record) {
return {
props: {
style: { background: record.color },
},
children: <div>{text}</div>,
};
},
for second link
rowClassName={(record) => record.color.replace('#', '')}

if you want to use styled-components you can wrap it like this
const StyledTable: React.FC<TableProps<T>> = styled(Table)`
.ant-table-header th {
background-color: green;
}
`
export const Table = () => (
<StyledTable
columns={columns}
dataSource={data}
...
/>

Related

How can I change css for the React code depending on the Redux state (true or false)

I'm using the react-calendar. In the UI you can choose the starting date of a range. The end of the range is made automatically depending on what user choose. They can choose between 7, 14, 21, 28 days. In component the range is set to false as if it is true user has to click second time and then I can't use automatic end date selection. This works fine.
But I've added a checkbox (with weekends). It should work in the way that if it is not checked the range excludes weekends. I can do it only visually changing the css file. I've prepared two css files where in one weekends background is white. The problem is that it works fine at the beginning. Then when I check the checkbox it shows the range with weekends but when i uncheck the checkbox nothing changes. I think I know where is the problem but I can't solve it...
The Calendar.js file:
import "react-calendar/dist/Calendar.css";
//import './Calendar.css';
//I use default react-calendar css plus the customized one
import React from "react";
import Calendar from "react-calendar";
import { setDateAction } from "./calendarSlice";
export const CalendarFull = (props) => {
const { chosenDate, chosenDuration, withWeekends, dispatch } = props;
// I take the chosenDuration and pass through dispatch to use it in the calendar Reducer (setCurrentDateReducer)
// where I need it for adding a range
if (withWeekends === true) {
const x = require('./Calendar.css');
} else {
const x = require("./Calendar1.css");
}
const onChangeSetDate = (date) => {
dispatch(setDateAction(date, chosenDuration));
};
return (
<div className="calendar">
<div className="calendar-container">
<Calendar
onChange={onChangeSetDate}
value={chosenDate}
minDate={new Date()}
/>
</div>
</div>
);
};
The Calendar.css file:
.react-calendar {
width: 400px;
max-width: 100%;
background-color: #fff;
color: #222;
border-radius: 8px;
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
font-family: Arial, Helvetica, sans-serif;
line-height: 1.125em;
margin-top: 2rem;
margin-left: 1rem;
}
.react-calendar__navigation button {
color: #6f48eb;
min-width: 44px;
background: none;
font-size: 16px;
margin-top: 8px;
}
.react-calendar__navigation button:enabled:hover,
.react-calendar__navigation button:enabled:focus {
background-color: #f8f8fa;
}
.react-calendar__navigation button[disabled] {
background-color: #f0f0f0;
}
abbr[title] {
text-decoration: none;
}
.react-calendar__month-view__days__day--weekend {
color: #d10000;
}
.react-calendar__tile:enabled:hover,
.react-calendar__tile:enabled:focus {
background: #6565b8;
color: #6f48eb;
border-radius: 6px;
}
.react-calendar__tile--now {
background: white;
border-radius: 6px;
font-weight: bold;
color: #0d9924;
}
.react-calendar__tile--now:enabled:hover,
.react-calendar__tile--now:enabled:focus {
background: #6f48eb33;
border-radius: 6px;
font-weight: bold;
color: white;
}
.react-calendar__tile--hasActive:enabled:hover,
.react-calendar__tile--hasActive:enabled:focus {
background: #a4a4e2;
}
.react-calendar__tile--active {
background: #6f48eb;
border-radius: 6px;
font-weight: bold;
color: white;
}
.react-calendar__tile--active:enabled:hover,
.react-calendar__tile--active:enabled:focus {
background: #a4a4e2;
color: white;
}
.react-calendar--selectRange .react-calendar__tile--hover {
background-color: #a8a8f3;
}
.react-calendar__tile--range {
background: #a4a4e2;
color: white;
border-radius: 0;
}
/*.react-calendar__month-view__days__day--weekend {
color: #d10000;
background-color: white;
}*/
.react-calendar__tile--rangeStart {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
background: #a4a4e2;
color: white;
}
.react-calendar__tile--rangeEnd {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
background: #a4a4e2;
color: white;
}
In the Calendar1.css at the end I have:
.react-calendar__month-view__days__day--weekend {
color: #d10000;
background-color: white;
}
This is the only difference.
Is there any way to make it working or maybe some completly diffferent approach?
I've tried a different approach and modified the Calendar.js file like that:
import './Calendar.css';
.
...
return (
<div>
<Calendar
onChange={onChangeSetDate}
value={chosenDate}
minDate={new Date()}
className={
withWeekends
? "react-calendar"
: "react-calendar__month-view__days__day--weekend1"
}
/>
</div>
);
and in the css file:
.react-calendar__month-view__days__day--weekend1 {
color: #d10000;
background-color: white;
}
I doesn't change weekends but changes the color of days of the week - like Mon, Tue, etc.
In a separate file the same changes weekends.
I'm quite confused and running out of ideas.
I've also tried:
className={
withWeekends
? "react-calendar"
: "react-calendar1"
}
with in css:
.react-calendar1__month-view__days__day--weekend {
color: #d10000;
background-color: white;
}
Result - nothing changes. I'm not good at BAM (quite bad I would say)...

SASS/SCSS - Making a selector that will "back out" to modify the parent of the selector

I would like to contain all relevant styles for a selector within a single code block, so that it can be easily referenced.
In my application, a selectors effective styles will be altered dramatically depending on the context in which it sits. For instance, let's assume this CSS:
.container.theme-dark .message
{
font-size: 16px;
background-color: black;
color: white;
}
.container.theme-light .message
{
font-size: 16px;
background-color: white;
color: black;
}
Then, imagine I have the following HTML:
<div>
<div class="container theme-dark">
<div class="message">Hello World</div>
</div>
<div class="container theme-light">
<div class="message">Hello World</div>
</div>
</div>
Right now with SCSS, I would create the relevant CSS like this:
.container
{
&.theme-dark
{
.message
{
background-color: black;
color: white;
}
}
&.theme-light
{
.message
{
background-color: white;
color: black;
}
}
.message
{
font-size: 16px;
}
}
I want to be able to generate that CSS using SCSS, with all of the relevant information for the .message element in one place. For instance (using a made-up $ operator that would do what I'm trying to accomplish):
.container
{
.message
{
font-size: 16px;
$.theme-light
{
background-color: white;
color: black;
}
$.theme-dark
{
background-color: black;
color: white;
}
}
}
Any ideas?
I'm thinking this might work, and is like what you're saying? (It would help me currently if you labeled each example as "Ideal CSS output", "Current SCSS, too many .message blocks", and "Ideal SCSS format")
.container
{
#at-root .message
{
font-size: 16px;
.theme-light &
{
background-color: white;
color: black;
}
.theme-dark &
{
background-color: black;
color: white;
}
}
}
With the #at-root there, it will generate .theme-light .message, which might be too permissive for some usages, so not the ideal solution...
https://codepen.io/anon/pen/ZMxjEq
Basically & gets replaced with the full tree-path, so .container .message, which without #at-root, will generate .theme-light .container .message, which does not work with the structure. Perhaps also consider this, which makes a reasonable compromise I would say:
.container
{
.message
{
font-size: 16px;
}
#at-root .message
{
.theme-dark
{
...
}
.theme-light
{
...
}
}
}
It's apparently a kind of hacky solution, but apparently works
This page might have some better guidance as well
This organization can be achieved if you use sass programatically:
$themes: light dark;
.container {
#for $i from 1 through length($themes) {
&.theme-#{nth($themes,$i)} {
.message {
font-size: 16px;
#if nth($themes,$i) == light {
background-color: white;
color: black;
} #else if nth($themes,$i) == dark {
background-color: black;
color: white;
}
}
}
}
}
This generates:
.container.theme-light .message {
font-size: 16px;
background-color: white;
color: black;
}
.container.theme-dark .message {
font-size: 16px;
background-color: black;
color: white;
}
The nested looping automatically groups the details at each level in the same block of code. This also scales to multiple levels of nesting. The critical point is that at inner loops you can reference the selectors of outer loops.
I eventually found this GitHub:
https://github.com/imkremen/sass-parent-append/blob/master/parrent-append.scss
Which I have adapted into this solution:
#function str-to-list($string, $separator: ' ', $startAt: 1)
{
$workStr: str-slice($string, $startAt);
$list: ();
$indexOfFirstSpace: str-index($workStr, $separator);
#if ($indexOfFirstSpace == null)
{
$list: ($workStr);
}
#else
{
$list: (str-slice($workStr, 1, $indexOfFirstSpace - 1));
$list: join($list, str-to-list($workStr, $startAt: $indexOfFirstSpace + 1));
}
#return $list;
}
#function getBase($appendix)
{
$parentSelector: str-to-list(#{&});
$pos: (length($parentSelector) - 1);
$selector: set-nth($parentSelector, $pos, nth($parentSelector, $pos) + $appendix);
#return $selector;
}
#mixin base($appendix)
{
#at-root #{getBase($appendix)}
{
#content;
}
}
Which I can then use like this:
.container
{
.message
{
font-size: 16px;
}
#include base(".theme-light")
{
background-color: white;
color: black;
}
#include base(".theme-dark")
{
background-color: black;
color: white;
}
}
which compiles into this:
.container .message
{
font-size: 16px;
}
.container.theme-light .message
{
background-color: white;
color: black;
}
.container.theme-dark .message
{
background-color: black;
color: white;
}

Less inheritance doesn't work

Hi can you check please following code? I want define some styles for class, and next apply same styles for another class. I've used inheritance for that but styles from parent aren't used:
.parent-item {
&:not(:last-child) {
margin-right: 20px;
}
}
.child-item {
&:extend(.parent-item);
//...
}
just add the word all next to the name of the class
.child-item {
&:extend(.parent-item all);
//...
}
for example
.parent-item {
color: green;
background: red;
&:not(:last-child) {
margin-right: 20px;
}
&:last-child {
color: red;
}
&:hover {
color: red;
}
}
.child-item {
&:extend(.parent-item all);
//...
}
result will be
.parent-item,
.child-item {
color: green;
background: red;
}
.parent-item:not(:last-child),
.child-item:not(:last-child) {
margin-right: 20px;
}
.parent-item:last-child,
.child-item:last-child {
color: red;
}
.parent-item:hover,
.child-item:hover {
color: red;
}

Convert & symbol in scss to less

I have to convert some SCSS files to LESS. For most part it is just case of changing $ with # but there are style that use the scss parent selector & that I don't know how to convert.
Here is example
// Sidebar
.sidebar {
.block {
&.newsletter {
.btn {
&:before {
background: transparent;
}
}
}
&.filter {
ol {
li {
a {
color: #blue;
&:before {
display: none;
}
}
}
}
}
.filter-options-title, .block-title {
color: #444;
padding-bottom: 10px;
font-size: 12px;
&:after {
color: #666;
}
}
}
}
How would I replace out those parent selectors to make it the same generated CSS?
The & parent selector is actually the same syntax in Less and SCSS!
From the Less Documentation on Parent Selectors:
The & operator
represents the parent selectors of a nested rule and is most commonly
used when applying a modifying class or pseudo-class to an existing
selector
In comparison, here's the SASS/ SCSS documentation on parent selectors for pseudo classes: http://sass-lang.com/documentation/Sass/Selector/Pseudo.html
So in the case of your code, it would be:
SCSS
$blue: blue;
.sidebar {
.block {
&.newsletter {
.btn {
&:before {
background: transparent;
}
}
}
&.filter {
ol li a {
color: $blue;
&:before {
display: none;
}
}
}
.filter-options-title, .block-title {
color: #444;
padding-bottom: 10px;
font-size: 12px;
&:after {
color: #666;
}
}
}
}
(try compiling/ validating here: https://www.sassmeister.com/)
LESS
#blue: blue;
.sidebar {
.block {
&.newsletter {
.btn {
&:before {
background: transparent;
}
}
}
&.filter {
ol li a {
color: #blue;
&:before {
display: none;
}
}
}
.filter-options-title, .block-title {
color: #444;
padding-bottom: 10px;
font-size: 12px;
&:after {
color: #666;
}
}
}
}
(try compiling/ validating here: http://winless.org/online-less-compiler)
As well as the official documentation, this article on CSS Tricks is helpful too: https://css-tricks.com/the-sass-ampersand
Hope that helps :)

How can I combine my LESS file with the default bootstrap less file

I am trying to create a different panel from the twitter bootstrap LESS and hence I created a new LESS file and named it panel.less. However, twitter bootstrap already have their default LESS file for panels which is called panels.less.
How can I combine them so that it would process my less file too?(This might not be the correct word)
Panel.less
`
.panel-profile .panel-heading {
position: relative;
}
.panel-profile .panel-heading h4 {
margin: 10px 0 20px;
font-weight: normal;
}
.panel-profile .panel-heading img {
margin: 0 auto 10px;
display: block;
border: 1px solid #ddd;
background: #fff;
}
#media (min-width:400px) {
.panel-profile .panel-heading a {
font-size: .75em;
float: right;
}
.panael-profile .panel-heading {
margin-bottom: 30px;
}
.panel-profile .panel-heading img {
position: absolute;
margin: 0;
display: inline;
bottom: -25px;
}
}
`
Bootstrap.less. - This is my version not the default
`
// Core variables and mixins
#import "variables.less";
#import "mixins.less";
// Reset
#import "normalize.less";
#import "print.less";
// Core CSS
#import "scaffolding.less";
#import "type.less";
#import "grid.less";
#import "forms.less";
#import "buttons.less";
// Components
#import "component-animations.less";
#import "muicons.less";
#import "dropdowns.less";
#import "button-groups.less";
#import "navs.less";
#import "navbar.less";
#import "media.less";
#import "list-group.less";
#import "close.less";
#import "panel.less";
`
Panels.less
`
//
// Panels
// --------------------------------------------------
// Base class
.panel {
margin-bottom: #line-height-computed;
background-color: #panel-bg;
border: 1px solid transparent;
border-radius: #panel-border-radius;
.box-shadow(0 1px 1px rgba(0,0,0,.05));
}
// Panel contents
.panel-body {
padding: #panel-body-padding;
&:extend(.clearfix all);
}
// Optional heading
.panel-heading {
padding: 10px 15px;
border-bottom: 1px solid transparent;
.border-top-radius((#panel-border-radius - 1));
> .dropdown .dropdown-toggle {
color: inherit;
}
}
// Within heading, strip any `h*` tag of its default margins for spacing.
.panel-title {
margin-top: 0;
margin-bottom: 0;
font-size: ceil((#font-size-base * 1.125));
color: inherit;
> a {
color: inherit;
}
}
// Optional footer (stays gray in every modifier class)
.panel-footer {
padding: 10px 15px;
background-color: #panel-footer-bg;
border-top: 1px solid #panel-inner-border;
.border-bottom-radius((#panel-border-radius - 1));
}
// List groups in panels
//
// By default, space out list group content from panel headings to account for
// any kind of custom content between the two.
.panel {
> .list-group {
margin-bottom: 0;
.list-group-item {
border-width: 1px 0;
border-radius: 0;
}
// Add border top radius for first one
&:first-child {
.list-group-item:first-child {
border-top: 0;
.border-top-radius((#panel-border-radius - 1));
}
}
// Add border bottom radius for last one
&:last-child {
.list-group-item:last-child {
border-bottom: 0;
.border-bottom-radius((#panel-border-radius - 1));
}
}
}
}
// Collapse space between when there's no additional content.
.panel-heading + .list-group {
.list-group-item:first-child {
border-top-width: 0;
}
}
// Tables in panels
//
// Place a non-bordered `.table` within a panel (not within a `.panel-body`) and
// watch it go full width.
.panel {
> .table,
> .table-responsive > .table {
margin-bottom: 0;
}
// Add border top radius for first one
> .table:first-child,
> .table-responsive:first-child > .table:first-child {
.border-top-radius((#panel-border-radius - 1));
> thead:first-child,
> tbody:first-child {
> tr:first-child {
td:first-child,
th:first-child {
border-top-left-radius: (#panel-border-radius - 1);
}
td:last-child,
th:last-child {
border-top-right-radius: (#panel-border-radius - 1);
}
}
}
}
// Add border bottom radius for last one
> .table:last-child,
> .table-responsive:last-child > .table:last-child {
.border-bottom-radius((#panel-border-radius - 1));
> tbody:last-child,
> tfoot:last-child {
> tr:last-child {
td:first-child,
th:first-child {
border-bottom-left-radius: (#panel-border-radius - 1);
}
td:last-child,
th:last-child {
border-bottom-right-radius: (#panel-border-radius - 1);
}
}
}
}
> .panel-body + .table,
> .panel-body + .table-responsive {
border-top: 1px solid #table-border-color;
}
> .table > tbody:first-child > tr:first-child th,
> .table > tbody:first-child > tr:first-child td {
border-top: 0;
}
> .table-bordered,
> .table-responsive > .table-bordered {
border: 0;
> thead,
> tbody,
> tfoot {
> tr {
> th:first-child,
> td:first-child {
border-left: 0;
}
> th:last-child,
> td:last-child {
border-right: 0;
}
}
}
> thead,
> tbody {
> tr:first-child {
> td,
> th {
border-bottom: 0;
}
}
}
> tbody,
> tfoot {
> tr:last-child {
> td,
> th {
border-bottom: 0;
}
}
}
}
> .table-responsive {
border: 0;
margin-bottom: 0;
}
}
// Collapsable panels (aka, accordion)
//
// Wrap a series of panels in `.panel-group` to turn them into an accordion with
// the help of our collapse JavaScript plugin.
.panel-group {
margin-bottom: #line-height-computed;
// Tighten up margin so it's only between panels
.panel {
margin-bottom: 0;
border-radius: #panel-border-radius;
overflow: hidden; // crop contents when collapsed
+ .panel {
margin-top: 5px;
}
}
.panel-heading {
border-bottom: 0;
+ .panel-collapse .panel-body {
border-top: 1px solid #panel-inner-border;
}
}
.panel-footer {
border-top: 0;
+ .panel-collapse .panel-body {
border-bottom: 1px solid #panel-inner-border;
}
}
}
// Contextual variations
.panel-default {
.panel-variant(#panel-default-border; #panel-default-text; #panel-default-heading-bg; #panel-default-border);
}
.panel-primary {
.panel-variant(#panel-primary-border; #panel-primary-text; #panel-primary-heading-bg; #panel-primary-border);
}
.panel-success {
.panel-variant(#panel-success-border; #panel-success-text; #panel-success-heading-bg; #panel-success-border);
}
.panel-info {
.panel-variant(#panel-info-border; #panel-info-text; #panel-info-heading-bg; #panel-info-border);
}
.panel-warning {
.panel-variant(#panel-warning-border; #panel-warning-text; #panel-warning-heading-bg; #panel-warning-border);
}
.panel-danger {
.panel-variant(#panel-danger-border; #panel-danger-text; #panel-danger-heading-bg; #panel-danger-border);
}
`
WORKS: I not sure what went wrong but I went to combine my panel LESS file with the default panel LESS file together and managed to get the results. But I am sure that is the wrong way to compile it.
I usually create an app-specific version of the main bootstrap file, let's call it app-bootstrap.less. Just copy the contents of the default bootstrap.less into this file. Here's also your chance to comment out parts of bootstrap that you're not using (e.g. popovers, panels, forms etc). Alternatively you could just do #import "bootstrap.less"; if you're using all of it.
What you'd do next is to add in your own panels.less. The directory search order used is likely determined by your setup, but I guess it would not be an easy task having #import "panels.less"; in app-bootstrap.less mean one thing (i.e. "import panels.less from local directory") and another in panels.less (i.e. "import panels.less from bootstrap). Therefore, what you could do is put your panel additions in app-panels.lessinstead and make sure that file is imported in your app-bootstrap.less.
You can include the bootstrap.less file (#import "path";) in your less file and then compile it - that way you'll overwrite the class in the "CSS way". Or you can include all the files that are included in bootstrap.less, but replace panel.less with your file, and then compile that.

Resources