Skip to content

Commit c762939

Browse files
authored
Fix #826 | Support merging multiple typography modifiers on a component (#831)
1 parent 94a2bb8 commit c762939

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

src/commons/__tests__/baseComponent.spec.js

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,10 @@
11
import baseComponent from '../baseComponent';
2-
import {Colors, Typography} from '../../style';
2+
import {Colors} from '../../style';
33

44
const BaseComponent = baseComponent(false);
55

66
describe('BaseComponent', () => {
7-
describe('extractTypographyValue', () => {
8-
it('should extract typography value according to typography modifier', () => {
9-
expect(new BaseComponent({text40: true}).extractTypographyValue()).toEqual(Typography.text40);
10-
expect(new BaseComponent({text70: true}).extractTypographyValue()).toEqual(Typography.text70);
11-
});
12-
13-
it('should return undefined if not typography modifier was sent', () => {
14-
expect(new BaseComponent({}).extractTypographyValue()).toEqual(undefined);
15-
});
16-
17-
it('should return take the last typography modifier prop in case there is more than one', () => {
18-
expect(new BaseComponent({
19-
text40: true,
20-
text70: true
21-
}).extractTypographyValue(),).toEqual(Typography.text70);
22-
expect(new BaseComponent({
23-
text70: true,
24-
text40: true
25-
}).extractTypographyValue(),).toEqual(Typography.text40);
26-
expect(new BaseComponent({
27-
text40: true,
28-
text70: false
29-
}).extractTypographyValue(),).toEqual(Typography.text40);
30-
});
31-
32-
it('should return value of the custom made typography', () => {
33-
const customTypography = {fontSize: Typography.text30.fontSize, fontWeight: '400'};
34-
Typography.loadTypographies({customTypography});
35-
expect(new BaseComponent({customTypography: true}).extractTypographyValue()).toEqual(customTypography);
36-
expect(new BaseComponent({
37-
text40: true,
38-
customTypography: true
39-
}).extractTypographyValue(),).toEqual(customTypography);
40-
expect(new BaseComponent({
41-
customTypography: true,
42-
text40: true
43-
}).extractTypographyValue(),).toEqual(Typography.text40);
44-
});
45-
});
46-
7+
478
describe('updateModifiers', () => {
489
it('should update state with new modifiers values if modifiers props have changed', () => {
4910
const uut = new BaseComponent({});

src/commons/__tests__/modifiers.spec.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ describe('Modifiers', () => {
4141
expect(uut.extractTypographyValue({})).toEqual(undefined);
4242
});
4343

44-
it('should return take the last typography modifier prop in case there is more than one', () => {
44+
it('should ignore modifiers with false value', () => {
45+
expect(uut.extractTypographyValue({
46+
text40: true,
47+
text70: false
48+
}),).toEqual(Typography.text40);
49+
});
50+
51+
it('should prioritize last typography modifier prop in case there is more than one', () => {
4552
expect(uut.extractTypographyValue({
4653
text40: true,
4754
text70: true
@@ -50,10 +57,7 @@ describe('Modifiers', () => {
5057
text70: true,
5158
text40: true
5259
}),).toEqual(Typography.text40);
53-
expect(uut.extractTypographyValue({
54-
text40: true,
55-
text70: false
56-
}),).toEqual(Typography.text40);
60+
5761
});
5862

5963
it('should return value of the custom made typography', () => {
@@ -63,11 +67,17 @@ describe('Modifiers', () => {
6367
expect(uut.extractTypographyValue({
6468
text40: true,
6569
customTypography: true
66-
}),).toEqual(customTypography);
70+
}),).toEqual({...Typography.text40, ...customTypography});
6771
expect(uut.extractTypographyValue({
6872
customTypography: true,
6973
text40: true
70-
}),).toEqual(Typography.text40);
74+
}),).toEqual({...customTypography, ...Typography.text40});
75+
});
76+
77+
it('should merge typography modifiers', () => {
78+
const bold = {fontWeight: 'bold'};
79+
Typography.loadTypographies({bold});
80+
expect(uut.extractTypographyValue({text70: true, bold: true})).toEqual({...Typography.text70, fontWeight: 'bold'});
7181
});
7282
});
7383

src/commons/modifiers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ export function extractTypographyValue(props: Dictionary<any>): object | undefin
125125
.keys()
126126
.filter(key => Typography.getKeysPattern().test(key))
127127
.value() as unknown as Array<keyof typeof TypographyPresets>;
128-
let typography;
128+
let typography: any;
129129
_.forEach(typographyPropsKeys, key => {
130130
if (props[key] === true) {
131-
typography = Typography[key];
131+
typography = {...typography, ...Typography[key]};
132132
}
133133
});
134134

0 commit comments

Comments
 (0)