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 3 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
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);
});
});
38 changes: 38 additions & 0 deletions src/components/MultiSelect/helpers/__test__/getChips.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { mount } from 'enzyme';
import getChips from '../getChips';
import { StyledChip } from '../../styled';

describe('getChips', () => {
test('should return null when value is falsey', () => {
HellWolf93 marked this conversation as resolved.
Show resolved Hide resolved
[null, undefined, false].forEach(value => {
expect(getChips(value)).toBe(null);
});
});

test('should return the correct amount of chips', () => {
const value = [
{
label: 'First',
name: 'first',
},
{
label: 'Second',
name: 'second',
},
];
const chips = getChips(value);
const wrapper = mount(<div>{chips}</div>);
expect(wrapper.find(StyledChip).length).toBe(2);
});

test('should return only one chip', () => {
const value = {
label: 'First',
name: 'first',
};
const chip = getChips(value);
const wrapper = mount(<div>{chip}</div>);
expect(wrapper.find(StyledChip).length).toBe(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import positionResolver from '../positionResolver';

const DEFAULT_MARGIN = 5;

describe('positionResolver', () => {
test('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);
});
});
21 changes: 21 additions & 0 deletions src/components/MultiSelect/helpers/getChips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { StyledChip } from '../styled';

export default function getChips(value, variant, deleteCallback) {
if (!value) {
return null;
}
if (Array.isArray(value)) {
return value.map(val => (
<StyledChip
key={val.name}
label={val.label}
variant={variant}
onDelete={() => deleteCallback(val)}
HellWolf93 marked this conversation as resolved.
Show resolved Hide resolved
/>
));
}
return (
HellWolf93 marked this conversation as resolved.
Show resolved Hide resolved
<StyledChip label={value.label} variant={variant} onDelete={() => deleteCallback(value)} />
);
}
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 positionResolver from './positionResolver';
import getChips from './getChips';

export { positionResolver, getChips };
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;
}
27 changes: 27 additions & 0 deletions src/components/MultiSelect/icons/plus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import PropTypes from 'prop-types';

const PlusIcon = props => {
const { className, style } = props;

return (
<svg className={className} style={style} width="14" height="14" viewBox="0 0 14 14">
<path
fill="#01B6F5"
HellWolf93 marked this conversation as resolved.
Show resolved Hide resolved
d="M12.181 5.964H8.036V1.819C8.036 1.247 7.573.783 7 .783s-1.036.464-1.036 1.036v4.145H1.819C1.246 5.964.783 6.428.783 7c0 .572.463 1.036 1.036 1.036h4.145v4.145c0 .572.463 1.036 1.036 1.036s1.036-.464 1.036-1.036V8.036h4.145c.573 0 1.036-.464 1.036-1.036 0-.572-.463-1.036-1.036-1.036z"
/>
</svg>
);
};

PlusIcon.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
};

PlusIcon.defaultProps = {
className: undefined,
style: undefined,
};

export default PlusIcon;
Loading