Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
fix: Dynamic HTML Tag Names for Level/Container (#147)
Browse files Browse the repository at this point in the history
* HTML tag type

* working fix for tag prop type

* node name fix

* level component dynamic HTML

* better docs on useDidMount
  • Loading branch information
Tiernebre committed Jul 25, 2021
1 parent e691617 commit 920c449
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/components/container/Container.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ it("renders in non-fluid mode by default", () => {
const container = screen.getByText(message);
expect(container).not.toHaveClass("is-fluid");
});

it("can be rendered as a different HTML element", () => {
const message = "This is a test container. Cool!";
render(<Container as="section">{message}</Container>);
const container = screen.getByText(message);
expect(container.nodeName).toEqual("SECTION");
});
5 changes: 4 additions & 1 deletion src/components/container/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { PropsWithChildren } from "react";
import { HTMLTag } from "../../types";
import {
ClassNameTransformMap,
createClassNameFromProps,
} from "../../utilities";

export type ContainerProps = PropsWithChildren<{
as?: HTMLTag;
fluid?: boolean;
className?: string;
}>;
Expand All @@ -13,6 +15,7 @@ const containerClassNameMapping: ClassNameTransformMap<ContainerProps> =
new Map([["fluid", () => "is-fluid"]]);

export const Container = ({
as: Comp = "div",
children,
className = "",
...classNameProps
Expand All @@ -23,5 +26,5 @@ export const Container = ({
["container", className]
);

return <div className={mappedClassName}>{children}</div>;
return <Comp className={mappedClassName}>{children}</Comp>;
};
5 changes: 5 additions & 0 deletions src/components/level/Level.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ it("can render content on the right", () => {
render(<Level right={right} />);
expect(screen.getByText(right)).toHaveClass("level-right");
});

it("can be rendered as a different HTML element besides a div", () => {
render(<Level as="header">Level</Level>);
expect(screen.getByText("Level").nodeName).toEqual("HEADER");
});
7 changes: 5 additions & 2 deletions src/components/level/Level.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { PropsWithChildren, ReactNode } from "react";
import { HTMLTag } from "../../types";
import {
ClassNameTransformMap,
createClassNameFromProps,
} from "../../utilities";

export type LevelProps = PropsWithChildren<{
as?: HTMLTag;
left?: ReactNode;
right?: ReactNode;
mobile?: boolean;
Expand All @@ -15,6 +17,7 @@ const classNameMap: ClassNameTransformMap<LevelProps> = new Map([
]);

export const Level = ({
as: Comp = "div",
left,
right,
children,
Expand All @@ -27,10 +30,10 @@ export const Level = ({
);

return (
<div className={className}>
<Comp className={className}>
{left && <div className="level-left">{left}</div>}
{children}
{right && <div className="level-right">{right}</div>}
</div>
</Comp>
);
};
8 changes: 6 additions & 2 deletions src/hooks/lifecycle/use-did-mount.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// specific ESLint disable. This hook is built
// to get past this specific check that should be used
// in other React code.
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect } from "react";

import { EffectCallback, useEffect } from "react";

/**
* useDidMount hook
* Calls a function on mount
*
* @param {Function} callback Callback function to be called on mount
*/
export const useDidMount = (callback: () => void): void => {
export const useDidMount = (callback: EffectCallback): void => {
useEffect(callback, []);
};
1 change: 1 addition & 0 deletions src/types/html/HTMLTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type HTMLTag = keyof JSX.IntrinsicElements;
1 change: 1 addition & 0 deletions src/types/html/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./HTMLTag";
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./alert";
export * from "./color";
export * from "./direction";
export * from "./form";
export * from "./html";
export * from "./position";
export * from "./size";
export * from "./typography";
Expand Down

0 comments on commit 920c449

Please sign in to comment.