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 636842f
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions packages/core/src/InlineEditor/InlineEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,19 @@ export const HvInlineEditor = fixedForwardRef(function HvInlineEditor<
const [cachedValue, setCachedValue] = useState(value);
const inputRef = useRef<HTMLInputElement>();
const { activeTheme } = useTheme();
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 +138,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 +173,35 @@ 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
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 636842f

Please sign in to comment.