Skip to content

Commit 547f6e9

Browse files
committed
Added gap modifier to the view component
1 parent 332875d commit 547f6e9

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

src/commons/__tests__/modifiers.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import {ThemeManager, Colors, Typography, BorderRadiuses} from '../../style';
2+
import {ThemeManager, Colors, Typography, BorderRadiuses, Spacings} from '../../style';
33
import * as uut from '../modifiers';
44

55
describe('Modifiers', () => {
@@ -329,6 +329,22 @@ describe('Modifiers', () => {
329329
left: true,
330330
'bg-red10': false
331331
});
332+
expect(uut.extractModifierProps({
333+
'paddingL-20': true,
334+
'gap-10': true,
335+
other: 'some-value'
336+
})).toEqual({
337+
'paddingL-20': true,
338+
'gap-10': true
339+
});
340+
expect(uut.extractModifierProps({
341+
'paddingL-20': true,
342+
'gap-s3': true,
343+
other: 'some-value'
344+
})).toEqual({
345+
'paddingL-20': true,
346+
'gap-s3': true
347+
});
332348
});
333349
});
334350

@@ -415,4 +431,23 @@ describe('Modifiers', () => {
415431
expect(modifiers.borderRadius).toBeUndefined();
416432
});
417433
});
434+
435+
describe('extractGapValues', () => {
436+
it('Should return gap 10', () => {
437+
const modifiers = uut.extractGapValues({'gap-10': true});
438+
expect(modifiers).toEqual({gap: 10});
439+
});
440+
it('Should return gap 20', () => {
441+
const modifiers = uut.extractGapValues({'gap-20': true});
442+
expect(modifiers).toEqual({gap: 20});
443+
});
444+
it('Should return gap spacing s3', () => {
445+
const modifiers = uut.extractGapValues({'gap-s3': true});
446+
expect(modifiers).toEqual({gap: Spacings.s3});
447+
});
448+
it('Should return gap spacing s10', () => {
449+
const modifiers = uut.extractGapValues({'gap-s10': true});
450+
expect(modifiers).toEqual({gap: Spacings.s10});
451+
});
452+
});
418453
});

