@bytebrand/fe-ui-core-autobahn 1.0.202 → 1.0.204

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.
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useEffect, useLayoutEffect, useRef } from 'react';
2
2
 
3
3
  import { TranslationFunction } from '../types';
4
4
  import { formatEur, formatEurCents, formatPct } from '../utils/financeFormat';
@@ -12,10 +12,9 @@ import { formatEur, formatEurCents, formatPct } from '../utils/financeFormat';
12
12
  * nominalInterestRate, effective rate = annualPercentageRate.
13
13
  */
14
14
  export interface CreditKeyFiguresData {
15
- /** contract term (paybackPeriod). Shown as the number of monthly rates, like the layer's
16
- * "N Raten à" row: term − 1 when there is a final instalment (Schlussrate), else term. */
15
+ /** contract term (paybackPeriod) — exactly the layer's "Vertragslaufzeit" (e.g. 48, also with a Schlussrate). */
17
16
  months?: number | null;
18
- /** final instalment (Schlussrate) of the same calculation — only used to count the rates. */
17
+ /** final instalment (Schlussrate) of the same calculation — not shown; kept for the hosts' data. */
19
18
  finalInstallment?: number | null;
20
19
  netLoan?: number | null;
21
20
  total?: number | null;
@@ -39,9 +38,9 @@ export function creditKeyFigureLines(t: TranslationFunction, f: CreditKeyFigures
39
38
  const line1: string[] = [];
40
39
  const line2: string[] = [];
41
40
  if (real(f.months)) {
42
- // the layer's "N Raten à" count (FinanceDetailsModal regularRateCount), the row the PM's mock maps "60 Mon." to
43
- const rates = Math.round(f.months) - (real(f.finalInstallment) ? 1 : 0);
44
- if (rates > 0) line1.push(t('autobahn:finance.kfMonths', { months: rates, defaultValue: `${rates} Mon.` }));
41
+ // the layer's "Vertragslaufzeit" (not the "N Raten à" count, which is one less with a Schlussrate)
42
+ const months = Math.round(f.months);
43
+ line1.push(t('autobahn:finance.kfMonths', { months, defaultValue: `${months} Mon.` }));
45
44
  }
46
45
  if (real(f.netLoan)) {
47
46
  const v = formatEur(Math.round(f.netLoan));
@@ -69,13 +68,53 @@ export function hasCreditKeyFigures(f: CreditKeyFiguresData): boolean {
69
68
  return [f.months, f.netLoan, f.total, f.nominal, f.apr].some(real);
70
69
  }
71
70
 
71
+ /** smallest font the auto-fit goes down to (px); the PM wants both lines to stay one line each. */
72
+ const MIN_FIT_PX = 6;
73
+ const useIsoLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
74
+
72
75
  /**
73
- * Two short lines with the financing key figures (Kerndaten). Plain text in the host's
74
- * conditions-line style; the " · " separators reuse the quiet .cc-terms-dot. The optional
75
- * `details` element (the host's existing details link) ends line 2.
76
+ * Exactly two lines with the financing key figures (Kerndaten) — PM 19.11: "from the months to
77
+ * Details it has to fit in 2 lines everywhere". Each line is one unbroken line (nowrap); when the
78
+ * host is too narrow the block's font size steps down (from the host's conditions-line size) until
79
+ * both lines fit, re-measured on resize. The " · " separators reuse the quiet .cc-terms-dot. The
80
+ * optional `details` element (the host's details link/button) ends line 2, always labelled
81
+ * "Details" (card.price.detailsLink), as in the PM's mock.
76
82
  */
77
83
  const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, figures, details, cls }) => {
78
84
  const [line1, line2] = creditKeyFigureLines(t, figures);
85
+ const ref = useRef<HTMLDivElement>(null);
86
+ const detailsLabel = t('autobahn:card.price.detailsLink', { defaultValue: 'Details' });
87
+ const detailsEl = React.isValidElement(details) ? React.cloneElement(details as React.ReactElement<any>, {}, detailsLabel) : details;
88
+ const key = `${line1.join('|')}#${line2.join('|')}#${detailsEl ? detailsLabel : ''}`;
89
+
90
+ useIsoLayoutEffect(() => {
91
+ const el = ref.current;
92
+ if (!el || typeof window === 'undefined') return undefined;
93
+ const fit = (): void => {
94
+ el.style.fontSize = '';
95
+ const lines = Array.prototype.slice.call(el.querySelectorAll('.ckf-line')) as HTMLElement[];
96
+ const tooWide = (): boolean => lines.some((l) => l.scrollWidth > l.clientWidth + 0.5);
97
+ if (!el.clientWidth || !tooWide()) return;
98
+ let size = parseFloat(window.getComputedStyle(el).fontSize) || 12;
99
+ for (let i = 0; i < 40 && size > MIN_FIT_PX && tooWide(); i++) {
100
+ size = Math.max(MIN_FIT_PX, size - 0.25);
101
+ el.style.fontSize = `${size}px`;
102
+ }
103
+ };
104
+ fit();
105
+ const RO = (window as any).ResizeObserver;
106
+ if (!RO) return undefined;
107
+ let width = el.clientWidth;
108
+ const ro = new RO(() => {
109
+ if (el.clientWidth !== width) {
110
+ width = el.clientWidth;
111
+ fit();
112
+ }
113
+ });
114
+ ro.observe(el);
115
+ return () => ro.disconnect();
116
+ }, [key]);
117
+
79
118
  const renderParts = (parts: string[]): React.ReactNode =>
80
119
  parts.map((part, i) => (
81
120
  <React.Fragment key={i}>
@@ -84,15 +123,15 @@ const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, f
84
123
  </React.Fragment>
85
124
  ));
86
125
  return (
87
- <div className={'ckf' + (cls ? ` ${cls}` : '')}>
126
+ <div ref={ref} className={'ckf' + (cls ? ` ${cls}` : '')}>
88
127
  {line1.length > 0 && <span className="ckf-line">{renderParts(line1)}</span>}
89
- {(line2.length > 0 || details) && (
128
+ {(line2.length > 0 || detailsEl) && (
90
129
  <span className="ckf-line">
91
130
  {renderParts(line2)}
92
- {details && (
131
+ {detailsEl && (
93
132
  <>
94
133
  {line2.length > 0 && <span className="cc-terms-dot"> · </span>}
95
- {details}
134
+ <span className="ckf-details">{detailsEl}</span>
96
135
  </>
97
136
  )}
98
137
  </span>