Skip to content

Commit e1d4594

Browse files
author
Hector Arce De Las Heras
committed
Improve input styles with new mixins
1 parent 00720bf commit e1d4594

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {
2+
getPaddingLeft,
3+
getPaddingLeftFromContainer,
4+
getPaddingRight,
5+
getPaddingRightFromContainer,
6+
} from '../input.mixin.utils';
7+
8+
describe('getPaddingLeft', () => {
9+
it('should return undefined when padding is not provided', () => {
10+
expect(getPaddingLeft()).toBeUndefined();
11+
});
12+
13+
it('should return the same value when padding is provided for all sides', () => {
14+
expect(getPaddingLeft('10px')).toBe('10px');
15+
});
16+
17+
it('should return the horizontal padding when vertical and horizontal padding are provided', () => {
18+
expect(getPaddingLeft('10px 20px')).toBe('20px');
19+
});
20+
21+
it('should return the horizontal padding when top, horizontal, and bottom padding are provided', () => {
22+
expect(getPaddingLeft('10px 20px 30px')).toBe('20px');
23+
});
24+
25+
it('should return the left padding when padding is provided for all four sides', () => {
26+
expect(getPaddingLeft('10px 20px 30px 40px')).toBe('40px');
27+
});
28+
29+
it('should return undefined when padding format is not recognized', () => {
30+
expect(getPaddingLeft('10px 20px 30px 40px 50px')).toBeUndefined();
31+
});
32+
});
33+
34+
describe('getPaddingRight', () => {
35+
it('should return undefined when padding is not provided', () => {
36+
expect(getPaddingRight()).toBeUndefined();
37+
});
38+
39+
it('should return the same value when padding is provided for all sides', () => {
40+
expect(getPaddingRight('10px')).toBe('10px');
41+
});
42+
43+
it('should return the horizontal padding when vertical and horizontal padding are provided', () => {
44+
expect(getPaddingRight('10px 20px')).toBe('20px');
45+
});
46+
47+
it('should return the horizontal padding when top, horizontal, and bottom padding are provided', () => {
48+
expect(getPaddingRight('10px 20px 30px')).toBe('20px');
49+
});
50+
51+
it('should return the right padding when padding is provided for all four sides', () => {
52+
expect(getPaddingRight('10px 20px 30px 40px')).toBe('20px');
53+
});
54+
55+
it('should return undefined when padding format is not recognized', () => {
56+
expect(getPaddingRight('10px 20px 30px 40px 50px')).toBeUndefined();
57+
});
58+
});
59+
60+
describe('getPaddingLeftFromContainer', () => {
61+
it('should return padding_left when it is defined', () => {
62+
const container = { padding_left: '10px' };
63+
expect(getPaddingLeftFromContainer(container)).toBe('10px');
64+
});
65+
66+
it('should return padding from getPaddingLeft when padding_left is not defined', () => {
67+
const container = { padding: '10px 20px 30px 40px' };
68+
expect(getPaddingLeftFromContainer(container)).toBe('40px');
69+
});
70+
71+
it('should return "0rem" when neither padding_left nor padding is defined', () => {
72+
const container = {};
73+
expect(getPaddingLeftFromContainer(container)).toBe('0rem');
74+
});
75+
});
76+
77+
describe('getPaddingRightFromContainer', () => {
78+
it('should return padding_right when it is defined', () => {
79+
const container = { padding_right: '10px' };
80+
expect(getPaddingRightFromContainer(container)).toBe('10px');
81+
});
82+
83+
it('should return padding from getPaddingRight when padding_right is not defined', () => {
84+
const container = { padding: '10px 20px 30px 40px' };
85+
expect(getPaddingRightFromContainer(container)).toBe('20px');
86+
});
87+
88+
it('should return "0rem" when neither padding_right nor padding is defined', () => {
89+
const container = {};
90+
expect(getPaddingRightFromContainer(container)).toBe('0rem');
91+
});
92+
});

src/styles/mixins/input.mixin.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { InputStyledProps, LabelStyledProps } from '@/components/input/types/inp
1010
import { getTypographyStyles } from '@/utils';
1111
import { pxToRem } from '@/utils/pxToRem/pxToRem';
1212

