Skip to content

Commit

Permalink
fix(InlineEditor): add tooltip if the text on the Inline Editor overf…
Browse files Browse the repository at this point in the history
…lows
  • Loading branch information
plagoa committed Nov 8, 2024
1 parent 5c46d6b commit 3261229
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions packages/core/src/InlineEditor/InlineEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from "react";
import React, { useCallback, useMemo, useRef, useState } from "react";

Check failure on line 1 in packages/core/src/InlineEditor/InlineEditor.tsx

View workflow job for this annotation

GitHub Actions / Tests / Static Checks

'useCallback' is defined but never used

Check failure on line 1 in packages/core/src/InlineEditor/InlineEditor.tsx

View workflow job for this annotation

GitHub Actions / Tests / Static Checks

'useMemo' is defined but never used
import { Edit } from "@hitachivantara/uikit-react-icons";
import {
useDefaultProps,
Expand All @@ -10,6 +10,7 @@ import { HvButton, HvButtonProps } from "../Button";
import { useControlled } from "../hooks/useControlled";
import { useEnhancedEffect } from "../hooks/useEnhancedEffect";
import { HvInput, HvInputProps } from "../Input";
import { HvTooltip } from "../Tooltip";
import {
fixedForwardRef,
PolymorphicComponentRef,
Expand Down Expand Up @@ -97,10 +98,20 @@ export const HvInlineEditor = fixedForwardRef(function HvInlineEditor<
const [cachedValue, setCachedValue] = useState(value);
const inputRef = useRef<HTMLInputElement>();
const { activeTheme } = useTheme();
const btnRef = useRef<HTMLButtonElement>(null);
const [isOverflowing, setIsOverflowing] = useState(false);

const typographyStyles = activeTheme?.typography[variant] || {};
const { lineHeight } = typographyStyles;

const checkOverflow = () => {
if (inputRef.current) {
setIsOverflowing(
inputRef.current.scrollWidth > inputRef.current.clientWidth,
);
}
};

useEnhancedEffect(() => {
const input = inputRef.current;
if (editMode && input) {
Expand Down Expand Up @@ -128,12 +139,14 @@ export const HvInlineEditor = fixedForwardRef(function HvInlineEditor<
newValue = cachedValue;
setEditMode(false);
setValue(newValue);
checkOverflow();
}
onKeyDown?.(event, newValue);
};

const handleChange: HvInputProps["onChange"] = (event, val) => {
setValue(val);
checkOverflow();
onChange?.(event, val);
};

Expand Down Expand Up @@ -161,33 +174,36 @@ export const HvInlineEditor = fixedForwardRef(function HvInlineEditor<
{...others}
/>
) : (
<HvButton
variant="secondaryGhost"
overrideIconColors={false}
endIcon={
<Edit
color="secondary_60"
className={cx(classes.icon, {
[classes.iconVisible]: showIcon,
})}
/>
}
className={cx(classes.button, {
[classes.largeText]: parseInt(lineHeight as string, 10) >= 28,
})}
onClick={handleClick}
disabled={disabled}
{...buttonProps}
>
<HvTypography
variant={variant}
noWrap
className={cx(classes.text, { [classes.textEmpty]: !value })}
{...typographyProps}
<HvTooltip title={isOverflowing && value}>
<HvButton
ref={btnRef}
variant="secondaryGhost"
overrideIconColors={false}
endIcon={
<Edit
color="secondary_60"
className={cx(classes.icon, {
[classes.iconVisible]: showIcon,
})}
/>
}
className={cx(classes.button, {
[classes.largeText]: parseInt(lineHeight as string, 10) >= 28,
})}
onClick={handleClick}
disabled={disabled}
{...buttonProps}
>
{value || placeholder}
</HvTypography>
</HvButton>
<HvTypography
variant={variant}
noWrap
className={cx(classes.text, { [classes.textEmpty]: !value })}
{...typographyProps}
>
{value || placeholder}
</HvTypography>
</HvButton>
</HvTooltip>
)}
</div>
);
Expand Down

0 comments on commit 3261229

Please sign in to comment.