Skip to content

Commit

Permalink
feat(dropdown-list): implements multiselect variant of dropdown-list
Browse files Browse the repository at this point in the history
* feat(dropdown-list): implements multiselect variant of dropdown-list

Co-authored-by: ogermain <[email protected]>
  • Loading branch information
pylafleur and ogermain-kronos authored May 28, 2024
1 parent 8430861 commit c6822b1
Show file tree
Hide file tree
Showing 10 changed files with 811 additions and 92 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/components/date-picker/calendar-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const CalendarHeader: VoidFunctionComponent<CalendarHeaderProps> = ({
ariaLabel={t('monthSelectLabel')}
data-testid="month-select"
options={monthsOptions}
onChange={(options) => {
onChange={(options: DropdownListOption) => {
changeMonth(months.indexOf(options.label));
}}
value={monthsOptions[getMonth(date)].value}
Expand All @@ -100,7 +100,7 @@ export const CalendarHeader: VoidFunctionComponent<CalendarHeaderProps> = ({
ariaLabel={t('yearSelectLabel')}
data-testid="year-select"
options={yearsOptions}
onChange={(options) => {
onChange={(options: DropdownListOption) => {
changeYear(parseInt(options.value, 10));
}}
value={getYear(date).toString()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1921,11 +1921,11 @@ input + .c1 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
height: var(--size-2x);
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
min-height: var(--size-2x);
padding: 0 var(--spacing-1x);
text-wrap: none;
-webkit-user-select: none;
Expand Down Expand Up @@ -2327,7 +2327,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a month"
aria-labelledby="uuid2_label"
aria-labelledby="uuid2_label uuid2_listbox_october_label"
aria-required="false"
class="c12"
data-testid="month-select"
Expand Down Expand Up @@ -2369,7 +2369,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a year"
aria-labelledby="uuid3_label"
aria-labelledby="uuid3_label uuid3_listbox_2010_label"
aria-required="false"
class="c12"
data-testid="year-select"
Expand Down Expand Up @@ -3131,11 +3131,11 @@ input + .c1 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
height: var(--size-2x);
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
min-height: var(--size-2x);
padding: 0 var(--spacing-1x);
text-wrap: none;
-webkit-user-select: none;
Expand Down Expand Up @@ -3543,7 +3543,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a month"
aria-labelledby="uuid2_label"
aria-labelledby="uuid2_label uuid2_listbox_october_label"
aria-required="false"
class="c12"
data-testid="month-select"
Expand Down Expand Up @@ -3585,7 +3585,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a year"
aria-labelledby="uuid3_label"
aria-labelledby="uuid3_label uuid3_listbox_2010_label"
aria-required="false"
class="c12"
data-testid="year-select"
Expand Down Expand Up @@ -4276,11 +4276,11 @@ input + .c1 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
height: var(--size-2halfx);
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
min-height: var(--size-2halfx);
padding: 0 var(--spacing-1x);
text-wrap: none;
-webkit-user-select: none;
Expand Down Expand Up @@ -4680,7 +4680,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a month"
aria-labelledby="uuid2_label"
aria-labelledby="uuid2_label uuid2_listbox_october_label"
aria-required="false"
class="c12"
data-testid="month-select"
Expand Down Expand Up @@ -4722,7 +4722,7 @@ label + .c3 {
aria-expanded="false"
aria-invalid="false"
aria-label="Select a year"
aria-labelledby="uuid3_label"
aria-labelledby="uuid3_label uuid3_listbox_2010_label"
aria-required="false"
class="c12"
data-testid="year-select"
Expand Down
51 changes: 51 additions & 0 deletions packages/react/src/components/dropdown-list/dropdown-list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ describe('Dropdown list', () => {
const wrapper = shallow(<DropdownList options={options} defaultValue="" />);

expect(getByTestId(wrapper, 'textbox').prop('value')).toBe('');
});

test('the specified defaultValues are independently displayed when list is multiselect', () => {
const wrapper = shallow(<DropdownList options={provinces} defaultValue={['nl', 'qc']} multiselect />);

expect(getByTestId(wrapper, 'listboxtag-qc').exists()).toBe(true);
expect(getByTestId(wrapper, 'listboxtag-nl').exists()).toBe(true);
expect(getByTestId(wrapper, 'tag-wrapper').children()).toHaveLength(2);
expect(getByTestId(wrapper, 'input').prop('value')).toBe('nl|qc');
});

test('no defaultValues are displayed when not specified and list is multiselect', () => {
const wrapper = shallow(<DropdownList options={provinces} multiselect />);

expect(getByTestId(wrapper, 'tag-wrapper').children()).toHaveLength(0);
expect(getByTestId(wrapper, 'input').prop('value')).toBe('');
});
});
Expand Down Expand Up @@ -129,6 +144,16 @@ describe('Dropdown list', () => {

expect(getByTestId(wrapper, 'textbox').prop('value')).toBe('bc');
});

test('clicking an option selects it and adds it to the input values when list is multiselect', () => {
const wrapper = mountWithTheme(<DropdownList options={provinces} defaultOpen multiselect />);

getByTestId(wrapper, 'listitem-nl').simulate('click');
getByTestId(wrapper, 'listitem-qc').simulate('click');

expect(getByTestId(wrapper, 'textbox').prop('value')).toBe('nl|qc');
expect(getByTestId(wrapper, 'input').prop('value')).toBe('nl|qc');
});
});

describe(('component is controlled'), () => {
Expand Down Expand Up @@ -313,6 +338,20 @@ describe('Dropdown list', () => {
expect(getByTestId(wrapper, 'input').prop('value')).toBe('sk');
});

test('Enter removes the focused Tag when list is multiselect', () => {
const wrapper = mountWithTheme(
<DropdownList options={provinces} defaultValue={['ab', 'bc']} defaultOpen multiselect />,
);

getByTestId(wrapper, 'listboxtag-bc').simulate(
'keydown',
{ key: 'Enter', preventDefault: jest.fn() },
);

expect(getByTestId(wrapper, 'textbox').prop('value')).toBe('ab');
expect(getByTestId(wrapper, 'input').prop('value')).toBe('ab');
});

describe('when typing printable characters', () => {
test('listbox opens when typing printable characters', () => {
const wrapper = shallow(<DropdownList options={provinces} />);
Expand Down Expand Up @@ -427,4 +466,16 @@ describe('Dropdown list', () => {

expect(tree).toMatchSnapshot();
});

test('matches the snapshot (multiselect)', () => {
const tree = renderWithProviders(
<DropdownList
defaultOpen
options={provinces}
multiselect
/>,
);

expect(tree).toMatchSnapshot();
});
});
Loading

0 comments on commit c6822b1

Please sign in to comment.