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 MultiSelect component #1606

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3c095c4
feat: implement MultiSelect component
HellWolf93 Jun 4, 2020
252a7a6
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 Jun 7, 2020
e75d728
Merge branch 'master' into implement-multiselect-component
TahimiLeonBravo Jun 9, 2020
cc704b7
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 Jun 14, 2020
bd63ca3
fix: refactor MultiSelect according to comments
HellWolf93 Jun 14, 2020
8fac4b7
fix: add more tests to MultiSelect component
HellWolf93 Jun 14, 2020
1e4d91f
Merge branch 'implement-multiselect-component' of https://github.com/…
HellWolf93 Jun 14, 2020
10be946
fix: add children prop to index.d.ts
HellWolf93 Jun 15, 2020
7211d21
fix: onChange callback param
HellWolf93 Jun 16, 2020
41a3bd3
fix: improve MultiSelect docs
HellWolf93 Jun 16, 2020
1e22381
fix: export MultiSelect from global index.d.ts
HellWolf93 Jun 18, 2020
a60cd4f
fix: MultiSelect value type
HellWolf93 Jun 18, 2020
8d61bb6
fix: split tests in 2 test cases
HellWolf93 Jun 18, 2020
58d015c
fix: use functional programming only
HellWolf93 Jun 18, 2020
415a988
fix: improve pageObject selector specificity
HellWolf93 Jun 18, 2020
60c3d2b
fix: several issues with focus and key handling
HellWolf93 Jun 18, 2020
6d5e09d
fix: remove unused useEffect
HellWolf93 Jun 18, 2020
7d9482b
fix: normalizeValue returning itemValue instead of value
HellWolf93 Jun 20, 2020
031a923
fix: MultiSelect keeping focus when TAB key pressed
HellWolf93 Jun 20, 2020
acdb2fb
Merge branch 'master' into implement-multiselect-component
LeandroTorresSicilia Jun 21, 2020
3933f1d
fix: lint error
LeandroTorresSicilia Jun 21, 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
26 changes: 26 additions & 0 deletions integration/specs/MultiSelect/multiSelect-1.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const PageMultiSelect = require('../../../src/components/MultiSelect/pageObject');

const MULTI_SELECT = '#multiselect-component-1';

describe('MultiSelect base', () => {
beforeAll(() => {
browser.url('/#!/MultiSelect/1');
});
beforeEach(() => {
browser.refresh();
const component = $(MULTI_SELECT);
component.waitForExist();
});

it('should put the input element focused when clicked', () => {
const input = new PageMultiSelect(MULTI_SELECT);
input.click();
expect(input.hasFocus()).toBe(true);
});

it('should put the input element focused when the label element is clicked', () => {
const input = new PageMultiSelect(MULTI_SELECT);
input.clickLabel();
expect(input.hasFocus()).toBe(true);
});
});
1 change: 1 addition & 0 deletions src/components/InternalDropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const InternalDropdown = forwardRef((props, reference) => {
}
return containerRef.current.focus();
},
contains: element => containerRef.current.contains(element),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember we need to be careful in what we expose here.
In this case why we need this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed in useOutsideClick hook, otherwise throws the error 'ref.current.contains' is not a function, it is used there to check if the target of the click event is a descendant of the element being rendered by the InternalOverlay component

}));

