Skip to content

Commit

Permalink
fix(flat-table-header): alternative color not changing according theme
Browse files Browse the repository at this point in the history
Closes: FE-5319
  • Loading branch information
AlexKryzh authored and DlgSHi committed Sep 6, 2022
1 parent 7ddd71e commit ce6e48c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const getAlternativeBackgroundColor = (colorTheme) => {
switch (colorTheme) {
case "light":
return "var(--colorsActionMinor100)";

case "transparent-base":
return "var(--colorsUtilityMajor025)";

case "transparent-white":
return "var(--colorsUtilityYang100)";

// default theme is "dark"
default:
return "var(--colorsActionMinor550)";
}
};

export default getAlternativeBackgroundColor;
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useLayoutEffect, useRef } from "react";
import React, { useLayoutEffect, useRef, useContext } from "react";
import PropTypes from "prop-types";
import styledSystemPropTypes from "@styled-system/prop-types";

import StyledFlatTableHeader from "./flat-table-header.style";
import { filterStyledSystemPaddingProps } from "../../../style/utils";
import { FlatTableThemeContext } from "../flat-table.component";

const paddingPropTypes = filterStyledSystemPaddingProps(
styledSystemPropTypes.space
Expand All @@ -23,6 +24,7 @@ const FlatTableHeader = ({
...rest
}) => {
const ref = useRef(null);
const { colorTheme } = useContext(FlatTableThemeContext);

useLayoutEffect(() => {
if (ref.current && reportCellWidth) {
Expand All @@ -36,6 +38,7 @@ const FlatTableHeader = ({
leftPosition={leftPosition || 0}
makeCellSticky={!!reportCellWidth}
align={align}
colorTheme={colorTheme}
data-element="flat-table-header"
colSpan={colspan}
rowSpan={rowspan}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { mount } from "enzyme";
import FlatTableHeader from "./flat-table-header.component";
import StyledFlatTableHeader from "./flat-table-header.style";
import { assertStyleMatch } from "../../../__spec_helper__/test-utils";
import { FlatTableThemeContext } from "../flat-table.component";
import getAlternativeBackgroundColor from "./flat-table-header-utils";

describe("FlatTableHeader", () => {
it("renders with proper width style rule when width prop is passed", () => {
Expand Down Expand Up @@ -49,25 +51,30 @@ describe("FlatTableHeader", () => {
});

describe('with the "alternativeBgColor" prop set', () => {
it('it overrides the header "background-color"', () => {
const wrapper = mount(
<table>
<thead>
<tr>
<FlatTableHeader alternativeBgColor />
</tr>
</thead>
</table>
);
it.each(["dark", "light", "transparent-white", "transparent-base"])(
'it overrides the header "background-color" with correspond color for %s themeColor"',
(colorTheme) => {
const wrapper = mount(
<FlatTableThemeContext.Provider value={{ colorTheme }}>
<table>
<thead>
<tr>
<FlatTableHeader alternativeBgColor>test 1</FlatTableHeader>
</tr>
</thead>
</table>
</FlatTableThemeContext.Provider>
);

assertStyleMatch(
{
backgroundColor: "var(--colorsActionMinor550)",
},
wrapper.find(StyledFlatTableHeader),
{ modifier: "&&&" }
);
});
assertStyleMatch(
{
backgroundColor: getAlternativeBackgroundColor(colorTheme),
},
wrapper.find(StyledFlatTableHeader),
{ modifier: "&&&" }
);
}
);
});

describe.each([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import styled, { css } from "styled-components";
import { padding } from "styled-system";
import getAlternativeBackgroundColor from "./flat-table-header-utils";

const verticalBorderSizes = {
small: "1px",
Expand All @@ -15,6 +16,7 @@ const StyledFlatTableHeader = styled.th`
leftPosition,
makeCellSticky,
verticalBorder,
colorTheme,
}) => css`
background-color: transparent;
border-width: 0;
Expand Down Expand Up @@ -57,11 +59,11 @@ const StyledFlatTableHeader = styled.th`
${
alternativeBgColor &&
css`
&&&:first-child {
border-left: 1px solid var(--colorsActionMinor550);
}
&&& {
background-color: var(--colorsActionMinor550);
background-color: ${getAlternativeBackgroundColor(colorTheme)};
}
&&&:first-child {
border-left: unset;
}
`
};
Expand Down

0 comments on commit ce6e48c

Please sign in to comment.