Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flex Fix #52

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/flex/__tests__/flexCol.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
setColumnSizing,
setColumnOffset,
setGutterWidth,
parseNumberToTwoDecimals,
FlexCol
} from '../src/FlexCol';

Expand Down Expand Up @@ -40,13 +41,13 @@ describe('FlexCol', () => {
describe('setColumnSizing', () => {
it('should return a string percentage value when provided with a number value as arg', () => {
const baseColumn = 8.3;
const expected = '33.2%';
const expected = '33.20%';
expect(setColumnSizing(baseColumn, 4)).toEqual(expected);
});

it('should return an array of string percentage values when provided with an array of number column sizings', () => {
const baseColumn = 8.3;
const expected = ['99.60000000000001%', '49.800000000000004%'];
const expected = ['99.60%', '49.80%'];
expect(setColumnSizing(baseColumn, [12, 6])).toEqual(expected);
});
});
Expand Down Expand Up @@ -77,6 +78,12 @@ describe('FlexCol', () => {
});
});

describe('parseNumberToTwoDecimals', () => {
it('should return a string number with only 2 decimal places', () => {
expect(parseNumberToTwoDecimals(5 * 1.3326)).toEqual('6.66');
});
});

describe('FlexCol', () => {
it('should render', () => {
const { asFragment } = render(<FlexCol columnSize={1}>Test</FlexCol>);
Expand Down
10 changes: 8 additions & 2 deletions packages/flex/src/FlexCol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export function setColumnSizing(
columnSize: number | number[]
): string | string[] {
return Array.isArray(columnSize) && columnSize.length > 1
? columnSize.map(size => `${size * baseColumnWidth}%`)
: `${(columnSize as number) * baseColumnWidth}%`;
? columnSize.map(
size => `${parseNumberToTwoDecimals(size * baseColumnWidth)}%`
)
: `${parseNumberToTwoDecimals((columnSize as number) * baseColumnWidth)}%`;
}

export function setColumnOffset(
Expand All @@ -61,6 +63,10 @@ export function setGutterWidth(
: `${(gutterWidth as number) / 2}em`;
}

export function parseNumberToTwoDecimals(value: number) {
return `${parseFloat(String(value)).toFixed(2)}`;
}

export function isNumber(val: number): boolean {
return !isNaN(val) && isFinite(val);
}