@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
112 lines (106 loc) • 4.46 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import { StyledTableRow } from "./TableRow";
import { StyledTableCell } from "./TableCell";
import { StyledTableBody } from "./TableBody";
import defaultTheme from "../defaultTheme";
const StyledTableOuter = styled.div.withConfig({
displayName: "Table__StyledTableOuter",
componentId: "sc-156ovmd-0"
})(["max-width:100%;width:100%;position:relative;&::after,&::before{content:\" \";display:", ";position:absolute;width:16px;height:100%;top:0;transition:opacity ", " ease-in-out;}&::after{opacity:", ";background-image:", ";right:0;}&::before{opacity:", ";left:0;background-image:", ";}"], ({
showShadows
}) => showShadows ? "block" : "none", ({
theme
}) => theme.orbit.durationNormal, ({
showRight
}) => showRight ? "1" : "0", ({
theme
}) => theme.orbit.backgroundTableShadowRight, ({
showLeft
}) => showLeft ? "1" : "0", ({
theme
}) => theme.orbit.backgroundTableShadowLeft);
StyledTableOuter.defaultProps = {
theme: defaultTheme
};
const StyledTableInner = styled.div.withConfig({
displayName: "Table__StyledTableInner",
componentId: "sc-156ovmd-1"
})(["width:100%;", ";"], ({
showShadows
}) => showShadows && css(["overflow-x:auto;-webkit-overflow-scrolling:touch;"]));
const StyledTable = styled.table.withConfig({
displayName: "Table__StyledTable",
componentId: "sc-156ovmd-2"
})(["display:table;border-collapse:collapse;border-spacing:0;width:100%;white-space:nowrap;& ", " > ", "{background-color:", ";border-bottom:1px solid ", ";transition:background-color ", " ease-in-out;&:nth-of-type(even){background-color:", ";}&:last-child{border:0;}&:hover{background-color:", ";}}& ", "{min-height:", ";padding:", ";}"], StyledTableBody, StyledTableRow, ({
theme
}) => theme.orbit.backgroundTable, ({
theme
}) => theme.orbit.borderColorTable, ({
theme
}) => theme.orbit.durationFast, ({
theme
}) => theme.orbit.backgroundTableEven, ({
theme
}) => theme.orbit.backgroundTableHover, StyledTableCell, ({
compact
}) => compact ? "24px" : "48px", ({
theme,
compact
}) => compact ? theme.orbit.paddingTableCompact : theme.orbit.paddingTable);
StyledTable.defaultProps = {
theme: defaultTheme
};
const Table = ({
children,
compact = false,
dataTest
}) => {
const [shadows, setShadows] = React.useState(false);
const [right, setRight] = React.useState(false);
const [left, setLeft] = React.useState(false);
const outer = React.useRef(null);
const inner = React.useRef(null);
const table = React.useRef(null);
const handleScroll = () => {
if (shadows && inner && table && outer) {
var _inner$current, _inner$current2, _outer$current, _table$current;
setLeft(((_inner$current = inner.current) === null || _inner$current === void 0 ? void 0 : _inner$current.scrollLeft) >= 5);
setRight(((_inner$current2 = inner.current) === null || _inner$current2 === void 0 ? void 0 : _inner$current2.scrollLeft) + ((_outer$current = outer.current) === null || _outer$current === void 0 ? void 0 : _outer$current.clientWidth) <= ((_table$current = table.current) === null || _table$current === void 0 ? void 0 : _table$current.clientWidth));
}
};
const handleResize = React.useCallback(() => {
if (table && outer) {
var _table$current2, _outer$current2;
const showShadows = ((_table$current2 = table.current) === null || _table$current2 === void 0 ? void 0 : _table$current2.clientWidth) > ((_outer$current2 = outer.current) === null || _outer$current2 === void 0 ? void 0 : _outer$current2.clientWidth);
setShadows(showShadows);
setRight(showShadows);
}
}, []);
React.useEffect(() => {
window.addEventListener("resize", handleResize);
handleResize();
return () => {
window.removeEventListener("resize", handleResize);
};
}, [handleResize]);
return React.createElement(StyledTableOuter, {
ref: outer,
showShadows: shadows,
showLeft: left,
showRight: right,
"data-test": dataTest
}, React.createElement(StyledTableInner, {
ref: inner,
onScroll: handleScroll,
showShadows: shadows
}, React.createElement(StyledTable, {
compact: compact,
ref: table
}, children)));
};
export default Table;
export { default as TableHead } from "./TableHead";
export { default as TableBody } from "./TableBody";
export { default as TableRow } from "./TableRow";
export { default as TableCell } from "./TableCell";