Skip to content

Commit

Permalink
X2-10922 added dynamic tooltip for tags (#351)
Browse files Browse the repository at this point in the history
added dynamic tooltip for tags
  • Loading branch information
SemenStruchev authored Oct 29, 2024
1 parent 8aa7bf0 commit f8525f8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/components/Forms/ComboBox.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import clsx from "clsx";
import PropTypes from "prop-types";
import React from "react";
import Select from "react-select";
import React, { useEffect, useRef, useState } from "react";
import Select, { components } from "react-select";
import CreatableSelect from "react-select/creatable";
import "./ComboBox.css";
import { Tooltip } from "../Tooltip";

// TODO: Common parameters should be defined in stories like `options` and `defaultValue`
export const ComboBox = ({ isCreatable = false, className, isError, ...rest }) => {
Expand All @@ -17,6 +18,7 @@ export const ComboBox = ({ isCreatable = false, className, isError, ...rest }) =
? {
IndicatorsContainer: () => null,
Menu: () => null,
MultiValueLabel: CustomMultiValue,
}
: null
}
Expand All @@ -25,6 +27,28 @@ export const ComboBox = ({ isCreatable = false, className, isError, ...rest }) =
);
};

const { MultiValueLabel } = components;

const CustomMultiValue = (props) => {
const labelRef = useRef(null);
const [isTooltipDisabled, setIsTooltipDisabled] = useState(true);

useEffect(() => {
if (labelRef.current) {
const isOverflowing = labelRef.current.offsetWidth > labelRef.current.offsetParent.offsetWidth;
setIsTooltipDisabled(!isOverflowing);
}
}, [props.data.label]);

return (
<MultiValueLabel {...props}>
<Tooltip disabled={isTooltipDisabled} maxWidth="none" content={<span>{props.data.label}</span>}>
<span ref={labelRef}>{props.data.label}</span>
</Tooltip>
</MultiValueLabel>
);
};

ComboBox.propTypes = {
className: PropTypes.string,
isError: PropTypes.bool,
Expand Down

0 comments on commit f8525f8

Please sign in to comment.