Skip to content

Commit

Permalink
fix: add spread props to Table and Tooltip component elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehenson committed Apr 8, 2024
1 parent e563fd4 commit f024736
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
47 changes: 38 additions & 9 deletions src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
import React, { PropsWithChildren, ReactElement, cloneElement } from "react";
import React, {
PropsWithChildren,
ReactElement,
TableHTMLAttributes,
cloneElement,
} from "react";

type TableProps = {
id?: string;
};

export const Table = ({ id, children }: PropsWithChildren<TableProps>) => (
<table id={id} className="ui-standard-container mb-4 sm:table-fixed">
export const Table = ({
id,
children,
...rest
}: PropsWithChildren<TableProps & TableHTMLAttributes<HTMLTableElement>>) => (
<table
id={id}
{...rest}
className={`ui-standard-container mb-4 sm:table-fixed ${
rest?.className ?? ""
}`}
>
{children}
</table>
);

export const TableBody = ({ children }: PropsWithChildren) => (
<tbody>{children}</tbody>
export const TableBody = ({
children,
...rest
}: PropsWithChildren<TableHTMLAttributes<HTMLTableSectionElement>>) => (
<tbody {...rest}>{children}</tbody>
);

export const TableHeader = ({ children }: PropsWithChildren) => (
<thead className="sticky bg-white z-10 top-0" key="sticky-block">
export const TableHeader = ({
children,
...rest
}: PropsWithChildren<TableHTMLAttributes<HTMLTableSectionElement>>) => (
<thead
{...rest}
className={`sticky bg-white z-10 top-0 ${rest?.className ?? ""}`}
>
{cloneElement(children as ReactElement, { isHeader: true })}
</thead>
);

export const TableRowHeader = ({ children }: PropsWithChildren) => (
export const TableRowHeader = ({
children,
...rest
}: PropsWithChildren<TableHTMLAttributes<HTMLTableRowElement>>) => (
<tr
className="-ml-24 mt-8 sm:ml-0 sm:mt-0 ui-text-overline1 !text-cool-black bg-light-grey sm:sticky z-10"
className={`-ml-24 mt-8 sm:ml-0 sm:mt-0 bg-light-grey sm:sticky z-10 ${
rest?.className ?? ""
}`}
style={{ top: "4rem" }}
>
{cloneElement(children as ReactElement, { isRowHeader: true })}
Expand Down
16 changes: 12 additions & 4 deletions src/core/Table/TableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const LabelCell = ({
}: PropsWithChildren<React.TdHTMLAttributes<HTMLTableCellElement>>) => {
const classes = `
ui-text-p1 !font-bold pt-24 pb-8 border-light-grey sm:p-24 sm:relative sm:top-2 flex sm:table-cell ${
rest.className ?? ""
rest?.className ?? ""
}
`;

Expand All @@ -56,7 +56,7 @@ const TableCell = ({
? "rounded-l-none rounded-r sm:rounded-lg py-20 px-24"
: "py-6"
}
${rest.className ?? ""}
${rest?.className ?? ""}
`}
>
{children}
Expand All @@ -67,7 +67,12 @@ const HeaderCell = ({
children,
...rest
}: PropsWithChildren<React.TdHTMLAttributes<HTMLTableCellElement>>) => (
<td {...rest} className="ui-text-h3 px-24 py-24 hidden sm:table-cell">
<td
{...rest}
className={`ui-text-h3 px-24 py-24 hidden sm:table-cell ${
rest?.className ?? ""
}`}
>
{children}
</td>
);
Expand All @@ -76,7 +81,10 @@ const CtaCell = ({
children,
...rest
}: PropsWithChildren<React.TdHTMLAttributes<HTMLTableCellElement>>) => (
<td {...rest} className="pt-24 hidden sm:table-cell">
<td
{...rest}
className={`pt-24 hidden sm:table-cell ${rest?.className ?? ""}`}
>
{children}
</td>
);
Expand Down
39 changes: 34 additions & 5 deletions src/core/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import React, { PropsWithChildren, useEffect, useRef, useState } from "react";
import React, {
ButtonHTMLAttributes,
HTMLAttributes,
PropsWithChildren,
useEffect,
useRef,
useState,
} from "react";
import Icon from "./Icon";
const Tooltip = ({ children }: PropsWithChildren) => {

type TooltipProps = {
triggerProps?: ButtonHTMLAttributes<HTMLButtonElement>;
tooltipProps?: HTMLAttributes<HTMLDivElement>;
} & HTMLAttributes<HTMLDivElement>;

const Tooltip = ({
children,
triggerProps,
tooltipProps,
...rest
}: PropsWithChildren<TooltipProps>) => {
const [open, setOpen] = useState(false);
const [position, setPosition] = useState({ x: 0, y: 0 });
const offset = 8;
Expand All @@ -26,28 +44,39 @@ const Tooltip = ({ children }: PropsWithChildren) => {
}, [open]);

return (
<div className="relative inline-block align-top h-16">
<div
{...rest}
className={`relative inline-block align-top h-16 ${
rest?.className ?? ""
}`}
>
<button
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
className="ml-8 p-0 relative top-1 focus:outline-none"
type="button"
ref={reference}
aria-describedby="tooltip"
{...triggerProps}
className={`ml-8 p-0 relative top-1 focus:outline-none ${
triggerProps?.className ?? ""
}`}
>
<Icon name="icon-gui-info" size="1rem" />
</button>

{open ? (
<div
className="bg-light-grey p-12 rounded pointer-events-none absolute z-20"
role="tooltip"
ref={floating}
style={{
top: -position.y,
left: -position.x,
boxShadow: "4px 4px 15px rgba(0, 0, 0, 0.2)",
}}
{...tooltipProps}
className={`bg-light-grey p-12 rounded pointer-events-none absolute z-20 ${
tooltipProps?.className ?? ""
}`}
>
<div className="w-256">{children}</div>
</div>
Expand Down

0 comments on commit f024736

Please sign in to comment.