-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
LeandroTorresSicilia
merged 21 commits into
nexxtway:master
from
HellWolf93:implement-multiselect-component
Jun 21, 2020
Merged
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 252a7a6
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 e75d728
Merge branch 'master' into implement-multiselect-component
TahimiLeonBravo cc704b7
Merge branch 'master' of https://github.com/nexxtway/react-rainbow in…
HellWolf93 bd63ca3
fix: refactor MultiSelect according to comments
HellWolf93 8fac4b7
fix: add more tests to MultiSelect component
HellWolf93 1e4d91f
Merge branch 'implement-multiselect-component' of https://github.com/…
HellWolf93 10be946
fix: add children prop to index.d.ts
HellWolf93 7211d21
fix: onChange callback param
HellWolf93 41a3bd3
fix: improve MultiSelect docs
HellWolf93 1e22381
fix: export MultiSelect from global index.d.ts
HellWolf93 a60cd4f
fix: MultiSelect value type
HellWolf93 8d61bb6
fix: split tests in 2 test cases
HellWolf93 58d015c
fix: use functional programming only
HellWolf93 415a988
fix: improve pageObject selector specificity
HellWolf93 60c3d2b
fix: several issues with focus and key handling
HellWolf93 6d5e09d
fix: remove unused useEffect
HellWolf93 7d9482b
fix: normalizeValue returning itemValue instead of value
HellWolf93 031a923
fix: MultiSelect keeping focus when TAB key pressed
HellWolf93 acdb2fb
Merge branch 'master' into implement-multiselect-component
LeandroTorresSicilia 3933f1d
fix: lint error
LeandroTorresSicilia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/components/MultiSelect/__test__/multiSelect.a11y.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/components/MultiSelect/helpers/__test__/hasChips.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/components/MultiSelect/helpers/__test__/normalizeValue.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/components/MultiSelect/helpers/__test__/positionResolver.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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