Skip to content

Commit

Permalink
fix(select): fixed issue in selected group options
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaRodyukov committed Oct 21, 2024
1 parent 9097936 commit a438626
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 4 additions & 15 deletions packages/select/src/components/options-list/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ import { GroupShape, OptionShape, OptionsListProps } from '../../typings';
import { isGroup, useVisibleOptions } from '../../utils';
import { Optgroup as DefaultOptgroup } from '../optgroup';

import styles from './index.module.css';

const createCounter = () => {
let count = 0;
import { createCounter, getSelectedItemsInGroup } from './utils';

// eslint-disable-next-line no-plusplus
return () => count++;
};
import styles from './index.module.css';

export const OptionsList = forwardRef<HTMLDivElement, OptionsListProps>(
(
Expand Down Expand Up @@ -85,15 +80,9 @@ export const OptionsList = forwardRef<HTMLDivElement, OptionsListProps>(
const scrollbarRef = useRef<HTMLDivElement>(null);
const counter = createCounter();
const renderGroup = (group: GroupShape) => {
const groupSelectedItems = selectedItems?.filter((item) =>
group.options.includes(item),
);
const groupSelectedItems = getSelectedItemsInGroup(selectedItems, group);
const handleSelectedItems = (items: OptionShape[]) => {
setSelectedItems(
(selectedItems?.filter((item) => !group.options.includes(item)) ?? []).concat(
items,
),
);
setSelectedItems(getSelectedItemsInGroup(selectedItems, group).concat(items));
};

return (
Expand Down
18 changes: 18 additions & 0 deletions packages/select/src/components/options-list/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GroupShape, OptionsListProps } from '../../typings';

export const getSelectedItemsInGroup = (
selectedItems: OptionsListProps['selectedItems'],
group: GroupShape,
) =>
selectedItems === undefined
? []
: selectedItems.filter(({ key: selectedItemKey }) =>
group.options.some((option) => option.key === selectedItemKey),
);

export const createCounter = () => {
let count = 0;

// eslint-disable-next-line no-plusplus
return () => count++;
};

0 comments on commit a438626

Please sign in to comment.