Skip to content

Commit 7e86825

Browse files
author
Hector Arce De Las Heras
committed
Refactor formatDateToNative function and modify CSS selectors in PillSelectorWrapper
This commit includes a refactoring of the formatDateToNative function to simplify the process of ensuring the year is four digits long, replacing a loop with the padStart method. This makes the code more concise and easier to understand. Additionally, the CSS selectors in the PillSelectorWrapper styled component have been modified to select only direct child elements, ensuring that the styles are applied only to the direct children and preventing any unintended styling of nested elements.
1 parent 41323c4 commit 7e86825

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/components/pillSelector/pillSelector.styled.tsx

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components';
1+
import styled, { css } from 'styled-components';
22

33
import { getStyles } from '@/utils';
44

@@ -7,23 +7,42 @@ import { PillSelectorStyles } from './types/pillSelectorTheme';
77
type PillSelectorStylesProp = {
88
styles: PillSelectorStyles;
99
isSelected?: boolean;
10+
hasThumb?: boolean;
1011
};
1112

1213
export const PillSelectorWrapper = styled.div<PillSelectorStylesProp>`
1314
${({ styles }) => getStyles(styles?.container)};
1415
15-
& :first-child {
16-
${({ styles }) => getStyles(styles?.firstPill)};
17-
${({ isSelected, styles }) => isSelected && getStyles(styles?.firstPill?.selected)}
18-
}
19-
& :last-child {
20-
${({ styles }) => getStyles(styles?.lastPill)};
21-
${({ isSelected, styles }) => isSelected && getStyles(styles?.lastPill?.selected)}
22-
}
23-
& :not(:last-child, :first-child) {
24-
${({ styles }) => getStyles(styles?.pill)};
25-
${({ isSelected, styles }) => isSelected && getStyles(styles?.pill?.selected)}
26-
}
16+
${({ hasThumb, styles, isSelected }) =>
17+
hasThumb
18+
? css`
19+
& > :nth-child(2) {
20+
${getStyles(styles?.firstPill)};
21+
${isSelected && getStyles(styles?.firstPill?.selected)}
22+
}
23+
& > :last-child {
24+
${getStyles(styles?.lastPill)};
25+
${isSelected && getStyles(styles?.lastPill?.selected)}
26+
}
27+
& > :not(:last-child, :nth-child(2)) {
28+
${getStyles(styles?.pill)};
29+
${isSelected && getStyles(styles?.pill?.selected)}
30+
}
31+
`
32+
: css`
33+
& > :first-child {
34+
${getStyles(styles?.firstPill)};
35+
${isSelected && getStyles(styles?.firstPill?.selected)}
36+
}
37+
& > :last-child {
38+
${getStyles(styles?.lastPill)};
39+
${isSelected && getStyles(styles?.lastPill?.selected)}
40+
}
41+
& > :not(:last-child, :first-child) {
42+
${getStyles(styles?.pill)};
43+
${isSelected && getStyles(styles?.pill?.selected)}
44+
}
45+
`};
2746
`;
2847

2948
export const ThumbStyled = styled.div<PillSelectorStylesProp>`

src/utils/syntheticComponents/__tests__/syntheticDate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ describe('syntheticDate test', () => {
2525

2626
const rightFormatDate = formatDateToNative(date, format);
2727

28-
expect(rightFormatDate).toBe('2000-02-11');
28+
expect(rightFormatDate).toBe('0002-02-11');
2929
});
3030
});

src/utils/syntheticComponents/syntheticDate/helper/formatDateToNative.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ export const formatDateToNative = (value: string, format: string): string => {
1717
const dd = cleanDate[ddIdx];
1818
const mm = cleanDate[mmIdx];
1919
let yyyy = cleanDate[yyyyIdx];
20-
2120
if (yyyy.length < 4) {
22-
const diff = Math.abs(yyyy.length - 4);
23-
for (let i = 0; i < diff; i++) {
24-
yyyy = yyyy.concat('', '0');
25-
}
21+
yyyy = yyyy.padStart(4, '0');
2622
}
27-
2823
return `${yyyy}-${mm}-${dd}`;
2924
}
3025
return '';

0 commit comments

Comments
 (0)