UNPKG

@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.

97 lines 4.33 kB
import * as React from "react"; import styled, { css } from "styled-components"; import { StyledTableRow } from "./TableRow"; import { StyledTableCell } from "./TableCell"; import { StyledTableBody } from "./TableBody"; import { StyledTableHead } from "./TableHead"; import { TYPE_OPTIONS } from "./consts"; import defaultTheme from "../defaultTheme"; const StyledTableOuter = styled.div.withConfig({ displayName: "Table__StyledTableOuter", componentId: "sc-1bgqd4h-0" })(["", ""], ({ showShadows, showLeft, showRight, theme }) => css(["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 ? "block" : "none", theme.orbit.durationNormal, showRight ? "1" : "0", theme.orbit.backgroundTableShadowRight, showLeft ? "1" : "0", theme.orbit.backgroundTableShadowLeft)); StyledTableOuter.defaultProps = { theme: defaultTheme }; const StyledTableInner = styled.div.withConfig({ displayName: "Table__StyledTableInner", componentId: "sc-1bgqd4h-1" })(["", ""], ({ showShadows }) => css(["width:100%;", ";"], showShadows && css(["overflow-x:auto;-webkit-overflow-scrolling:touch;"]))); const StyledTable = styled.table.withConfig({ displayName: "Table__StyledTable", componentId: "sc-1bgqd4h-2" })(["", ";"], ({ theme, type, striped, compact }) => css(["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;", " &:last-child{border:0;}&:hover{background-color:", ";}}& ", "{height:", ";padding:", ";line-height:", ";color:", ";}"], StyledTableBody, StyledTableRow, theme.orbit.backgroundTable, theme.orbit.borderColorTable, theme.orbit.durationFast, striped && css(["&:nth-of-type(even){background-color:", ";}"], theme.orbit.backgroundTableEven), theme.orbit.backgroundTableHover, StyledTableCell, compact ? theme.orbit.spaceXLarge : theme.orbit.spaceXXLarge, compact ? `6px ${theme.orbit.spaceSmall}` : `10px ${theme.orbit.spaceSmall}`, theme.orbit.lineHeightTextNormal, type === TYPE_OPTIONS.SECONDARY && theme.orbit.paletteInkNormal)); StyledTable.defaultProps = { theme: defaultTheme }; const Table = ({ children, striped = true, compact = false, dataTest, id, type = TYPE_OPTIONS.PRIMARY }) => { 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.current && table.current && outer.current) { setLeft(inner.current?.scrollLeft >= 5); setRight(inner.current.scrollLeft + outer.current.clientWidth < table.current.clientWidth); } }; const handleResize = React.useCallback(() => { if (table.current && outer.current) { const showShadows = table.current.clientWidth > outer.current.clientWidth; setShadows(showShadows); setRight(showShadows); } }, []); React.useEffect(() => { window.addEventListener("resize", handleResize); handleResize(); return () => { window.removeEventListener("resize", handleResize); }; }, [handleResize]); return /*#__PURE__*/React.createElement(StyledTableOuter, { ref: outer, showShadows: shadows, showLeft: left, showRight: right, "data-test": dataTest, id: id }, /*#__PURE__*/React.createElement(StyledTableInner, { ref: inner, onScroll: handleScroll, showShadows: shadows }, /*#__PURE__*/React.createElement(StyledTable, { striped: striped, compact: compact, type: type, ref: table }, children))); }; export default Table; export { default as TableHead } from "./TableHead"; export { default as TableBody } from "./TableBody"; export { default as TableFooter } from "./TableFooter"; export { default as TableRow } from "./TableRow"; export { default as TableCell } from "./TableCell"; export { StyledTable, StyledTableCell, StyledTableBody, StyledTableRow, StyledTableHead };