Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Combobox - avoid checking children on parent select #577

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/amplify-components",
"version": "7.9.5",
"version": "7.9.6",
"description": "Frontend Typescript components for the Amplify team",
"main": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Inputs/ComboBox/ComboBox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ test('Parent multi select - nested selection with preselected parent works as ex
await user.keyboard('{ArrowDown}');
await user.keyboard('{Enter}');

expect(handler).toBeCalledWith([items[0].children[1]], items[0].children[0]);
expect(handler).toBeCalledWith([items[0].children[0]], items[0].children[0]);
});

test('Parent multi select - nested child label shows as expected', async () => {
Expand Down
4 changes: 1 addition & 3 deletions src/components/Inputs/ComboBox/ComboBoxMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ export const ComboBoxMenuItem = <T extends ComboBoxOptionRequired>(
onItemKeyDown,
onItemSelect,
selectableParent = true,
isParentSelected = false,
} = props;
const [openParent, setOpenParent] = useState(false);
const focusingChildIndex = useRef<number>(-1);
const childRefs = useRef<(HTMLButtonElement | null)[]>([]);

const selectedValues =
'values' in props ? props.values.map(({ value }) => value) : [];
const isSelected = selectedValues.includes(item.value) || isParentSelected;
const isSelected = selectedValues.includes(item.value);

const parentIcon = useMemo(() => {
if (!multiselect || !item.children || item.children.length === 0)
Expand Down Expand Up @@ -185,7 +184,6 @@ export const ComboBoxMenuItem = <T extends ComboBoxOptionRequired>(
onItemKeyDown={handleOnChildKeyDown}
onItemSelect={onItemSelect}
selectableParent={selectableParent}
isParentSelected={isSelected}
/>
))}
</>
Expand Down
20 changes: 0 additions & 20 deletions src/hooks/useComboBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ const useComboBox = <T extends ComboBoxOptionRequired>(
return [parent, ...getParentsRecursively(parent.value)];
};

const getSiblings = (value: string): ComboBoxOption<T>[] => {
const children = getParent(value)?.children;
if (!children) return [];

return children.filter((child) => child.value !== value);
};

const getValuesToAdd = (
values: ComboBoxOption<T>[],
item: ComboBoxOption<T>
Expand All @@ -154,19 +147,6 @@ const useComboBox = <T extends ComboBoxOptionRequired>(
({ value }) => value
);

const isIncludedByParent = values.some(({ value }) =>
[...flatParents].includes(value)
);

const siblings = getSiblings(item.value);

if (isIncludedByParent) {
return [
...siblings,
...values.filter(({ value }) => !flatParents.includes(value)),
];
}

return [
item,
...values.filter(
Expand Down
Loading