Skip to content

Commit 5a69d1d

Browse files
committed
refactor(getsize): create 'getSize' util
1 parent 25bcc7c commit 5a69d1d

File tree

6 files changed

+28
-41
lines changed

6 files changed

+28
-41
lines changed

src/Avatar/Avatar.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import propTypes from 'prop-types';
33
import styled from 'styled-components';
4+
import { getSize } from '../common/utils';
45

56
const StyledAvatar = styled.div`
67
display: inline-block;
@@ -41,22 +42,13 @@ const SlyledAvatarIMG = styled.img`
4142
`;
4243

4344
const Avatar = React.forwardRef(function Avatar(props, ref) {
44-
const {
45-
alt,
46-
children,
47-
noBorder,
48-
size: sizeProp,
49-
square,
50-
src,
51-
...otherProps
52-
} = props;
45+
const { alt, children, noBorder, size, square, src, ...otherProps } = props;
5346

54-
const size = typeof sizeProp === 'number' ? `${sizeProp}px` : sizeProp;
5547
return (
5648
<StyledAvatar
5749
noBorder={noBorder}
5850
ref={ref}
59-
size={size}
51+
size={getSize(size)}
6052
square={square}
6153
{...otherProps}
6254
>

src/Bar/Bar.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { renderWithTheme } from '../../test/utils';
22

3-
import Bar from './Bar';
3+
import { Bar } from './Bar';
44

55
describe('<Bar />', () => {
66
it('should render bar', () => {

src/Bar/Bar.tsx

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
1-
import React, { forwardRef } from 'react';
1+
import React from 'react';
22
import styled from 'styled-components';
33
import { CommonStyledProps } from '../types';
4+
import { getSize } from '../common/utils';
45

56
type BarProps = {
67
size?: string | number;
78
} & React.HTMLAttributes<HTMLDivElement> &
89
CommonStyledProps;
910

10-
const StyledBar = styled.div<BarProps & { size?: string }>`
11+
// TODO: add horizontal variant
12+
// TODO: allow user to specify number of bars (like 3 horizontal bars for drag handle)
13+
const Bar = styled.div<BarProps>`
14+
${({ theme, size = '100%' }) => `
1115
display: inline-block;
1216
box-sizing: border-box;
13-
height: ${({ size }) => size};
17+
height: ${getSize(size)};
1418
width: 5px;
15-
border-top: 2px solid ${({ theme }) => theme.borderLightest};
16-
border-left: 2px solid ${({ theme }) => theme.borderLightest};
17-
border-bottom: 2px solid ${({ theme }) => theme.borderDark};
18-
border-right: 2px solid ${({ theme }) => theme.borderDark};
19-
background: ${({ theme }) => theme.material};
19+
border-top: 2px solid ${theme.borderLightest};
20+
border-left: 2px solid ${theme.borderLightest};
21+
border-bottom: 2px solid ${theme.borderDark};
22+
border-right: 2px solid ${theme.borderDark};
23+
background: ${theme.material};
24+
`}
2025
`;
2126

22-
// TODO: add horizontal variant
23-
// TODO: allow user to specify number of bars (like 3 horizontal bars for drag handle)
24-
const Bar = forwardRef<HTMLDivElement, BarProps>(function Bar(
25-
{ size: sizeProp = '100%', ...otherProps },
26-
ref
27-
) {
28-
const size = typeof sizeProp === 'number' ? `${sizeProp}px` : sizeProp;
29-
30-
return <StyledBar size={size} ref={ref} {...otherProps} />;
31-
});
32-
33-
export default Bar;
27+
export { Bar, BarProps };

src/Divider/Divider.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import styled from 'styled-components';
2+
import { getSize } from '../common/utils';
23
import { Orientation } from '../types';
34

4-
function getSize(value: string | number) {
5-
return typeof value === 'number' ? `${value}px` : value;
6-
}
7-
interface DividerProps {
5+
type DividerProps = {
86
size?: string | number;
97
orientation?: Orientation;
10-
}
8+
};
119
const Divider = styled.div<DividerProps>`
1210
${({ orientation, theme, size = '100%' }) =>
1311
orientation === 'vertical'

src/Slider/Slider.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
createDisabledTextStyles,
1111
createHatchedBackground
1212
} from '../common';
13-
import { clamp, roundValueToStep } from '../common/utils';
13+
import { clamp, getSize, roundValueToStep } from '../common/utils';
1414
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
1515
import useForkRef from '../common/hooks/useForkRef';
1616
import { useIsFocusVisible } from '../common/hooks/useIsFocusVisible';
@@ -242,7 +242,7 @@ const Slider = React.forwardRef(function Slider(props, ref) {
242242
step,
243243
min,
244244
max,
245-
size: sizeProp,
245+
size,
246246
marks: marksProp,
247247
onChange,
248248
onChangeCommitted,
@@ -475,13 +475,12 @@ const Slider = React.forwardRef(function Slider(props, ref) {
475475
};
476476
}, [handleTouchEnd, handleTouchMove, handleTouchStart]);
477477

478-
const size = typeof sizeProp === 'number' ? `${sizeProp}px` : sizeProp;
479478

480479
return (
481480
<Wrapper
482481
isDisabled={disabled}
483482
vertical={vertical}
484-
size={size}
483+
size={getSize(size)}
485484
onMouseDown={handleMouseDown}
486485
ref={handleRef}
487486
isFocused={focusVisible}

src/common/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ export function roundValueToStep(value: number, step: number, min: number) {
103103
const nearest = Math.round((value - min) / step) * step + min;
104104
return Number(nearest.toFixed(getDecimalPrecision(step)));
105105
}
106+
107+
export function getSize(value: string | number) {
108+
return typeof value === 'number' ? `${value}px` : value;
109+
}

0 commit comments

Comments
 (0)