Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X2-10922 added dynamic tooltip for tags #351

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading