Skip to content
Draft
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
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
import { expect } from 'chai';
import React from 'react';
import sinon from 'sinon';
import { SelectListWithGroups } from './select-list-with-groups';

type TestItem = {
id: string;
selected: boolean;
disabled?: boolean;
label: string;
};

const label = { displayLabelKey: 'label' as const, name: 'Connections' };

function ariaChecked(testId: string) {
return screen
.getByTestId(testId)
.closest('input')
?.getAttribute('aria-checked');
}

function click(testId: string) {
userEvent.click(screen.getByTestId(testId), undefined, {
skipPointerEventsCheck: true,
});
}

describe('SelectListWithGroups', function () {
let groups: { id: string; name: string; items: TestItem[] }[];
let onChange: sinon.SinonStub;

beforeEach(function () {
groups = [
{
id: 'g1',
name: 'Group 1',
items: [
{ id: 'a', selected: true, label: 'A' },
{ id: 'b', selected: false, label: 'B' },
],
},
{
id: 'g2',
name: 'Group 2',
items: [{ id: 'c', selected: true, label: 'C' }],
},
];
onChange = sinon.stub();
});

function renderList(disabled?: boolean) {
render(
<SelectListWithGroups
groups={groups}
label={label}
onChange={onChange}
disabled={disabled}
/>
);
}

it('renders group names, items and counts', function () {
renderList();
expect(screen.getByText('Group 1')).to.be.visible;
expect(screen.getByLabelText('B')).to.be.visible;
expect(
screen.getByTestId('select-list-group-g1-count').textContent
).to.equal('1 / 2');
expect(
screen.getByTestId('select-list-group-g2-count').textContent
).to.equal('1 / 1');
});

it('reflects selection state on the all and group checkboxes', function () {
renderList();
expect(ariaChecked('select-list-all-checkbox')).to.equal('mixed');
expect(ariaChecked('select-list-group-g1-checkbox')).to.equal('mixed');
expect(ariaChecked('select-list-group-g2-checkbox')).to.equal('true');
});

it('disables the group checkbox when all its items are disabled', function () {
groups[1].items[0].disabled = true;
renderList();
expect(
screen.getByTestId('select-list-group-g2-checkbox').closest('input')
?.ariaDisabled
).to.equal('true');
expect(
screen.getByTestId('select-list-all-checkbox').closest('input')
?.ariaDisabled
).to.not.equal('true');
});

it('disables everything when the component is disabled', function () {
renderList(true);
for (const id of [
'select-list-all-checkbox',
'select-list-group-g1-checkbox',
'select-a',
]) {
expect(screen.getByTestId(id).closest('input')?.ariaDisabled).to.equal(
'true'
);
}
});

it('select all affects every enabled item and returns a flat list in order', function () {
groups[0].items[1].disabled = true;
renderList();
click('select-list-all-checkbox');
expect(onChange).to.have.been.calledWith([
{ id: 'a', selected: true, label: 'A' },
{ id: 'b', selected: false, disabled: true, label: 'B' },
{ id: 'c', selected: true, label: 'C' },
]);
});

it('group select all affects only that group', function () {
renderList();
click('select-list-group-g1-checkbox');
expect(onChange).to.have.been.calledWith([
{ id: 'a', selected: true, label: 'A' },
{ id: 'b', selected: true, label: 'B' },
{ id: 'c', selected: true, label: 'C' },
]);
});

it('group deselect all affects only that group', function () {
renderList();
click('select-list-group-g2-checkbox');
expect(onChange).to.have.been.calledWith([
{ id: 'a', selected: true, label: 'A' },
{ id: 'b', selected: false, label: 'B' },
{ id: 'c', selected: false, label: 'C' },
]);
});

it('toggles a single item', function () {
renderList();
click('select-b');
expect(onChange).to.have.been.calledWith([
{ id: 'a', selected: true, label: 'A' },
{ id: 'b', selected: true, label: 'B' },
{ id: 'c', selected: true, label: 'C' },
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import React from 'react';
import { spacing } from '@leafygreen-ui/tokens';
import { css, cx } from '@leafygreen-ui/emotion';
import { palette } from '@leafygreen-ui/palette';
import { useDarkMode } from '../hooks/use-theme';
import { Checkbox } from './leafygreen';
import {
checkboxStyles,
checkboxSelectAllStyles,
containerStyles,
listHeaderStyles,
listBodyStyles,
selectAllLabelStyles,
} from './select-list';
import type { SelectItem } from './select-list';

const groupsStyles = css({
display: 'flex',
flexDirection: 'column',
gap: spacing[300] + spacing[100],
paddingTop: spacing[200],
});

const groupStyles = css({
display: 'flex',
flexDirection: 'column',
border: `1px solid ${palette.gray.light2}`,
borderRadius: spacing[200],
overflow: 'hidden',
flexShrink: 0,
});
const groupStylesDark = css({ borderColor: palette.gray.dark2 });

const groupHeaderStyles = css({
display: 'flex',
alignItems: 'center',
// Matches the LG checkbox-to-label gap.
gap: spacing[200],
padding: `${spacing[200]}px ${spacing[300]}px`,
fontWeight: 700,
backgroundColor: palette.gray.light3,
});
const groupHeaderStylesDark = css({ backgroundColor: palette.gray.dark3 });

const groupNameStyles = css({
display: 'flex',
alignItems: 'center',
gap: spacing[100],
flex: 1,
});

const groupCountStyles = css({
fontSize: 11,
fontWeight: 600,
color: palette.gray.dark1,
});

const groupItemStyles = css({
padding: `${spacing[200]}px ${spacing[300]}px`,
paddingLeft: spacing[300] + spacing[600],
borderTop: `1px solid ${palette.gray.light2}`,
});
const groupItemStylesDark = css({ borderColor: palette.gray.dark2 });

export type SelectGroup<T extends SelectItem> = {
id: string;
name: string | JSX.Element;
icon?: React.ReactNode;
items: T[];
};

type SelectListWithGroupsProps<T extends SelectItem> = {
groups: SelectGroup<T>[];
label: {
displayLabelKey: string & keyof T;
ariaLabelKey?: string & keyof T;
name: string | JSX.Element;
};
onChange: (newList: T[]) => void;
disabled?: boolean;
className?: string;
};

function selectionState<T extends SelectItem>(items: T[]) {
const all = items.every((item) => item.selected);
const none = items.every((item) => !item.selected);
return {
checked: all,
indeterminate: !all && !none,
allDisabled: items.every((item) => item.disabled),
selectedCount: items.filter((item) => item.selected).length,
};
}

export function SelectListWithGroups<T extends SelectItem>(
props: SelectListWithGroupsProps<T>
): React.ReactElement {
const { groups, label, disabled, onChange } = props;
const isDarkMode = useDarkMode();

const items = groups.flatMap((group) => group.items);
const allState = selectionState(items);

const setSelected = (predicate: (item: T) => boolean, selected: boolean) => {
onChange(
items.map((item) =>
predicate(item) && !item.disabled ? { ...item, selected } : item
)
);
};

return (
<div className={cx(props.className, containerStyles)}>
<div className={listHeaderStyles}>
<Checkbox
className={cx(checkboxStyles, checkboxSelectAllStyles)}
id="select-list-all"
data-testid="select-list-all-checkbox"
aria-label="Select all"
onChange={(e) => setSelected(() => true, e.target.checked)}
checked={allState.checked}
indeterminate={allState.indeterminate}
disabled={disabled || allState.allDisabled}
/>
<div className={selectAllLabelStyles}>{label.name}</div>
</div>
<div className={cx(listBodyStyles, groupsStyles)}>
{groups.map((group) => {
const groupState = selectionState(group.items);
return (
<div
key={`select-list-group-${group.id}`}
data-testid={`select-list-group-${group.id}`}
className={cx(groupStyles, isDarkMode && groupStylesDark)}
>
<div
className={cx(
groupHeaderStyles,
isDarkMode && groupHeaderStylesDark
)}
>
<Checkbox
id={`select-list-group-${group.id}`}
data-testid={`select-list-group-${group.id}-checkbox`}
aria-label={
typeof group.name === 'string'
? `Select all in ${group.name}`
: 'Select all in group'
}
onChange={(e) =>
setSelected(
(item) => group.items.some((i) => i.id === item.id),
e.target.checked
)
}
checked={groupState.checked}
indeterminate={groupState.indeterminate}
disabled={disabled || groupState.allDisabled}
/>
<div className={groupNameStyles}>
{group.icon}
{group.name}
</div>
<div
className={groupCountStyles}
data-testid={`select-list-group-${group.id}-count`}
>
{groupState.selectedCount} / {group.items.length}
</div>
</div>
{group.items.map((item) => (
<div
className={cx(
groupItemStyles,
isDarkMode && groupItemStylesDark
)}
key={`select-list-item-${item.id}`}
data-testid={`select-list-item-${item.id}`}
>
<Checkbox
id={`select-${item.id}`}
name={`select-${item.id}`}
data-testid={`select-${item.id}`}
label={item[label.displayLabelKey] as React.ReactNode}
aria-label={
item[
label.ariaLabelKey ?? label.displayLabelKey
] as string
}
onChange={(e) =>
setSelected((i) => i.id === item.id, e.target.checked)
}
checked={item.selected}
disabled={item.disabled || disabled}
/>
</div>
))}
</div>
);
})}
</div>
</div>
);
}
Loading
Loading