Skip to content

Commit

Permalink
Select: fix strategy="fixed" not scrolling to selected (#2112)
Browse files Browse the repository at this point in the history
* Fixed strategy=fixed not scrolling to selected

* Minor fix in story

---------

Co-authored-by: josephmathew900 <[email protected]>
  • Loading branch information
farhanlatheef and josephmathew900 authored Mar 6, 2024
1 parent 4b91c30 commit 48c2dcb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
27 changes: 18 additions & 9 deletions src/components/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,24 @@ const CustomInput = props => {
);
};

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

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

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

const Placeholder = props => {
const { selectProps } = props;
Expand Down
16 changes: 8 additions & 8 deletions stories/Components/Select.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ const Template = args => (
</div>
);

const OPTIONS = Array.from({ length: 20 }, (_, index) => ({
value: `value${index}`,
label: `Value ${index}`,
}));

const Default = Template.bind({});
Default.args = {
label: "Select",
defaultValue: { value: "value3", label: "Value three" },
defaultValue: { value: "value15", label: "Value 15" },
placeholder: "Select an option",
isDisabled: false,
isClearable: true,
isSearchable: true,
name: "ValueList",
optionRemapping: {},
options: [
{ value: "value1", label: "Value one" },
{ value: "value2", label: "Value two" },
{ value: "value3", label: "Value three" },
{ value: "value4", label: "Value four" },
{ value: "value5", label: "Value five" },
],
options: OPTIONS,
strategy: "fixed",
};

const Sizes = args => (
Expand Down

0 comments on commit 48c2dcb

Please sign in to comment.