const updateScrollingArrows = () => {
Expand Down
36 changes: 36 additions & 0 deletions src/components/MultiSelect/__test__/chips.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { mount } from 'enzyme';
import Chips from '../chips';
import { StyledChip } from '../styled';

describe('<Chips />', () => {
it('should not render any chip', () => {
const component = mount(<Chips />);
expect(component.find(StyledChip).length).toBe(0);
});

it('should render only one chip', () => {
const value = { label: 'Test', name: 'test' };
const component = mount(<Chips value={value} />);
expect(component.find(StyledChip).length).toBe(1);
});

it('should render the right amount of chips', () => {
const value = [
{
label: 'Option 1',
name: 'option-1',
},
{
label: 'Option 2',
name: 'option-2',
},
{
label: 'Option 3',
name: 'option-3',
},
];
const component = mount(<Chips value={value} />);
expect(component.find(StyledChip).length).toBe(3);
});
});
30 changes: 30 additions & 0 deletions src/components/MultiSelect/__test__/multiSelect.a11y.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { mount } from 'enzyme';
import { axe } from 'jest-axe';
import MultiSelect from '..';
import Option from '../../Option';

describe('<MultiSelect/>', () => {
it('should be accessible', async () => {
expect.assertions(1);
const value = [
{
label: 'First',
name: 'first',
},
{
label: 'Second',
name: 'second',
},
];
const component = mount(
<MultiSelect value={value} label="Label">
<Option name="first" label="First" />
<Option name="second" label="Second" />
</MultiSelect>,
);
const html = component.html();
const results = await axe(html);
expect(results).toHaveNoViolations();
});
});
83 changes: 83 additions & 0 deletions src/components/MultiSelect/__test__/multiSelect.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from 'react';
import { mount } from 'enzyme';
import MultiSelect from '..';
import Option from '../../Option';
import HelpText from '../../Input/styled/helpText';
import ErrorText from '../../Input/styled/errorText';
import Label from '../../Input/label/labelText';
import { StyledChip, StyledPlaceholder, StyledInput } from '../styled';

describe('<MultiSelect />', () => {
it('should render Label when label prop is passed', () => {
const component = mount(<MultiSelect label="Label" />);
expect(component.find(Label).exists()).toBe(true);
});

it('should render a HelpText when bottomHelpText prop is passed', () => {
const component = mount(<MultiSelect bottomHelpText="Help text" />);
expect(component.find(HelpText).exists()).toBe(true);
});

it('should render a ErrorText when error prop is passed', () => {
const component = mount(<MultiSelect error="Error text" />);
expect(component.find(ErrorText).exists()).toBe(true);
});

it('should render the placeholder when there is no selected items', () => {
const component = mount(<MultiSelect placeholder="Placeholder text" />);
expect(component.find(StyledPlaceholder).exists()).toBe(true);
});

it('should render the correct amount of chips', () => {
const value = [
{
label: 'First',
name: 'first',
},
{
label: 'Second',
name: 'second',
},
];
const component = mount(
<MultiSelect value={value}>
<Option name="first" label="First" />
<Option name="second" label="Second" />
</MultiSelect>,
);
expect(component.find(StyledChip).length).toBe(2);
});

it('should fire change event when an item is removed', () => {
const value = [
{
label: 'First',
name: 'first',
},
];
const mockOnChange = jest.fn();
const component = mount(
<MultiSelect value={value} onChange={mockOnChange}>
<Option name="first" label="First" />
<Option name="second" label="Second" />
</MultiSelect>,
);
const button = component.find(StyledChip).find('button');
button.simulate('click');
expect(mockOnChange).toHaveBeenCalledWith([]);
});

it('should fire focus event', () => {
const mockOnFocus = jest.fn();
const component = mount(<MultiSelect onFocus={mockOnFocus} />);
component.find(StyledInput).simulate('focus');
expect(mockOnFocus).toHaveBeenCalledTimes(1);
});

it('should fire blur event', () => {
const mockOnBlur = jest.fn();
const component = mount(<MultiSelect onBlur={mockOnBlur} />);
component.find(StyledInput).simulate('blur');
expect(mockOnBlur).toHaveBeenCalledTimes(1);
});
});
46 changes: 46 additions & 0 deletions src/components/MultiSelect/chips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import PropTypes from 'prop-types';
import { StyledChip } from './styled';

function Chips(props) {
const { value, variant, onDelete } = props;
if (!value) {
return null;
}
if (Array.isArray(value)) {
return value.map(val => (
<StyledChip
key={val.name}
label={val.label}
variant={variant}
onDelete={() => onDelete(val)}
/>
));
}
return <StyledChip label={value.label} variant={variant} onDelete={() => onDelete(value)} />;
}

Chips.propTypes = {
value: PropTypes.oneOfType([
PropTypes.shape({
name: PropTypes.string,
label: PropTypes.string,
}),
PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
label: PropTypes.string,
}),
),
]),
variant: PropTypes.oneOf(['base', 'neutral', 'outline-brand', 'brand']),
onDelete: PropTypes.func,
};

