Skip to content

Commit 38cca2d

Browse files
authored
Allow undefined value for class name prop (#8)
* allowing undefined and null values as valid className prop
1 parent ae0204f commit 38cca2d

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

.changeset/little-panthers-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zemd/react-slottable": patch
3+
---
4+
5+
allowing undefined and null values as a valid values for className prop

src/slottable.test.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { slottable } from "./slottable";
22
import { render } from "@testing-library/react";
3-
import type { TSlottablePropsFactory } from "dist";
3+
import type { TSlottablePropsFactory } from "./types";
44
import { describe, test, expect } from "vitest";
55

66
describe("slottable", () => {
77
test("should render children in specific slots", () => {
88
const Component = slottable<"div", TSlottablePropsFactory<object>>(
99
({ children }) => {
1010
return <div>{children}</div>;
11-
},
11+
}
1212
);
1313
const { getByText } = render(
1414
<Component>
1515
<div data-testid="test-child">Test child</div>
16-
</Component>,
16+
</Component>
1717
);
1818
expect(typeof Component).toBe("object"); // slottable returns Exotic component
1919
expect(getByText("Test child")).toBeInTheDocument();
@@ -24,8 +24,38 @@ describe("slottable", () => {
2424
({ children }) => {
2525
return <div>{children}</div>;
2626
},
27-
"TestComponent",
27+
"TestComponent"
2828
);
2929
expect(Component.displayName).toBe("TestComponent");
3030
});
31+
32+
test("should accept undefined as a valid value for className prop", () => {
33+
const Component = slottable<"div", TSlottablePropsFactory<object>>(
34+
({ children }) => {
35+
return <div>{children}</div>;
36+
}
37+
);
38+
const { getByText } = render(
39+
<Component className={undefined}>
40+
<div data-testid="test-child">Test child 1</div>
41+
</Component>
42+
);
43+
expect(typeof Component).toBe("object"); // slottable returns Exotic component
44+
expect(getByText("Test child 1")).toBeInTheDocument();
45+
});
46+
47+
test("should accept null as a valid value for className prop", () => {
48+
const Component = slottable<"div", TSlottablePropsFactory<object>>(
49+
({ children }) => {
50+
return <div>{children}</div>;
51+
}
52+
);
53+
const { getByText } = render(
54+
<Component className={null}>
55+
<div data-testid="test-child">Test child 2</div>
56+
</Component>
57+
);
58+
expect(typeof Component).toBe("object"); // slottable returns Exotic component
59+
expect(getByText("Test child 2")).toBeInTheDocument();
60+
});
3161
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export type PropsWithComponent<
168168
* Common props for components that usually all components have.
169169
*/
170170
export interface TCommonProps {
171-
className?: string;
171+
className?: string | undefined | null;
172172
style?: CSSProperties;
173173
ref?: Ref<any>;
174174
}

0 commit comments

Comments
 (0)