Skip to content

Commit d2d03b8

Browse files
author
Kubit
committed
Fix input label bug size
1 parent 0e07d60 commit d2d03b8

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

src/components/inputCurrency/inputCurrencyStandAlone.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as React from 'react';
44
import { ScreenReaderOnly } from '@/components/screenReaderOnly';
55
import { Text, TextComponentType } from '@/components/text';
66
import { useId } from '@/hooks';
7+
import { useElementBoundingClientRect } from '@/hooks/useElementBoundingClientRect/useElementBoundingClientRect';
78
import { POSITIONS } from '@/types/positions';
89
import { pxToRem } from '@/utils';
910

@@ -18,6 +19,11 @@ export const InputCurrencyStandAloneComponent = (
1819
ref: React.ForwardedRef<HTMLInputElement | undefined>
1920
): JSX.Element => {
2021
const [width, setWidth] = React.useState(0);
22+
23+
const { measures } = useElementBoundingClientRect({
24+
ref: ref as React.MutableRefObject<HTMLInputElement>,
25+
});
26+
2127
const measuredRef = React.useCallback(node => {
2228
if (node !== null) {
2329
const widthWithMargin =
@@ -127,7 +133,8 @@ export const InputCurrencyStandAloneComponent = (
127133
width,
128134
props.currencyNameContainerPosition === InputContentPosition.OUT ||
129135
props.styles?.[props.state]?.currencyNameContainerPosition === InputContentPosition.OUT,
130-
props.helpMessagePosition ?? props.styles?.[props.state]?.helpMessage?.position
136+
props.helpMessagePosition ?? props.styles?.[props.state]?.helpMessage?.position,
137+
measures?.width
131138
)
132139
}
133140
truncate={true}

src/components/inputPhone/inputPhoneStandAlone.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { ForwardedRef, forwardRef, useCallback, useState } from 'react';
33
import { ElementOrIcon } from '@/components/elementOrIcon';
44
import { Text } from '@/components/text';
55
import { useId } from '@/hooks';
6+
import { useElementBoundingClientRect } from '@/hooks/useElementBoundingClientRect/useElementBoundingClientRect';
67
import { POSITIONS } from '@/types';
78
import { pxToRem } from '@/utils';
89

@@ -18,6 +19,11 @@ export const InputPhoneStandAloneComponent = (
1819
ref: ForwardedRef<HTMLInputElement | undefined>
1920
): JSX.Element => {
2021
const [width, setWidth] = useState(0);
22+
23+
const { measures } = useElementBoundingClientRect({
24+
ref: ref as React.MutableRefObject<HTMLInputElement>,
25+
});
26+
2127
const measuredRef = useCallback(node => {
2228
if (node !== null) {
2329
const widthWithMargin =
@@ -39,6 +45,7 @@ export const InputPhoneStandAloneComponent = (
3945
const flagVariant =
4046
props.flag?.variant ?? props.styles?.[props.state]?.affixIconHighlighted?.variant;
4147
const flagSize = props.flag?.size ?? props.styles?.[props.state]?.affixIconHighlighted?.size;
48+
4249
const renderAffix = () => (
4350
<AffixStyled ref={measuredRef} id={extraAriaLabelledBy} styles={props.styles?.[props.state]}>
4451
{props.prefixNode ? (
@@ -123,7 +130,8 @@ export const InputPhoneStandAloneComponent = (
123130
POSITIONS.LEFT,
124131
width,
125132
props.styles?.[props.state]?.affixContainerPosition === InputContentPosition.OUT,
126-
props.styles?.[props.state]?.helpMessage?.position
133+
props.styles?.[props.state]?.helpMessage?.position,
134+
measures?.width
127135
)
128136
}
129137
variant={props.inputVariant ?? props.styles?.[props.state]?.inputVariant}

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export { useScrollEffect } from './useScrollEffect/useScrollEffect';
1717
export * from './useScrollPosition/useScrollPosition';
1818
export { useZoomEffect } from './useZoomEffect/useZoomEffect';
1919
export { useDeviceHeight } from './useDeviceHeight/useDeviceHeight';
20+
export { useElementBoundingClientRect } from './useElementBoundingClientRect/useElementBoundingClientRect';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { act, renderHook } from '@testing-library/react-hooks';
2+
3+
import { useElementBoundingClientRect } from '../useElementBoundingClientRect';
4+
5+
describe('useElementBoundingClientRect Hook', () => {
6+
it('should return the correct bounding client rect', () => {
7+
const ref = { current: document.createElement('div') };
8+
const { result } = renderHook(() => useElementBoundingClientRect({ ref }));
9+
10+
act(() => {
11+
// Simulate a resize event
12+
window.dispatchEvent(new Event('resize'));
13+
});
14+
15+
expect(result.current.measures).toEqual(ref.current.getBoundingClientRect());
16+
});
17+
18+
it('should return undefined if the element reference is null', () => {
19+
const ref = { current: null };
20+
const { result } = renderHook(() => useElementBoundingClientRect({ ref }));
21+
22+
expect(result.current.measures).toBeUndefined();
23+
});
24+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as React from 'react';
2+
3+
interface Params {
4+
ref?: React.MutableRefObject<HTMLElement | null>;
5+
}
6+
7+
interface ReturnValue {
8+
measures?: DOMRect;
9+
}
10+
11+
/**
12+
* Custom hook that returns the bounding client rect of a specified element.
13+
*
14+
* @param {Params} props - The hook parameters.
15+
* @param {React.MutableRefObject<HTMLElement | null>} props.ref - The reference to the element.
16+
*
17+
* @returns {ReturnValue} - The hook return value.
18+
* @returns {DOMRect | undefined} ReturnValue.measures - The bounding client rect of the element.
19+
*/
20+
export const useElementBoundingClientRect = (props: Params): ReturnValue => {
21+
const [measures, setMeasures] = React.useState<DOMRect | undefined>(undefined);
22+
23+
React.useEffect(() => {
24+
const element = props.ref?.current;
25+
if (!element) {
26+
return () => null;
27+
}
28+
29+
const getMeasures = () => {
30+
if (element) {
31+
const measure = element?.getBoundingClientRect();
32+
setMeasures(measure);
33+
}
34+
};
35+
36+
getMeasures();
37+
38+
window.addEventListener('resize', getMeasures);
39+
return () => window.removeEventListener('resize', getMeasures);
40+
}, [props.ref]);
41+
42+
return { measures };
43+
};

src/styles/mixins/input.mixin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const mapLabelTypeStyles = (props: LabelStyledProps): CSSProp => {
7373
`
7474
: css`
7575
display: inline-block;
76-
width: 100%;
76+
/* width: 100%; */
7777
`;
7878

7979
const isFilledLabel = ({ styles, leftExtraStyles }: LabelStyledProps): CSSProp =>
@@ -256,17 +256,17 @@ export const helpMessageMargin = (marginLeft: number): CSSProp => css`
256256
`;
257257

258258
// label is not STANDARD (absolute position) and input has inner left content
259-
export const labelInTopStyles = (marginLeft: number): CSSProp => css`
259+
export const labelInTopStyles = (marginLeft: number, affixWidth: number): CSSProp => css`
260260
> div {
261261
margin-left: ${marginLeft}rem;
262-
width: calc(100% - ${marginLeft}rem);
262+
width: ${affixWidth}px;
263263
}
264264
/* We need to write the CSS code inside this CSS extension, which will only be applied to the web page when opening the Firefox browser. */
265265
/* Firefox need a explicit position when you use position:absolute; */
266266
@-moz-document url-prefix() {
267267
label {
268268
left: calc(${marginLeft}rem);
269-
width: calc(100% - ${marginLeft}rem);
269+
width: ${affixWidth}px;
270270
}
271271
}
272272
`;

0 commit comments

Comments
 (0)