13+
import { getPaddingLeftFromContainer, getPaddingRightFromContainer } from './input.mixin.utils';
14+
1315
const isStandardLabel = (labelType: string | undefined): boolean => {
1416
return labelType === LABEL_TYPE.STANDARD;
1517
};
@@ -175,16 +177,17 @@ export const getIconPaddingDeprecated = (
175177
position?: InputIconPosition,
176178
styles?: InputBasicStateProps
177179
): CSSProp => {
178-
const padding = styles?.inputContainer?.padding?.split(' ')[1] || null;
179180
const iconWidth = styles?.inputIcon?.width;
180181

181182
if (position === InputIconPosition.RIGHT) {
183+
const paddingRight = getPaddingRightFromContainer(styles?.inputContainer);
182184
return css`
183-
padding-right: calc(${padding} + ${iconWidth});
185+
padding-right: calc(${paddingRight} + ${iconWidth});
184186
`;
185187
}
188+
const paddingLeft = getPaddingLeftFromContainer(styles?.inputContainer);
186189
return css`
187-
padding-left: calc(${padding} + ${iconWidth});
190+
padding-left: calc(${paddingLeft} + ${iconWidth});
188191
`;
189192
};
190193

@@ -193,21 +196,24 @@ export const getIconPadding = (
193196
rightIcon?: boolean,
194197
styles?: InputBasicStateProps
195198
): CSSProp | undefined => {
196-
const padding = styles?.inputContainer?.padding?.split(' ')[1] || styles?.inputContainer?.padding;
197199
const iconWidth = styles?.inputIcon?.width;
198200

199201
if (leftIcon && rightIcon) {
202+
const paddingLeft = getPaddingLeftFromContainer(styles?.inputContainer);
203+
const paddingRight = getPaddingRightFromContainer(styles?.inputContainer);
200204
return css`
201-
padding-left: calc(${padding} + ${iconWidth});
202-
padding-right: calc(${padding} + ${iconWidth});
205+
padding-left: calc(${paddingLeft} + ${iconWidth});
206+
padding-right: calc(${paddingRight} + ${iconWidth});
203207
`;
204208
} else if (leftIcon) {
209+
const paddingLeft = getPaddingLeftFromContainer(styles?.inputContainer);
205210
return css`
206-
padding-left: calc(${padding} + ${iconWidth});
211+
padding-left: calc(${paddingLeft} + ${iconWidth});
207212
`;
208213
} else if (rightIcon) {
214+
const paddingRight = getPaddingRightFromContainer(styles?.inputContainer);
209215
return css`
210-
padding-right: calc(${padding} + ${iconWidth});
216+
padding-right: calc(${paddingRight} + ${iconWidth});
211217
`;
212218
}
213219
return undefined;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { CommonStyleType } from '@/types';
2+
3+
export const getPaddingLeft = (padding?: string): string | undefined => {
4+
if (!padding) {
5+
return undefined;
6+
}
7+
const values = padding.split(' ');
8+
switch (values.length) {
9+
case 1: // All four sides are the same
10+
return values[0];
11+
case 2: // Vertical | Horizontal
12+
return values[1];
13+
case 3: // Top | Horizontal | Bottom
14+
return values[1];
15+
case 4: // Top | Right | Bottom | Left
16+
return values[3];
17+
default:
18+
return undefined;
19+
}
20+
};
21+
22+
export const getPaddingRight = (padding?: string): string | undefined => {
23+
if (!padding) {
24+
return undefined;
25+
}
26+
const values = padding.split(' ');
27+
switch (values.length) {
28+
case 1: // All four sides are the same
29+
return values[0];
30+
case 2: // Vertical | Horizontal
31+
return values[1];
32+
case 3: // Top | Horizontal | Bottom
33+
return values[1];
34+
case 4: // Top | Right | Bottom | Left
35+
return values[1];
36+
default:
37+
return undefined;
38+
}
39+
};
40+
41+
export const getPaddingLeftFromContainer = (container?: CommonStyleType): string | undefined => {
42+
return container?.padding_left ?? getPaddingLeft(container?.padding) ?? '0rem';
43+
};
44+
45+
export const getPaddingRightFromContainer = (container?: CommonStyleType): string | undefined => {
46+
return container?.padding_right ?? getPaddingRight(container?.padding) ?? '0rem';
47+
};

0 commit comments

Comments
 (0)