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

feat: implement showCheckbox prop on the MultiSelect component #1689

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c3cb6dd
feat: implement `showCheckbox` prop on the `MultiSelect` component
HellWolf93 Jul 6, 2020
6d2001b
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 Jul 6, 2020
5f36b04
Merge branch 'master' into implement-showCheckBox-prop
TahimiLeonBravo Jul 12, 2020
3e5b134
Merge branch 'master' into implement-showCheckBox-prop
TahimiLeonBravo Jul 15, 2020
e4e5d37
Merge branch 'master' into implement-showCheckBox-prop
TahimiLeonBravo Jul 16, 2020
5895389
fix: improve MultiSelect component
HellWolf93 Jul 29, 2020
3e5fb44
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 Jul 29, 2020
8e6b440
Merge branch 'implement-showCheckBox-prop' of https://github.com/Hell…
HellWolf93 Jul 29, 2020
ed19091
fix: outside click not working
HellWolf93 Jul 30, 2020
d27c42e
Merge branch 'master' into implement-showCheckBox-prop
TahimiLeonBravo Jul 31, 2020
ee3a1ce
Merge branch 'master' into implement-showCheckBox-prop
LeandroTorresSicilia Aug 2, 2020
d89ec6c
fix: failing test cases
HellWolf93 Aug 3, 2020
9f52c7c
fix: Option component test case failing
HellWolf93 Aug 3, 2020
d71867d
fix: remove stopPropagation usages
HellWolf93 Aug 3, 2020
36ca1d0
fix: remove unnecessary component in MultiSelect
HellWolf93 Aug 3, 2020
e4ef87e
fix: refactor getAllValues to make it more functional
HellWolf93 Aug 3, 2020
cd2141c
fix: remove unused import
HellWolf93 Aug 3, 2020
061e1c2
Merge branch 'implement-showCheckBox-prop' of https://github.com/Hell…
HellWolf93 Aug 3, 2020
b113d10
Merge branch 'master' into implement-showCheckBox-prop
LeandroTorresSicilia Aug 4, 2020
90b54f8
fix: remove unnecessary preventDefault
HellWolf93 Aug 4, 2020
ff91bb5
fix: move normalizeValue back to MultiSelect and fix tests
HellWolf93 Aug 4, 2020
a92942d
fix: update normalizeValue import
HellWolf93 Aug 4, 2020
191515a
fix: remove unnecessary timeouts
HellWolf93 Aug 4, 2020
eb81162
fix: getAllValues missing icon and tests
HellWolf93 Aug 5, 2020
98f9289
Merge branch 'implement-showCheckBox-prop' of https://github.com/Hell…
HellWolf93 Aug 5, 2020
ed8b28d
Merge branch 'master' into implement-showCheckBox-prop
LeandroTorresSicilia Aug 5, 2020
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
75 changes: 41 additions & 34 deletions src/components/InternalDropdown/__test__/internalDropdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,56 @@ import React from 'react';
import { mount } from 'enzyme';
import { ENTER_KEY } from '../../../libs/constants';
import InternalDropdown from '../';
import PicklistOption from '../../PicklistOption';
import Option from '../../Option';
import Spinner from '../../Spinner';

jest.useFakeTimers();
jest.mock('../helpers/scrollTo', () => jest.fn());