Chips.defaultProps = {
value: undefined,
variant: 'base',
onDelete: () => {},
};

export default Chips;
17 changes: 17 additions & 0 deletions src/components/MultiSelect/helpers/__test__/hasChips.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import hasChips from '../hasChips';

describe('hasChips', () => {
it('should return true', () => {
const values = [{ label: 'Label', name: 'Name' }, [{ label: 'Label', name: 'Name' }]];
values.forEach(val => {
expect(hasChips(val)).toBe(true);
});
});

it('should return false', () => {
const values = [undefined, null, 0, false, true, []];
values.forEach((val, index) => {
expect(hasChips(val)).toBe(false);
});
});
});
21 changes: 21 additions & 0 deletions src/components/MultiSelect/helpers/__test__/normalizeValue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import normalizeValue from '../normalizeValue';

describe('normalizeValue', () => {
it('should return an empty array', () => {
const values = [undefined, null, 0, true, []];
values.forEach(val => {
expect(normalizeValue(val)).toEqual([]);
});
});

it('should return the right values', () => {
const values = [
{ label: 'Label', name: 'Name', value: 'Value', icon: 'Icon' },
[{ label: 'Label', name: 'Name', value: 'Value', icon: 'Icon' }],
];
const expected = [{ label: 'Label', name: 'Name', value: 'Value' }];
values.forEach(val => {
expect(normalizeValue(val)).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import positionResolver from '../positionResolver';

const DEFAULT_MARGIN = 5;

describe('positionResolver', () => {
it('should return the correct position', () => {
const param = {
trigger: {
leftUpAnchor: {
x: 209,
y: 175,
},
leftBottomAnchor: {
x: 209,
y: 215,
},
rightUpAnchor: {
x: 249,
y: 175,
},
rightBottomAnchor: {
x: 249,
y: 215,
},
},
viewport: {
width: 1100,
height: 761,
},
content: {
height: 220,
width: 220,
},
};
const expected = {
top: param.trigger.leftBottomAnchor.y + DEFAULT_MARGIN,
left: param.trigger.rightBottomAnchor.x - param.content.width,
};
expect(positionResolver(param)).toEqual(expected);
});
});
9 changes: 9 additions & 0 deletions src/components/MultiSelect/helpers/hasChips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function hasChips(value) {
if (!value) {
return false;
}
if (Array.isArray(value)) {
return value.length > 0;
}
return typeof value === 'object';
}
4 changes: 4 additions & 0 deletions src/components/MultiSelect/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import hasChips from './hasChips';
import positionResolver from './positionResolver';

export { hasChips, positionResolver };
26 changes: 26 additions & 0 deletions src/components/MultiSelect/helpers/normalizeValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function normalizeValue(value) {
if (!value || typeof value !== 'object') {
return [];
}
if (Array.isArray(value)) {
if (value.length === 0) {
return [];
}
return value.map(item => {
const { label, name, value: itemValue } = item;
return {
label,
name,
value: itemValue,
};
});
HellWolf93 marked this conversation as resolved.
Show resolved Hide resolved
}
const { label, name, value: itemValue } = value;
return [
{
label,
name,
value: itemValue,
},
];
}
15 changes: 15 additions & 0 deletions src/components/MultiSelect/helpers/positionResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import InternalOverlay from '../../InternalOverlay';

export default function positionResolver(opts) {
const { trigger, viewport, content } = opts;
const newOpts = {
trigger,
viewport,
content: {
...content,
},
};
const pos = InternalOverlay.defaultPositionResolver(newOpts);
pos.left = trigger.rightBottomAnchor.x - content.width;
return pos;
}
Loading