-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SelectField] [DropDownMenu] Support multi select (#6165)
* [SelectField] Add multi select to SelectField and DropDownMenu Make `Menu`s multi select support available in SelectField and DropDownMenu. Add `multiple: PropTypes.bool` to SelectField and DropDownMenu, and document that if true, value props and value/payload onChange parameters are arrays. * [SelectField] Add onChangeRenderer Make it possible to change the default comma separated rendering * rename onChangeRenderer to selectionRenderer * Add ExampleMultiSelect docs example * Handle multiple value initialized to null * Adhere to naming convension: handleOnChange -> handleChange * Fix lint errors * Add multi select test to DropDownMenu * Add multi select test to Menu * Add multi select test to SelectField * Handle initial null menuValue * Always pass value when a selectionRenderer is provided. * Avoid function indirection and destructuration overkill * Remove blank lines from render * Assert state before and after deselecting item1 * Cleanup component by unmounting wrapper in afterEach * Add SelectField selectionRenderer test * require react-tap-event-plugin for karma tests * Only persist event when necessary. * Add a little documentation * Add ExampleSelectionRenderer * Remove multiple guard in handleTouchTapControl The ClearFix component is covered by the Popover so it is not possible to interact with it.
- Loading branch information
1 parent
18e9c49
commit 20325bc
Showing
11 changed files
with
440 additions
and
25 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
docs/src/app/components/pages/components/SelectField/ExampleMultiSelect.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,53 @@ | ||
import React, {Component} from 'react'; | ||
import SelectField from 'material-ui/SelectField'; | ||
import MenuItem from 'material-ui/MenuItem'; | ||
|
||
const names = [ | ||
'Oliver Hansen', | ||
'Van Henry', | ||
'April Tucker', | ||
'Ralph Hubbard', | ||
'Omar Alexander', | ||
'Carlos Abbott', | ||
'Miriam Wagner', | ||
'Bradley Wilkerson', | ||
'Virginia Andrews', | ||
'Kelly Snyder', | ||
]; | ||
|
||
/** | ||
* `SelectField` can handle multiple selections. It is enabled with the `multiple` property. | ||
*/ | ||
export default class SelectFieldExampleMultiSelect extends Component { | ||
state = { | ||
values: [], | ||
}; | ||
|
||
handleChange = (event, index, values) => this.setState({values}); | ||
|
||
menuItems(values) { | ||
return names.map((name) => ( | ||
<MenuItem | ||
key={name} | ||
insetChildren={true} | ||
checked={values && values.includes(name)} | ||
value={name} | ||
primaryText={name} | ||
/> | ||
)); | ||
} | ||
|
||
render() { | ||
const {values} = this.state; | ||
return ( | ||
<SelectField | ||
multiple={true} | ||
hintText="Select a name" | ||
value={values} | ||
onChange={this.handleChange} | ||
> | ||
{this.menuItems(values)} | ||
</SelectField> | ||
); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
docs/src/app/components/pages/components/SelectField/ExampleSelectionRenderer.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,64 @@ | ||
import React, {Component} from 'react'; | ||
import SelectField from 'material-ui/SelectField'; | ||
import MenuItem from 'material-ui/MenuItem'; | ||
|
||
const persons = [ | ||
{value: 0, name: 'Oliver Hansen'}, | ||
{value: 1, name: 'Van Henry'}, | ||
{value: 2, name: 'April Tucker'}, | ||
{value: 3, name: 'Ralph Hubbard'}, | ||
{value: 4, name: 'Omar Alexander'}, | ||
{value: 5, name: 'Carlos Abbott'}, | ||
{value: 6, name: 'Miriam Wagner'}, | ||
{value: 7, name: 'Bradley Wilkerson'}, | ||
{value: 8, name: 'Virginia Andrews'}, | ||
{value: 9, name: 'Kelly Snyder'}, | ||
]; | ||
|
||
/** | ||
* The rendering of selected items can be customized by providing a `selectionRenderer`. | ||
*/ | ||
export default class SelectFieldExampleSelectionRenderer extends Component { | ||
state = { | ||
values: [], | ||
}; | ||
|
||
handleChange = (event, index, values) => this.setState({values}); | ||
|
||
selectionRenderer = (values) => { | ||
switch (values.length) { | ||
case 0: | ||
return ''; | ||
case 1: | ||
return persons[values[0]].name; | ||
default: | ||
return `${values.length} names selected`; | ||
} | ||
} | ||
|
||
menuItems(persons) { | ||
return persons.map((person) => ( | ||
<MenuItem | ||
key={person.value} | ||
insetChildren={true} | ||
checked={this.state.values.includes(person.value)} | ||
value={person.value} | ||
primaryText={person.name} | ||
/> | ||
)); | ||
} | ||
|
||
render() { | ||
return ( | ||
<SelectField | ||
multiple={true} | ||
hintText="Select a name" | ||
value={this.state.values} | ||
onChange={this.handleChange} | ||
selectionRenderer={this.selectionRenderer} | ||
> | ||
{this.menuItems(persons)} | ||
</SelectField> | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.