src/commons/modifiers.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const MARGIN_KEY_PATTERN = new RegExp(`margin[LTRBHV]?-([0-9]*|${Spacings
1313
export const ALIGNMENT_KEY_PATTERN = /(left|top|right|bottom|center|centerV|centerH|spread)/;
1414
export const POSITION_KEY_PATTERN = /^abs([F|L|R|T|B|V|H])?$/;
1515
const BACKGROUND_COLOR_KEYS_PATTERN = Colors.getBackgroundKeysPattern();
16+
export const GAP_KEY_PATTERN = new RegExp(`gap-([0-9]*|${Spacings.getKeysPattern()})`);
1617

1718
export interface AlteredOptions {
1819
flex?: boolean;
@@ -33,6 +34,7 @@ export interface ExtractedStyle {
3334
alignments?: ReturnType<typeof extractAlignmentsValues>;
3435
flexStyle?: ReturnType<typeof extractFlexStyle>;
3536
positionStyle?: ReturnType<typeof extractPositionStyle>;
37+
gap?: ReturnType<typeof extractGapValues>;
3638
}
3739

3840
export type ModifiersOptions = {
@@ -45,6 +47,7 @@ export type ModifiersOptions = {
4547
alignments?: boolean;
4648
flex?: boolean;
4749
position?: boolean;
50+
gap?: boolean;
4851
};
4952

5053
const PADDING_VARIATIONS = {
@@ -74,11 +77,11 @@ const STYLE_KEY_CONVERTERS = {
7477
} as const;
7578

7679
export type PaddingLiterals = keyof typeof PADDING_VARIATIONS;
77-
export type NativePaddingKeyType = typeof PADDING_VARIATIONS[PaddingLiterals];
80+
export type NativePaddingKeyType = (typeof PADDING_VARIATIONS)[PaddingLiterals];
7881
export type MarginLiterals = keyof typeof MARGIN_VARIATIONS;
79-
export type NativeMarginModifierKeyType = typeof MARGIN_VARIATIONS[MarginLiterals];
82+
export type NativeMarginModifierKeyType = (typeof MARGIN_VARIATIONS)[MarginLiterals];
8083
export type FlexLiterals = keyof typeof STYLE_KEY_CONVERTERS;
81-
export type NativeFlexModifierKeyType = typeof STYLE_KEY_CONVERTERS[FlexLiterals];
84+
export type NativeFlexModifierKeyType = (typeof STYLE_KEY_CONVERTERS)[FlexLiterals];
8285
export type ColorLiterals = keyof (typeof colorsPalette & typeof DesignTokens);
8386
export type TypographyLiterals = keyof typeof TypographyPresets;
8487
export type BorderRadiusLiterals = keyof typeof BorderRadiusesLiterals;
@@ -93,6 +96,7 @@ export type AlignmentLiterals =
9396
| 'top'
9497
| 'bottom';
9598
export type PositionLiterals = 'absF' | 'absL' | 'absR' | 'absT' | 'absB' | 'absV' | 'absH';
99+
export type GapLiterals = 'gap';
96100

97101
export type Modifier<T extends string> = Partial<Record<T, boolean>>;
98102
export type CustomModifier = {[key: string]: boolean};
@@ -109,14 +113,16 @@ export type MarginModifiers = Modifier<MarginLiterals>;
109113
// export type MarginModifiers = Partial<{[key: `${MarginLiterals}-${number}`]: boolean}>;
110114
export type FlexModifiers = Modifier<FlexLiterals>;
111115
export type BorderRadiusModifiers = Modifier<BorderRadiusLiterals>;
116+
export type GapModifiers = Modifier<GapLiterals>;
112117

113118
export type ContainerModifiers = AlignmentModifiers &
114119
PositionModifiers &
115120
PaddingModifiers &
116121
MarginModifiers &
117122
FlexModifiers &
118123
BorderRadiusModifiers &
119-
BackgroundColorModifier;
124+
BackgroundColorModifier &
125+
GapModifiers;
120126

121127
export function extractColorValue(props: Dictionary<any>) {
122128
const colorPropsKeys = Object.keys(props).filter(key => Colors[key] !== undefined);
@@ -187,6 +193,23 @@ export function extractMarginValues(props: Dictionary<any>) {
187193
return margins;
188194
}
189195

196+
export function extractGapValues(props: Dictionary<any>) {
197+
const gap: {gap?: number} = {};
198+
const gapPropsKeys = Object.keys(props).filter(key => GAP_KEY_PATTERN.test(key));
199+
// Taking only the last one
200+
const gapModifier = _.findLast(gapPropsKeys, key => props[key] === true);
201+
if (gapModifier) {
202+
const [, gapValue] = gapModifier.split('-') as ['gap', 'string'];
203+
const parsedNumber = Number(gapValue);
204+
if (!isNaN(parsedNumber)) {
205+
gap.gap = parsedNumber;
206+
} else if (Spacings.getKeysPattern().test(gapValue)) {
207+
gap.gap = Spacings[gapValue as keyof typeof SpacingLiterals];
208+
}
209+
}
210+
return gap;
211+
}
212+
190213
export function extractAlignmentsValues(props: Dictionary<any>) {
191214
const {row, center} = props;
192215
const alignments: any = {};
@@ -306,6 +329,7 @@ export function extractModifierProps(props: Dictionary<any>) {
306329
PADDING_KEY_PATTERN,
307330
MARGIN_KEY_PATTERN,
308331
ALIGNMENT_KEY_PATTERN,
332+
GAP_KEY_PATTERN,
309333
Colors.getBackgroundKeysPattern()
310334
];
311335
const modifierProps = _.pickBy(props, (_value, key) => {
@@ -338,7 +362,9 @@ export function extractComponentProps(component: any, props: Dictionary<any>, ig
338362
}
339363

340364
//@ts-ignore
341-
export function getThemeProps<T extends object>(props: T = this.props, context = this.context, componentDisplayName = ''):T {
365+
export function getThemeProps<T extends object>(props: T = this.props,
366+
context = this.context,
367+
componentDisplayName = ''): T {
342368
const componentName =
343369
//@ts-ignore
344370
componentDisplayName || this.displayName || this.constructor.displayName || this.constructor.name;
@@ -368,7 +394,8 @@ export function generateModifiersStyle(options: ModifiersOptions = {
368394
margins: true,
369395
alignments: true,
370396
flex: true,
371-
position: true
397+
position: true,
398+
gap: true
372399
},
373400
props: Dictionary<any>) {
374401
//@ts-ignore
@@ -406,6 +433,9 @@ props: Dictionary<any>) {
406433
if (options.position) {
407434
style.positionStyle = extractPositionStyle(boundProps);
408435
}
436+
if (options.gap) {
437+
style.gap = extractGapValues(boundProps);
438+
}
409439

410440
return style;
411441
// clean empty objects and undefined

src/components/view/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ const modifiersOptions = {
5353
margins: true,
5454
alignments: true,
5555
flex: true,
56-
position: true
56+
position: true,
57+
gap: true
5758
};
5859

5960
/**
@@ -91,7 +92,8 @@ function View(props: ViewProps, ref: any) {
9192
margins,
9293
alignments,
9394
flexStyle,
94-
positionStyle
95+
positionStyle,
96+
gap
9597
} = useModifiers(themeProps, modifiersOptions);
9698
const [ready, setReady] = useState(!renderDelay);
9799

@@ -130,6 +132,7 @@ function View(props: ViewProps, ref: any) {
130132
paddings,
131133
margins,
132134
alignments,
135+
gap,
133136
style
134137
];
135138
}, [
@@ -141,6 +144,7 @@ function View(props: ViewProps, ref: any) {
141144
paddings,
142145
margins,
143146
alignments,
147+
gap,
144148
style
145149
]);
146150

0 commit comments

Comments
 (0)