@bytebrand/fe-ui-core 4.2.120 → 4.2.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytebrand/fe-ui-core",
3
- "version": "4.2.120",
3
+ "version": "4.2.121",
4
4
  "description": "UI components for the auto.de project",
5
5
  "main": "index.ts",
6
6
  "module": "dist/common.js",
@@ -1,4 +1,4 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React from 'react';
2
2
  import styles from './FormattedNumber.styl';
3
3
 
4
4
  import classnames from 'classnames';
@@ -13,38 +13,11 @@ export interface IFormattedPriceProps {
13
13
  className?: string;
14
14
  afterCommaClassName?: string;
15
15
  beforeCommaClassName?: string;
16
- fontSize?:number;
17
16
  }
18
17
 
19
18
  // const breakpointForRound = 10000;
20
19
  const separatorCounter = 3;
21
- const FormattedNumber: React.FunctionComponent<IFormattedPriceProps> = ({ className, value, disableFormatting, toRound, numbersAfterDot, beforeCommaClassName, afterCommaClassName, fontSize }) => {
22
- const textRef = useRef(null);
23
- const uniqueId = _uniqueId(className);
24
-
25
- const [newFontSize, setFontSize] = useState(fontSize);
26
- useEffect(() => {
27
- const priceElement = document.getElementById(uniqueId);
28
- const resizeFont = () => {
29
- const textWidth = priceElement.getBoundingClientRect().width;
30
- let parentWidth = fontSize === 40 ? 150 : 100;
31
- if (window.innerWidth < 720) {
32
- parentWidth = fontSize === 40 ? 200 : 150;
33
- }
34
- if (fontSize && textWidth > 120) {
35
- textRef.current.parentNode.style.maxWidth = `${parentWidth}px`;
36
- const newFont = Math.floor(newFontSize * parentWidth / textWidth);
37
- priceElement.style.fontSize = `${newFont}px`;
38
- priceElement.style.verticalAlign = 'top';
39
- textRef.current.parentNode.style.fontSize = `${newFont}px`;
40
- setFontSize(newFont);
41
- }
42
- };
43
- resizeFont();
44
- window.addEventListener('resize', resizeFont);
45
- return () => window.removeEventListener('resize', resizeFont);
46
- }, [uniqueId, newFontSize]);
47
-
20
+ const FormattedNumber: React.FunctionComponent<IFormattedPriceProps> = ({ className, value, disableFormatting, toRound, numbersAfterDot, beforeCommaClassName, afterCommaClassName }) => {
48
21
  function getFormattedNumber() {
49
22
  // First we need to check if value is actual number;
50
23
 
@@ -99,7 +72,7 @@ const FormattedNumber: React.FunctionComponent<IFormattedPriceProps> = ({ classN
99
72
 
100
73
  return (
101
74
  <span className={classnames(styles.formattedNumber, className)}>
102
- <span className={styles.value} ref={textRef} id={uniqueId}>{val}</span>
75
+ <span className={styles.value}>{val}</span>
103
76
  </span>
104
77
  );
105
78
  };
@@ -1,4 +1,4 @@
1
- import * as React from 'react';
1
+ import React, { useEffect, useRef, useCallback } from 'react';
2
2
  import classnames from 'classnames';
3
3
  import FormattedNumber from '../../FormattedNumber/FormattedNumber';
4
4
  import styles from './VehicleFormattedPrice.styl';
@@ -23,7 +23,7 @@ export interface IVehiclePriceProps {
23
23
  size?: 'small' | 'semimedium' | 'medium' | 'large';
24
24
  disableStyles?: boolean;
25
25
  disablePrice?: boolean;
26
- fontSize?:number;
26
+ isDynamicFontSize?:boolean;
27
27
 
28
28
  // FormattedNumber props
29
29
  toRound?: boolean;
@@ -35,6 +35,20 @@ export interface IVehiclePriceProps {
35
35
  }
36
36
 
37
37
  const VehicleFormattedPrice: React.FunctionComponent<IVehiclePriceProps> = (props) => {
38
+ const textRef = useRef(null);
39
+
40
+ const updateFontSize = useCallback(() => {
41
+ if (price && isDynamicFontSize && textRef.current) {
42
+ const priceLength = Math.round(price).toString().length;
43
+ const newSize = Math.min(60, 200 / priceLength);
44
+ textRef.current.style.fontSize = `${newSize}px`;
45
+ }
46
+ }, [props.price, props.isDynamicFontSize]);
47
+
48
+ useEffect(() => {
49
+ updateFontSize();
50
+ }, [updateFontSize]);
51
+
38
52
  function renderUnit(): React.ReactNode {
39
53
  const { postfix, unit, unitClassName } = props;
40
54
  return (
@@ -79,7 +93,7 @@ const VehicleFormattedPrice: React.FunctionComponent<IVehiclePriceProps> = (prop
79
93
  // FormattedNumber props
80
94
  const {
81
95
  unit, sub, numbersAfterDot, toRound, numberContainerClassName,
82
- afterCommaClassName, beforeCommaClassName, disableFormatting, disablePrice,fontSize
96
+ afterCommaClassName, beforeCommaClassName, disableFormatting, disablePrice, isDynamicFontSize
83
97
  } = props;
84
98
 
85
99
  const decimalsClass = classnames(afterCommaClassName, styles.decimals);
@@ -95,7 +109,7 @@ const VehicleFormattedPrice: React.FunctionComponent<IVehiclePriceProps> = (prop
95
109
  disableFormatting,
96
110
  numbersAfterDot,
97
111
  toRound,
98
- fontSize,
112
+ isDynamicFontSize,
99
113
  beforeCommaClassName,
100
114
  value: price,
101
115
  className: numberContainerClassName,
@@ -103,7 +117,7 @@ const VehicleFormattedPrice: React.FunctionComponent<IVehiclePriceProps> = (prop
103
117
  };
104
118
 
105
119
  return (
106
- <span className={containerClass}>
120
+ <span className={containerClass} ref={textRef}>
107
121
  {<FormattedNumber {...formattedNumberProps} />}
108
122
  {(unit && !monthly) && renderUnit()}
109
123
  {(monthly && unit) && renderUnitMonthly()}
@@ -127,6 +141,7 @@ VehicleFormattedPrice.defaultProps = {
127
141
  unitClassName: '',
128
142
  postfixClassName: '',
129
143
  subClassName: '',
144
+ isDynamicFontSize: false,
130
145
  sub: '',
131
146
  // FormattedNumber props
132
147
  toRound: false,
@@ -91,7 +91,7 @@ const PriceContent: React.FunctionComponent<IPriceContentProps> = ({
91
91
  postfix={postfix}
92
92
  numbersAfterDot={0}
93
93
  size='large'
94
- fontSize={tabContentPaddingClassName.includes('tabContentPaddingForBuy')?40:60}
94
+ isDynamicFontSize={tabContentPaddingClassName.includes('tabContentPaddingForBuy')? false : true}
95
95
  />
96
96
  </div>
97
97
  ) : null}