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 e42df82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 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
6 changes: 3 additions & 3 deletions packages/classname/test/classname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ describe('@bem-react/classname', () => {
expect(b({ modName: false, mod: 'val' })).toEqual('Block-Elem Block-Elem_mod_val')
})

test('zero', () => {
test('zero string', () => {
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 e42df82

Please sign in to comment.