Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 23 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,41 @@ const CustomInput = props => {

const CustomOption = props => {
const ref = useRef();
const { dataCy } = props.data;
const {
innerProps,
data: { dataCy, tooltipProps = {} },
} = 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 (isPresent(tooltipProps)) {
const mergedTooltipProps = {
zIndex: 1_000_001,
position: "bottom-start",
...tooltipProps,
};

return (
<Tooltip {...mergedTooltipProps}>
<div>{optionComponent}</div>
</Tooltip>
);
}

return optionComponent;
Comment thread
VedanshAgrawal22 marked this conversation as resolved.
Outdated
};

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

const OptionWithTooltip = Template.bind({});
OptionWithTooltip.storyName = "Option with Tooltip";
OptionWithTooltip.args = {
label: "Select with Tooltips",
placeholder: "Hover any option",
strategy: "fixed",
options: OPTIONS.map(option => ({
value: option.value,
label: option.label,
tooltipProps: {
position: "top-start",
content: `Tooltip content for ${option.label}`,
},
})),
};

OptionWithTooltip.parameters = {
docs: {
description: {
story: `Use the \`tooltipProps\` 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 +584,7 @@ export {
Sizes,
MultiSelect,
Grouped,
OptionWithTooltip,
Creatable,
AsyncCreatable,
Searchable,
Expand Down
20 changes: 20 additions & 0 deletions tests/Select.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,24 @@ describe("Select", () => {
"group2-option1"
);
});

it("should show tooltip when hovering over an option with tooltipProps", async () => {
const tooltipText = "Tooltip content";
const optionsWithTooltip = [
{
label: "Option 1",
value: "option-1",
tooltipProps: { content: 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();
});
});