Skip to content

Commit e42df82

Browse files
committed
fix(classname): fix zero mod value bug
1 parent 6d78a65 commit e42df82

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

packages/classname/classname.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export type Preset = {
4949
v?: string
5050
}
5151

52+
function isEmpty(val: string | boolean | number | undefined) {
53+
return !val && val !== 0
54+
}
55+
5256
/**
5357
* BEM className configure function.
5458
*
@@ -81,7 +85,7 @@ export function withNaming(preset: Preset): ClassNameInitilizer {
8185

8286
if (modVal === true) {
8387
className += modPrefix + k
84-
} else if (modVal) {
88+
} else if (!isEmpty(modVal)) {
8589
className += modPrefix + k + modValueDelimiter + modVal
8690
}
8791
}

packages/classname/test/classname.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ describe('@bem-react/classname', () => {
4747
expect(b({ modName: false, mod: 'val' })).toEqual('Block-Elem Block-Elem_mod_val')
4848
})
4949

50-
test('zero', () => {
50+
test('zero string', () => {
5151
const b = cn('Block')
52-
expect(b({ modName: '0' })).toEqual('Block Block_modName_0')
52+
expect(b({ modName: '0', mod: 0 })).toEqual('Block Block_modName_0 Block_mod_0')
5353
})
5454

5555
test('undefined', () => {
@@ -211,7 +211,7 @@ describe('@bem-react/classname', () => {
211211

212212
test('zero', () => {
213213
const b = cCn('block')
214-
expect(b({ modName: '0' })).toEqual('block block_modName_0')
214+
expect(b({ modName: '0', mod: 0 })).toEqual('block block_modName_0 block_mod_0')
215215
})
216216
})
217217
})

0 commit comments

Comments
 (0)