Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 17 additions & 3 deletions src/components/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { hyphenize } from "utils";

import Label from "./Label";
import Spinner from "./Spinner";
import Tooltip from "./Tooltip";

const SIZES = { small: "small", medium: "medium", large: "large" };

Expand Down Expand Up @@ -75,22 +76,35 @@ const CustomInput = props => {

const CustomOption = props => {
const ref = useRef();
const { dataCy } = props.data;
const {
innerProps,
data: { dataCy, tooltipContent = "" },
} = props;

useEffect(() => {
props.isSelected && ref.current.scrollIntoView();
}, [props.isSelected]);

return (
const optionComponent = (
<components.Option
{...props}
innerRef={ref}
innerProps={{
...props.innerProps,
...innerProps,
"data-cy": dataCy || `${hyphenize(props.label)}-select-option`,
}}
/>
);

if (tooltipContent) {
return (
<Tooltip content={tooltipContent} zIndex={1_000_001}>
Comment thread
VedanshAgrawal22 marked this conversation as resolved.
Outdated
<div>{optionComponent}</div>
</Tooltip>
);
}

return optionComponent;
};

const Placeholder = props => {
Expand Down
22 changes: 22 additions & 0 deletions stories/Components/Select.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ Grouped.args = {
],
};

const OptionTooltips = Template.bind({});
Comment thread
VedanshAgrawal22 marked this conversation as resolved.
Outdated
OptionTooltips.storyName = "Option Tooltip";
OptionTooltips.args = {
label: "Select with Tooltips",
placeholder: "Hover any option",
strategy: "fixed",
options: OPTIONS.map(option => ({
value: option.value,
label: option.label,
tooltipContent: `Tooltip content for ${option.label}`,
})),
};

OptionTooltips.parameters = {
docs: {
description: {
story: `Use the \`tooltipContent\` field on each option to show a tooltip when hovering.`,
},
},
};

const Creatable = args => {
const [options, setOptions] = useState([
{ value: "value1", label: "Value one" },
Expand Down Expand Up @@ -560,6 +581,7 @@ export {
Sizes,
MultiSelect,
Grouped,
OptionTooltips,
Creatable,
AsyncCreatable,
Searchable,
Expand Down
16 changes: 16 additions & 0 deletions tests/Select.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,20 @@ describe("Select", () => {
"group2-option1"
);
});

it("should show tooltip when hovering over an option with tooltipContent", async () => {
const tooltipText = "Tooltip content";
const optionsWithTooltip = [
{ label: "Option 1", value: "option-1", tooltipContent: tooltipText },
];

render(<Select label="Select" options={optionsWithTooltip} />);

const select = screen.getByRole("combobox");
await userEvent.click(select);
const option = screen.getByText("Option 1");
await userEvent.hover(option);

expect(await screen.findByText(tooltipText)).toBeInTheDocument();
});
});