Skip to content

Commit 51b36b0

Browse files
author
Andrii Kirmas
committed
Tolerate empty classNames
1 parent 824f97d commit 51b36b0

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

src/ctx.spec.tsx

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,49 @@ import { classNamesCheck, classNamingCtx } from "."
33
import expectToRender from "../expect-to-render"
44
import { ClassNames } from "./defs"
55

6-
describe(classNamingCtx.name, () => {
7-
it("demo", () => {
8-
function Component(props: ClassNames<true, "class1"|"class2">) {
9-
const classes = classNamingCtx(props)
6+
function Component(props: ClassNames<true, "class1"|"class2">) {
7+
const classes = classNamingCtx(props)
8+
return <>
9+
<div {...classes(true, {class1: true, class2: false})}/>
10+
<div {...classes("class2")}/>
11+
</>
12+
}
1013

11-
return <>
12-
<div {...classes(true, {class1: true, class2: false})}/>
13-
<div {...classes("class2")}/>
14-
</>
15-
}
14+
function App({classNames, className}: ClassNames<true, "App__Item", typeof Component>) {
15+
return <Component {...{
16+
...classNamingCtx({
17+
classNames, className
18+
})(
19+
true, "App__Item"
20+
),
21+
classNames
22+
}}/>
23+
}
1624

17-
function App({classNames, className}: ClassNames<true, "App__Item", typeof Component>) {
18-
const classes = classNamingCtx({classNames, className})
25+
describe(classNamingCtx.name, () => {
26+
it("demo", () => expectToRender(
27+
<App className="MyApp" classNames={classNamesCheck()} />,
28+
[
29+
'<div class="MyApp App__Item class1"></div>',
30+
'<div class="class2"></div>'
31+
]
32+
))
1933

20-
return <>
21-
<Component {...classes("App__Item")} {...{classNames}}/>
22-
</>
23-
}
34+
it("forget className", () => expectToRender(
35+
//@ts-expect-error Property 'className' is missing
36+
<App classNames={classNamesCheck()}/>,
37+
[
38+
'<div class="App__Item class1"></div>',
39+
'<div class="class2"></div>'
40+
]
41+
))
42+
it("forget classNames", () => expectToRender(
43+
//@ts-expect-error Property 'classNames' is missing
44+
<App className={"MyApp"}/>,
45+
[
46+
'<div class="MyApp App__Item class1"></div>',
47+
'<div class="class2"></div>'
48+
]
49+
))
2450

25-
expectToRender(
26-
<App className="MyApp" classNames={classNamesCheck()} />,
27-
[
28-
'<div class="App__Item class1"></div>',
29-
'<div class="class2"></div>'
30-
]
31-
)
32-
})
3351
})

src/ctx.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ function classNamingCtx<ClassKeys extends string/*, withClassNames extends boole
3434
{classNames, className}: ClassNamer<ClassKeys>,
3535
options?: ClassNamerOptions//<withClassNames>
3636
): tClassNaming<ClassKeys> {
37-
emptize(classNames)
38-
3937
return classNamer.bind({classNames, className, ...options}) as tClassNaming<ClassKeys>
4038
}
4139

@@ -49,7 +47,7 @@ type ClassNamerOptions<
4947
}>
5048

5149
function classNamer<ClassKeys extends string>(
52-
this: ClassNamer<ClassKeys> & ClassNamerOptions,
50+
this: Partial<ClassNamer<ClassKeys> & ClassNamerOptions>,
5351
arg0: true | ToggleMap<ClassKeys> | ClassKeys,
5452
arg1?: ToggleMap<ClassKeys> | ClassKeys,
5553
...args: (ClassKeys | Falsy)[]
@@ -82,7 +80,7 @@ function classNamer<ClassKeys extends string>(
8280

8381
for (let i = allowed.length; i--;) {
8482
const key = allowed[i]
85-
, hash: ClassValue = classNames[key]
83+
, hash: ClassValue = classNames?.[key]
8684

8785
if (hash !== undefined)
8886
//@ts-expect-error

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ function classNamedToString(this: {className: string}) {
3939
return this.className
4040
}
4141

42-
function emptize(source: Record<string, any>) {
43-
if (!source.hasOwnProperty(stringifyProperty))
42+
function emptize(source: undefined|Record<string, any>) {
43+
if (
44+
source
45+
&& !source.hasOwnProperty(stringifyProperty)
46+
)
4447
$defineProperty(source, stringifyProperty, {value: emptyLambda})
4548
return source
4649
}

0 commit comments

Comments
 (0)