describe('InternalDropdown', () => {
beforeEach(() => {
jest.useFakeTimers();
});
it('should fire onChange when option is selected by click', () => {
const onChangeFn = jest.fn();
const component = mount(
<InternalDropdown label="Picklist" onChange={onChangeFn}>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
component
.find('li')
.at(1)
.simulate('mousedown');
jest.runAllTimers();
expect(onChangeFn).toHaveBeenCalledWith({
icon: null,
label: 'Option 2',
name: 'option2',
icon: null,
value: undefined,
});
});
it('should fire onChange with the right data when select one option and multiple is passed and there are no options selected', () => {
const onChangeFn = jest.fn();
const component = mount(
<InternalDropdown label="Picklist" onChange={onChangeFn} multiple>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
component
.find('li')
.at(2)
.simulate('mousedown');
jest.runAllTimers();
expect(onChangeFn).toHaveBeenCalledWith([
{
icon: null,
label: 'Option 3',
name: 'option3',
icon: null,
value: undefined,
},
]);
Expand All @@ -55,34 +60,35 @@ describe('InternalDropdown', () => {
const onChangeFn = jest.fn();
const value = [
{
icon: null,
label: 'Option 1',
name: 'option1',
icon: null,
value: undefined,
},
];
const component = mount(
<InternalDropdown label="Picklist" onChange={onChangeFn} multiple value={value}>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
component
.find('li')
.at(1)
.simulate('mousedown');
jest.runAllTimers();
expect(onChangeFn).toHaveBeenCalledWith([
{
icon: null,
label: 'Option 1',
name: 'option1',
icon: null,
value: undefined,
},
{
icon: null,
label: 'Option 2',
name: 'option2',
icon: null,
value: undefined,
},
]);
Expand All @@ -91,47 +97,48 @@ describe('InternalDropdown', () => {
const onChangeFn = jest.fn();
const value = [
{
icon: null,
label: 'Option 1',
name: 'option1',
icon: null,
value: undefined,
},
{
icon: null,
label: 'Option 2',
name: 'option2',
icon: null,
value: undefined,
},
];
const component = mount(
<InternalDropdown label="Picklist" onChange={onChangeFn} multiple value={value}>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
const optionsSelected = [
{
icon: null,
label: 'Option 1',
name: 'option1',
icon: null,
value: undefined,
},
];
component
.find('li')
.at(1)
.simulate('mousedown');
jest.runAllTimers();
expect(onChangeFn).toHaveBeenCalledWith(optionsSelected);
});

it('should fire onChange when option is selected by pressing ENTER key', () => {
const onChangeFn = jest.fn();
const component = mount(
<InternalDropdown label="Picklist" onChange={onChangeFn}>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
jest.runAllTimers();
Expand All @@ -142,18 +149,18 @@ describe('InternalDropdown', () => {
keyCode: ENTER_KEY,
});
expect(onChangeFn).toHaveBeenCalledWith({
icon: null,
label: 'Option 1',
name: 'option1',
icon: null,
value: undefined,
});
});
it('should render svg and input elements when enableSearch is passed', () => {
const component = mount(
<InternalDropdown label="Picklist" enableSearch>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
expect(component.find('SearchIcon').exists()).toBe(true);
Expand All @@ -162,9 +169,9 @@ describe('InternalDropdown', () => {
it('should not render svg and input elements if enableSearch is not passed', () => {
const component = mount(
<InternalDropdown label="Picklist">
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
expect(component.find('SearchIcon').exists()).toBe(false);
Expand All @@ -173,9 +180,9 @@ describe('InternalDropdown', () => {
it('should render loading icon when isLoading is passed', () => {
const component = mount(
<InternalDropdown label="Picklist" isLoading>
<PicklistOption label="Option 1" name="option1" />
<PicklistOption label="Option 2" name="option2" />
<PicklistOption label="Option 3" name="option3" />
<Option label="Option 1" name="option1" />
<Option label="Option 2" name="option2" />
<Option label="Option 3" name="option3" />
</InternalDropdown>,
);
expect(component.find(Spinner).exists()).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import getAllValues from '../getAllValues';

describe('getAllValues', () => {
it('should return the right value', () => {
const values = [
{
icon: 'Icon 1',
label: 'Label 1',
name: 'Name 1',
value: 'Value 1',
variant: 'default',
},
{
icon: 'Icon 2',
label: 'Label 2',
name: 'Name 2',
value: 'Value 2',
variant: 'default',
extra: 'extra',
},
{
icon: 'Icon 3',
label: 'Label 3',
name: 'Name 3',
value: 'Value 3',
variant: 'other',
},
];
const expected = [
{
icon: 'Icon 1',
label: 'Label 1',
name: 'Name 1',
value: 'Value 1',
},
{
icon: 'Icon 2',
label: 'Label 2',
name: 'Name 2',
value: 'Value 2',
},
];
expect(getAllValues(values)).toEqual(expected);
});
});
18 changes: 18 additions & 0 deletions src/components/InternalDropdown/helpers/getAllValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default function getAllValues(children) {
if (!Array.isArray(children)) {
return [];
}
const values = children.reduce((accumulator, child) => {
const { icon, label, name, value, variant } = child;
if (variant === 'default') {
accumulator.push({
icon,
label,
name,
value,
});
}
return accumulator;
}, []);
return values;
}
13 changes: 13 additions & 0 deletions src/components/InternalDropdown/helpers/isChecked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import getAllValues from './getAllValues';

export default function isChecked(value, children) {
if (!value || value.length === 0) {
return false;
}

if (value.length === getAllValues(children).length) {
return true;
}

return 'indeterminate';
}
Loading