Skip to content

Commit 1e9568c

Browse files
committed
refactor(utils): split className utils in modules
1 parent 17a2ad0 commit 1e9568c

9 files changed

+100
-21
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getBasicClass } from '@lumx/core/js/utils';
2+
3+
describe(getBasicClass, () => {
4+
it('should return correct basic CSS class for different types and values', () => {
5+
// Test cases with different inputs
6+
expect(getBasicClass({ prefix: 'test', type: 'color', value: 'primary' })).toBe('test--color-primary');
7+
expect(getBasicClass({ prefix: 'test', type: 'variant', value: 'button' })).toBe('test--variant-button');
8+
expect(getBasicClass({ prefix: 'test', type: 'isDark', value: true })).toBe('test--is-dark');
9+
expect(getBasicClass({ prefix: 'test', type: 'dark', value: true })).toBe('test--is-dark');
10+
expect(getBasicClass({ prefix: 'test', type: 'hasDark', value: true })).toBe('test--has-dark');
11+
expect(getBasicClass({ prefix: 'test', type: 'isActive', value: false })).toBe('');
12+
13+
// Additional tests for boolean types
14+
expect(getBasicClass({ prefix: 'test', type: 'hasBorder', value: true })).toBe('test--has-border');
15+
expect(getBasicClass({ prefix: 'test', type: 'isVisible', value: false })).toBe('');
16+
17+
// Tests for undefined
18+
expect(getBasicClass({ prefix: 'test', type: 'variant', value: undefined })).toBe('test--variant-undefined');
19+
});
20+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getFontColorClassName } from '@lumx/react/utils/className/getFontColorClassName';
2+
3+
describe(getFontColorClassName, () => {
4+
it('should get lumx class for font color', () => {
5+
expect(getFontColorClassName('dark')).toBe('lumx-color-font-dark-N');
6+
});
7+
8+
it('should get lumx class for font color with variant', () => {
9+
expect(getFontColorClassName('red', 'L2')).toBe('lumx-color-font-red-L2');
10+
});
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ColorPalette, ColorVariant } from '@lumx/react';
2+
3+
/**
4+
* Returns the classname associated to the given color and variant.
5+
* For example, for 'dark' and 'L2' it returns `lumx-color-font-dark-l2`
6+
*/
7+
export const getFontColorClassName = (color: ColorPalette, colorVariant: ColorVariant = ColorVariant.N) => {
8+
return `lumx-color-font-${color}-${colorVariant}`;
9+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getRootClassName } from '@lumx/react/utils/className/getRootClassName';
2+
3+
describe(getRootClassName, () => {
4+
it('should transform the component name into a lumx class', () => {
5+
expect(getRootClassName('Table')).toBe('lumx-table');
6+
});
7+
8+
it('should transform the sub component name into a lumx class', () => {
9+
expect(getRootClassName('TableBody', true)).toBe('lumx-table__body');
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { CSS_PREFIX } from '@lumx/react/constants';
2-
1+
import { CSS_PREFIX } from '@lumx/core/js/constants';
32
import kebabCase from 'lodash/kebabCase';
4-
import { ColorPalette, ColorVariant, Typography } from '@lumx/react/components';
53

64
// See https://regex101.com/r/YjS1uI/3
75
const LAST_PART_CLASSNAME = /^(.*)-(.+)$/gi;
86

9-
export { getBasicClass, handleBasicClasses } from '@lumx/core/js/utils';
10-
117
/**
128
* Get the name of the root CSS class of a component based on its name.
139
*
@@ -26,19 +22,3 @@ export function getRootClassName(componentName: string, subComponent?: boolean):
2622
}
2723
return formattedClassName;
2824
}
29-
30-
/**
31-
* Returns the classname associated to the given color and variant.
32-
* For example, for 'dark' and 'L2' it returns `lumx-color-font-dark-l2`
33-
*/
34-
export const getFontColorClassName = (color: ColorPalette, colorVariant: ColorVariant = ColorVariant.N) => {
35-
return `lumx-color-font-${color}-${colorVariant}`;
36-
};
37-
38-
/**
39-
* Returns the classname associated to the given typography.
40-
* For example, for `Typography.title` it returns `lumx-typography-title`
41-
*/
42-
export const getTypographyClassName = (typography: Typography) => {
43-
return `lumx-typography-${typography}`;
44-
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getTypographyClassName } from '@lumx/react/utils/className/getTypographyClassName';
2+
3+
describe(getTypographyClassName, () => {
4+
it('should generate lumx typography class', () => {
5+
expect(getTypographyClassName('title')).toBe('lumx-typography-title');
6+
});
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Typography } from '@lumx/react';
2+
3+
/**
4+
* Returns the classname associated to the given typography.
5+
* For example, for `Typography.title` it returns `lumx-typography-title`
6+
*/
7+
export const getTypographyClassName = (typography: Typography) => {
8+
return `lumx-typography-${typography}`;
9+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { handleBasicClasses } from '@lumx/core/js/utils';
2+
3+
describe(handleBasicClasses, () => {
4+
it('should return correct combined CSS classes based on props', () => {
5+
const className = handleBasicClasses({
6+
prefix: 'test',
7+
// Undefined
8+
undefined,
9+
// Null
10+
null: null,
11+
// Empty string
12+
emptyString: '',
13+
// Empty array
14+
emptyArray: [],
15+
// Empty object
16+
emptyObject: {},
17+
// String
18+
color: 'red',
19+
// False property
20+
isDisabled: false,
21+
// Boolean without a prefix (is or has)
22+
visible: true,
23+
// Has prefix
24+
hasButton: true,
25+
});
26+
expect(className).toBe('test test--color-red test--is-visible test--has-button');
27+
});
28+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { getBasicClass, handleBasicClasses } from '@lumx/core/js/utils';
2+
export { getRootClassName } from './getRootClassName';
3+
export { getTypographyClassName } from './getTypographyClassName';
4+
export { getFontColorClassName } from './getFontColorClassName';

0 commit comments

Comments
 (0)