Skip to content

Commit 836ab26

Browse files
authored
fix: add observer for long value component (#221)
1 parent 2f7e358 commit 836ab26

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/lib/kit/components/LongValue/LongValue.tsx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React from 'react';
22

3-
import {Text} from '@gravity-ui/uikit';
3+
import once from 'lodash/once';
4+
5+
import {Text, type TextProps} from '@gravity-ui/uikit';
46

57
import {block} from '../../utils';
68

79
import './LongValue.scss';
810

911
const b = block('long-value');
1012

11-
export interface LongValueProps {
13+
export interface LongValueProps extends TextProps {
1214
value?: string | number | boolean;
13-
className?: string;
1415
}
1516

16-
export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
17-
const prevValue = React.useRef<typeof value | null>(null);
17+
export const LongValue: React.FC<LongValueProps> = ({value, className, ...restProps}) => {
1818
const ref = React.useRef<HTMLDivElement>(null);
1919
const [open, setOpen] = React.useState(false);
2020
const [long, setLong] = React.useState(false);
@@ -23,10 +23,10 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
2323

2424
React.useEffect(() => {
2525
if (ref.current) {
26-
if (value !== prevValue.current) {
27-
const {offsetWidth, scrollWidth} = ref.current;
26+
const {offsetWidth, scrollWidth} = ref.current;
2827

29-
if (offsetWidth < scrollWidth) {
28+
const setFlags = once((offsetW: number, scrollW: number) => {
29+
if (offsetW < scrollW) {
3030
if (!long) {
3131
setLong(true);
3232
}
@@ -39,15 +39,37 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
3939
setOpen(false);
4040
}
4141
}
42+
});
43+
44+
// element has been rendered, but is not displayed on the page
45+
if (offsetWidth === 0 && scrollWidth === 0) {
46+
const observer = new IntersectionObserver((entries) => {
47+
const entry = entries.find((e) => e.target === ref.current);
4248

43-
prevValue.current = value;
49+
if (entry && entry.isIntersecting) {
50+
const target = entry.target as HTMLDivElement;
51+
52+
setFlags(target.offsetWidth, target.scrollWidth);
53+
}
54+
});
55+
56+
observer.observe(ref.current);
57+
58+
return () => {
59+
observer.disconnect();
60+
};
61+
} else {
62+
setFlags(offsetWidth, scrollWidth);
4463
}
4564
}
46-
});
65+
66+
return;
67+
}, [value]);
4768

4869
return (
4970
<Text
5071
as="div"
72+
{...restProps}
5173
ref={ref}
5274
className={b({open, long}, className)}
5375
// @ts-ignore

src/stories/components/InputPreview/InputPreview.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,12 @@ export const InputPreview: React.FC<InputPreviewProps> = ({
175175
search={searchInput}
176176
/>
177177
</div>
178-
{togglerInput === 'view' ? (
179-
<div className={b('input-view')}>
180-
<DynamicView
181-
value={form.values.input}
182-
spec={transformIncorrect(form.values.options)}
183-
/>
184-
</div>
185-
) : null}
178+
<div className={b('input-view', {hidden: togglerInput !== 'view'})}>
179+
<DynamicView
180+
value={form.values.input}
181+
spec={transformIncorrect(form.values.options)}
182+
/>
183+
</div>
186184
{togglerInput === 'json' ? (
187185
<div className={b('monaco')}>{renderMonaco(form.values.input)}</div>
188186
) : null}

0 commit comments

Comments
 (0)