-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCOM-439: Add multiselection filter
- Loading branch information
Showing
5 changed files
with
154 additions
and
0 deletions.
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
52 changes: 52 additions & 0 deletions
52
lib/SearchAndSort/components/MultiSelectionFilter/MultiSelectionFilter.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,52 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { MultiSelection } from '@folio/stripes-components'; | ||
|
||
export default class MultiSelectionFilter extends React.Component { | ||
static propTypes = { | ||
dataOptions: PropTypes.arrayOf(PropTypes.shape({ | ||
label: PropTypes.node, | ||
value: PropTypes.any, | ||
})).isRequired, | ||
name: PropTypes.string.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
selectedValues: PropTypes.arrayOf(PropTypes.string), | ||
}; | ||
|
||
static defaultProps = { | ||
selectedValues: [], | ||
} | ||
|
||
onChangeHandler = (selectedDataOptions) => { | ||
const { | ||
name, | ||
onChange, | ||
} = this.props; | ||
|
||
onChange({ | ||
name, | ||
values: selectedDataOptions.map((option) => option.value), | ||
}); | ||
}; | ||
|
||
getSelectedDataOptions() { | ||
const { | ||
selectedValues, | ||
dataOptions, | ||
} = this.props; | ||
|
||
return selectedValues | ||
.map((curValue) => dataOptions.find((curAvailableValue) => curAvailableValue.value === curValue)); | ||
} | ||
|
||
render() { | ||
return ( | ||
<MultiSelection | ||
{...this.props} | ||
value={this.getSelectedDataOptions()} | ||
onChange={this.onChangeHandler} | ||
/> | ||
); | ||
} | ||
} |
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 @@ | ||
export { default } from './MultiSelectionFilter'; |
96 changes: 96 additions & 0 deletions
96
lib/SearchAndSort/components/MultiSelectionFilter/tests/MultiSelectionFilter-test.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,96 @@ | ||
import React from 'react'; | ||
import { describe, beforeEach, it } from '@bigtest/mocha'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import { mountWithContext } from '@folio/stripes-components/tests/helpers'; | ||
import MultiSelectInteractor from '@folio/stripes-components/lib/MultiSelection/tests/interactor'; | ||
|
||
import MultiSelectionFilter from '../MultiSelectionFilter'; | ||
|
||
const multiSelectionFilter = new MultiSelectInteractor(); | ||
|
||
const dataOptions = [ | ||
{ value: 'en', label: 'English' }, | ||
{ value: 'fr', label: 'French' }, | ||
{ value: 'ge', label: 'German' }, | ||
{ value: 'it', label: 'Italian' }, | ||
]; | ||
|
||
describe('MultiSelectionFilter', () => { | ||
const onChangeHandler = sinon.spy(); | ||
|
||
beforeEach(async () => { | ||
await mountWithContext( | ||
<MultiSelectionFilter | ||
name="language" | ||
dataOptions={dataOptions} | ||
onChange={onChangeHandler} | ||
/> | ||
); | ||
|
||
onChangeHandler.resetHistory(); | ||
}); | ||
|
||
it('should not render any selected values by default', () => { | ||
expect(multiSelectionFilter.values()).to.deep.equal([]); | ||
}); | ||
|
||
describe('when there are selected values', () => { | ||
beforeEach(async () => { | ||
await mountWithContext( | ||
<MultiSelectionFilter | ||
name="language" | ||
selectedValues={['en', 'fr', 'ge']} | ||
dataOptions={dataOptions} | ||
onChange={onChangeHandler} | ||
/> | ||
); | ||
}); | ||
|
||
it('should display selected values', () => { | ||
expect(multiSelectionFilter.values(0).valLabel).to.equal('English'); | ||
expect(multiSelectionFilter.values(1).valLabel).to.equal('French'); | ||
expect(multiSelectionFilter.values(2).valLabel).to.equal('German'); | ||
}); | ||
|
||
describe('after click on a value delete button', () => { | ||
beforeEach(async () => { | ||
await multiSelectionFilter.values(0).clickRemoveButton(); | ||
}); | ||
|
||
it('should call onChange callback with filter name and rest values', () => { | ||
expect(onChangeHandler.args[0]).to.deep.equal([{ | ||
name: 'language', | ||
values: ['fr', 'ge'], | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('after click on selected option', () => { | ||
beforeEach(async () => { | ||
await multiSelectionFilter.options(1).clickOption(); | ||
}); | ||
|
||
it('should call onChange callback with filter name and rest values', () => { | ||
expect(onChangeHandler.args[0]).to.deep.equal([{ | ||
name: 'language', | ||
values: ['en', 'ge'] | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('after click on unselected option', () => { | ||
beforeEach(async () => { | ||
await multiSelectionFilter.options(3).clickOption(); | ||
}); | ||
|
||
it('should pass filter name and all selected values to onChange callback', () => { | ||
expect(onChangeHandler.args[0]).to.deep.equal([{ | ||
name: 'language', | ||
values: ['en', 'fr', 'ge', 'it'], | ||
}]); | ||
}); | ||
}); | ||
}); | ||
}); |
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