Skip to content

Commit

Permalink
fix(classname): fix zero mod value bug
Browse files Browse the repository at this point in the history
  • Loading branch information
frontstall committed Mar 22, 2024
1 parent 6d78a65 commit 1009fef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/classname/classname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export type Preset = {
v?: string
}

function isEmpty(val: string | boolean | number | undefined) {
return !val && val !== 0
}

/**
* BEM className configure function.
*
Expand Down Expand Up @@ -81,7 +85,7 @@ export function withNaming(preset: Preset): ClassNameInitilizer {

if (modVal === true) {
className += modPrefix + k
} else if (modVal) {
} else if (!isEmpty(modVal)) {
className += modPrefix + k + modValueDelimiter + modVal
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/classname/test/classname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('@bem-react/classname', () => {

test('zero', () => {
const b = cn('Block')
expect(b({ modName: '0' })).toEqual('Block Block_modName_0')
expect(b({ modName: '0', mod: 0 })).toEqual('Block Block_modName_0 Block_mod_0')
})

test('undefined', () => {
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('@bem-react/classname', () => {

test('zero', () => {
const b = cCn('block')
expect(b({ modName: '0' })).toEqual('block block_modName_0')
expect(b({ modName: '0', mod: 0 })).toEqual('block block_modName_0 block_mod_0')
})
})
})
Expand Down

0 comments on commit 1009fef

Please sign in to comment.