diff --git a/src/lib/kit/components/LongValue/LongValue.tsx b/src/lib/kit/components/LongValue/LongValue.tsx index 5053dfef..76692880 100644 --- a/src/lib/kit/components/LongValue/LongValue.tsx +++ b/src/lib/kit/components/LongValue/LongValue.tsx @@ -1,6 +1,8 @@ import React from 'react'; -import {Text} from '@gravity-ui/uikit'; +import once from 'lodash/once'; + +import {Text, type TextProps} from '@gravity-ui/uikit'; import {block} from '../../utils'; @@ -8,13 +10,11 @@ import './LongValue.scss'; const b = block('long-value'); -export interface LongValueProps { +export interface LongValueProps extends TextProps { value?: string | number | boolean; - className?: string; } -export const LongValue: React.FC = ({value, className}) => { - const prevValue = React.useRef(null); +export const LongValue: React.FC = ({value, className, ...restProps}) => { const ref = React.useRef(null); const [open, setOpen] = React.useState(false); const [long, setLong] = React.useState(false); @@ -23,10 +23,10 @@ export const LongValue: React.FC = ({value, className}) => { React.useEffect(() => { if (ref.current) { - if (value !== prevValue.current) { - const {offsetWidth, scrollWidth} = ref.current; + const {offsetWidth, scrollWidth} = ref.current; - if (offsetWidth < scrollWidth) { + const setFlags = once((offsetW: number, scrollW: number) => { + if (offsetW < scrollW) { if (!long) { setLong(true); } @@ -39,15 +39,37 @@ export const LongValue: React.FC = ({value, className}) => { setOpen(false); } } + }); + + // element has been rendered, but is not displayed on the page + if (offsetWidth === 0 && scrollWidth === 0) { + const observer = new IntersectionObserver((entries) => { + const entry = entries.find((e) => e.target === ref.current); - prevValue.current = value; + if (entry && entry.isIntersecting) { + const target = entry.target as HTMLDivElement; + + setFlags(target.offsetWidth, target.scrollWidth); + } + }); + + observer.observe(ref.current); + + return () => { + observer.disconnect(); + }; + } else { + setFlags(offsetWidth, scrollWidth); } } - }); + + return; + }, [value]); return ( = ({ search={searchInput} /> - {togglerInput === 'view' ? ( -
- -
- ) : null} +
+ +
{togglerInput === 'json' ? (
{renderMonaco(form.values.input)}
) : null}