Skip to content

Commit 49caaf0

Browse files
committed
feat: calculate input left pading based on prefix width
1 parent d6dbe34 commit 49caaf0

File tree

10 files changed

+41
-16
lines changed

10 files changed

+41
-16
lines changed

src/@next/CurrencyInput/CurrencyInput.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Interactive.args = {
2424
onChange: (value: number) => {
2525
console.log('Currency on changed value: ', value);
2626
},
27+
prefix: '$',
2728
};

src/@next/CurrencyInput/CurrencyInput.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useState } from 'react';
2-
import { InputProps } from '../Input/Input';
3-
import { StyledCurrency } from './CurrencyStyles';
2+
import { Input, InputProps } from '../Input/Input';
43

54
export type CurrencyInputProps = Omit<
65
InputProps,
@@ -9,13 +8,14 @@ export type CurrencyInputProps = Omit<
98
locale?: string;
109
value?: number;
1110
onChange?: (value: number) => void;
11+
prefix?: string;
1212
};
1313

1414
export const CurrencyInput = React.forwardRef<
1515
HTMLInputElement,
1616
CurrencyInputProps
1717
>(function CurrencyInput(
18-
{ locale = 'en', value = 0, onChange, ...props }: CurrencyInputProps,
18+
{ locale = 'en', value = 0, onChange, prefix, ...props }: CurrencyInputProps,
1919
ref
2020
) {
2121
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -58,10 +58,10 @@ export const CurrencyInput = React.forwardRef<
5858
};
5959

6060
return (
61-
<StyledCurrency
61+
<Input
6262
ref={ref}
6363
type="text"
64-
prefix={<div>$</div>}
64+
prefix={<div>{prefix ?? '$'}</div>}
6565
{...props}
6666
value={formattedValue === '0' ? '' : formattedValue}
6767
onChange={handleChange}

src/@next/CurrencyInput/CurrencyStyles.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/@next/Input/Input.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import {
33
StyledContainer,
44
StyledInput,
@@ -21,22 +21,34 @@ export const Input = React.forwardRef<HTMLInputElement, InputProps>(
2121
const hasPrefix = !!prefix;
2222
const hasSuffix = !!suffix;
2323

24+
const prefixRef = useRef(null);
25+
2426
const Prefix = () =>
2527
hasPrefix ? (
26-
<StyledPrefixContainer>{prefix}</StyledPrefixContainer>
28+
<StyledPrefixContainer ref={prefixRef}>{prefix}</StyledPrefixContainer>
2729
) : null;
2830

2931
const Suffix = () =>
3032
hasSuffix ? (
3133
<StyledSuffixContainer>{suffix}</StyledSuffixContainer>
3234
) : null;
3335

36+
const [prefixWidth, setPrefixWidth] = React.useState(0);
37+
38+
useEffect(() => {
39+
if (hasPrefix) {
40+
const prefixWidth = prefixRef.current.getBoundingClientRect().width;
41+
setPrefixWidth(prefixWidth);
42+
}
43+
}, [hasPrefix, prefix]);
44+
3445
return (
3546
<StyledContainer
3647
ref={ref}
3748
data-prefix={hasPrefix}
3849
data-error={error}
3950
data-disabled={disabled}
51+
prefixWidth={prefixWidth}
4052
>
4153
<Prefix />
4254
<StyledInput disabled={disabled} {...props} />

src/@next/Input/InputStyle.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import styled from 'styled-components';
22
import * as Breakpoints from '../utilities/breakpoints';
33
import { Neutral, Red } from '../utilities/colors';
44
import { InputProps } from './Input';
5+
import { space4, space8 } from '../utilities/spacing';
56

6-
export const StyledContainer = styled.div<InputProps>`
7+
export const StyledContainer = styled.div<InputProps & { prefixWidth: number }>`
78
position: relative;
89
display: flex;
910
flex-direction: column;
@@ -17,7 +18,7 @@ export const StyledContainer = styled.div<InputProps>`
1718
line-height: 150%;
1819
1920
&[data-prefix='true'] input {
20-
padding-left: 40px;
21+
padding-left: ${props => props.prefixWidth}px;
2122
}
2223
2324
&[data-error='true'] input {
@@ -58,18 +59,28 @@ export const StyledPrefixContainer = styled.div`
5859
height: 17px;
5960
width: 17px;
6061
margin: 10px 14px;
62+
margin-right: ${space8};
6163
fill: ${Neutral.B40};
6264
}
6365
6466
div {
6567
color: ${Neutral.B40};
6668
margin: 18px 12px;
69+
margin-right: ${space4};
6770
}
6871
`;
6972

7073
export const StyledSuffixContainer = styled(StyledPrefixContainer)`
7174
left: auto;
7275
right: 0;
76+
77+
svg {
78+
margin: 10px 14px;
79+
}
80+
81+
div {
82+
margin: 18px 12px;
83+
}
7384
`;
7485

7586
export const StyledInput = styled.input<InputProps>`

test/e2e/currencyInput/currency.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ test('Currency - locale', async ({ page }) => {
2323
await currencyPage.goto('args=locale:id');
2424
await expect(currencyPage.container).toHaveScreenshot('currency-id.png');
2525
});
26+
27+
test('Currency - long currency prefix', async ({ page }) => {
28+
const currencyPage = getPage(page);
29+
await currencyPage.goto('args=prefix:CAD');
30+
await expect(currencyPage.container).toHaveScreenshot(
31+
'currency-long-prefix.png'
32+
);
33+
});
216 Bytes
Loading
219 Bytes
Loading
2.51 KB
Loading
1 Byte
Loading

0 commit comments

Comments
 (0)