-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
5,381 additions
and
14,044 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "@testing-library/jest-dom"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { HTMLProps } from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { classified } from "./classified"; | ||
|
||
type Props = { variant?: "primary"; block?: boolean }; | ||
|
||
test("should create a component", () => { | ||
let Button = classified("button")([]); | ||
render(<Button>hello</Button>); | ||
expect(screen.getByRole("button", { name: /hello/i })).toBeInTheDocument(); | ||
}); | ||
|
||
test("should apply string classNames", () => { | ||
let Button = classified("button")(["btn"]); | ||
render(<Button>hello</Button>); | ||
expect(screen.getByRole("button", { name: /hello/i }).className).toBe("btn"); | ||
}); | ||
|
||
test("should apply dynamic classNames", () => { | ||
let Button = classified("button")([() => "btn"]); | ||
render(<Button>hello</Button>); | ||
expect(screen.getByRole("button", { name: /hello/i }).className).toBe("btn"); | ||
}); | ||
|
||
test("should pass props to dynamic className creator", () => { | ||
let Button = classified("button")([({ variant }: Props) => `btn-${variant}`]); | ||
render(<Button variant="primary">hello</Button>); | ||
expect(screen.getByRole("button", { name: /hello/i }).className).toBe( | ||
"btn-primary" | ||
); | ||
}); | ||
|
||
test("should combine both static and dynamic classNames", () => { | ||
let Button = classified("button")([ | ||
"btn", | ||
({ variant }: Props) => `btn-${variant}`, | ||
({ block }: Props) => block && "btn-block", | ||
]); | ||
render( | ||
<Button variant="primary" block> | ||
hello | ||
</Button> | ||
); | ||
expect(screen.getByRole("button", { name: /hello/i }).className).toBe( | ||
"btn btn-primary btn-block" | ||
); | ||
}); | ||
|
||
test("should accept any react component as the first argument", () => { | ||
function Link(props: HTMLProps<HTMLAnchorElement>) { | ||
return <a {...props} />; | ||
} | ||
|
||
let ClassifiedLink = classified(Link)(["link"]); | ||
render(<ClassifiedLink href="/read-more">Read more</ClassifiedLink>); | ||
expect(screen.getByRole("link", { name: /read more/i }).className).toBe( | ||
"link" | ||
); | ||
}); | ||
|
||
test("component should accept any additional `className`", () => { | ||
let Button = classified("button")(["btn"]); | ||
render(<Button className="special">hello</Button>); | ||
expect(screen.getByRole("button", { name: /hello/i }).className).toBe( | ||
"btn special" | ||
); | ||
}); | ||
|
||
test("component should accept `as` prop", () => { | ||
let Button = classified("button")(["btn"]); | ||
render( | ||
<Button as="a" href="/"> | ||
hello | ||
</Button> | ||
); | ||
expect(screen.getByRole("link", { name: /hello/i }).className).toBe("btn"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { ComponentProps, ElementType, HTMLProps } from "react"; | ||
import { createElement, forwardRef } from "react"; | ||
import isPropValid from "@emotion/is-prop-valid"; | ||
|
||
type Falsy = false | 0 | "" | null | undefined; | ||
|
||
function classified(type) { | ||
return function createClassifiedComponent(names) { | ||
function ClassifiedComponent({ as = type, children, ...props }, ref) { | ||
let className = names | ||
.concat(props.className) | ||
.map(name => (isFunc(name) ? name(props) : name)) | ||
.filter(Boolean) | ||
.join(" "); | ||
|
||
let elementProps = isString(as) ? getHtmlProps(props) : props; | ||
|
||
return createElement( | ||
as, | ||
Object.assign(elementProps, { className, ref }), | ||
children | ||
); | ||
} | ||
|
||
return forwardRef(ClassifiedComponent); | ||
}; | ||
} | ||
|
||
function cx(names: Array<string | Falsy> = []) { | ||
return names.filter(Boolean).join(" "); | ||
} | ||
|
||
function isFunc(x: unknown): x is Function { | ||
return typeof x === "function"; | ||
} | ||
|
||
function isString(x: unknown): x is string { | ||
return typeof x === "string"; | ||
} | ||
|
||
function getHtmlProps(props: Record<string, any>) { | ||
return Object.keys(props).reduce((htmlProps, prop) => { | ||
if (isPropValid(prop)) { | ||
htmlProps[prop] = props[prop]; | ||
} | ||
return htmlProps; | ||
}, {} as Partial<typeof props>); | ||
} | ||
|
||
export { classified, cx }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["vitest/globals"], | ||
"target": "es2016", | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"noUncheckedIndexedAccess": true, | ||
"noEmit": true, | ||
"jsx": "preserve" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference types="vitest" /> | ||
/// <reference types="vite/client" /> | ||
|
||
import react from "@vitejs/plugin-react"; | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
plugins: [react()], | ||
test: { | ||
globals: true, | ||
environment: "jsdom", | ||
setupFiles: "./setup-test.ts", | ||
}, | ||
}); |