-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: chart legend support px rem em % unit type
- Loading branch information
1 parent
0e31d25
commit 4e023e6
Showing
8 changed files
with
313 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/react-components/src/hooks/utils/parseValueAndUnit.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { parseValueAndUnit } from './parseValueAndUnit'; | ||
|
||
describe('parseValueAndUnit', () => { | ||
it('should parse an input with only a number', () => { | ||
expect(parseValueAndUnit('100')).toEqual({ value: 100, unit: 'px' }); | ||
}); | ||
|
||
it('should parse an input with a number followed by a unit', () => { | ||
expect(parseValueAndUnit('75%')).toEqual({ value: 75, unit: '%' }); | ||
}); | ||
|
||
it('should parse an input with a number followed by a unit', () => { | ||
expect(parseValueAndUnit('50em')).toEqual({ value: 50, unit: 'em' }); | ||
}); | ||
|
||
it('should return 0 for value and px for unit if input is just a string', () => { | ||
expect(parseValueAndUnit('')).toEqual({ value: 0, unit: 'px' }); | ||
}); | ||
|
||
it('should handle input with spaces and mixed units', () => { | ||
expect(parseValueAndUnit(' 200 px ')).toEqual({ value: 200, unit: 'px' }); | ||
}); | ||
|
||
it('should default to 0 for value if number is not parseable', () => { | ||
expect(parseValueAndUnit('px')).toEqual({ value: 0, unit: 'px' }); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/react-components/src/hooks/utils/parseValueAndUnit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const parseValueAndUnit = ( | ||
input: string | ||
): { value: number; unit: string } => { | ||
const value = parseInt(input); | ||
const unit = input.replace(/[0-9]/g, '').trim(); | ||
return { value: value || 0, unit: unit || 'px' }; | ||
}; |
Oops, something went wrong.