Skip to content

Commit

Permalink
feat: autoscroll singleselect options in view
Browse files Browse the repository at this point in the history
affects: @medly-components/core
  • Loading branch information
gmukul01 committed Jan 26, 2025
1 parent a1f33b9 commit d413d39
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/core/src/components/SingleSelect/Options/Options.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { WithStyle } from '@medly-components/utils';
import { useCombinedRefs, WithStyle } from '@medly-components/utils';
import type { FC } from 'react';
import { forwardRef, memo } from 'react';
import { forwardRef, memo, useEffect, useRef } from 'react';
import Option from './Option';
import * as Styled from './Options.styled';
import { OptionsProps } from './types';
const Component: FC<OptionsProps> = memo(
forwardRef(({ options, onOptionClick, hasError, ...restProps }, ref) => (
<Styled.Options {...restProps} ref={ref}>
{options.map(option => (
<Option
key={option.value}
{...option}
variant={restProps.variant}
onClick={onOptionClick}
hasError={hasError}
size={restProps.size}
maxWidth={restProps.maxWidth}
includesNestedOptions={restProps.includesNestedOptions}
/>
))}
</Styled.Options>
))
forwardRef(({ options, onOptionClick, hasError, ...restProps }, ref) => {
const optionsRef = useCombinedRefs<HTMLUListElement>(ref, useRef(null));

useEffect(() => {
optionsRef?.current?.scrollIntoView({ behavior: 'smooth' });
}, []);

return (
<Styled.Options {...restProps} ref={optionsRef}>
{options.map(option => (
<Option
key={option.value}
{...option}
variant={restProps.variant}
onClick={onOptionClick}
hasError={hasError}
size={restProps.size}
maxWidth={restProps.maxWidth}
includesNestedOptions={restProps.includesNestedOptions}
/>
))}
</Styled.Options>
);
})
);
Component.defaultProps = {
isNested: false
Expand Down

0 comments on commit d413d39

Please sign in to comment.