Skip to content

Commit

Permalink
Add closeOnSelect prop to SelectMenu (#588)
Browse files Browse the repository at this point in the history
* Add closeOnSelect prop to SelectMenu

This change adds a prop that when true the select
menu will close when an option has been selected.

closes #456

* Add docs for SelectMenu closeOnSave
  • Loading branch information
robphoenix authored and Solon Aguiar committed Jun 20, 2019
1 parent 9fc7444 commit a3b44d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/src/pages/components/select-menu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ and uses `react-tiny-virtual-list` for the rendering of the virtualized list of
The `SelectMenu` is unopinionated in how many items are selected in the list.
Pass an array to the `selected` prop to select more items.

## Close on select

The `SelectMenu` by default will stay open when an option is selected.
This can be configured so that the menu closes on selection.
This will not apply for Multiselect menus.

## Options prop structure

```js static
Expand Down
12 changes: 11 additions & 1 deletion src/select-menu/src/OptionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ export default class OptionsList extends PureComponent {
*/
isMultiSelect: PropTypes.bool,

/**
* When true, menu closes on option selection.
*/
closeOnSelect: PropTypes.bool,

/**
* This holds the values of the options
*/
selected: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
selected: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
onSelect: PropTypes.func,
onDeselect: PropTypes.func,
onFilterChange: PropTypes.func,
Expand Down Expand Up @@ -201,6 +208,9 @@ export default class OptionsList extends PureComponent {

handleSelect = item => {
this.props.onSelect(item)
if (!this.props.isMultiSelect && this.props.closeOnSelect) {
this.props.close()
}
}

handleDeselect = item => {
Expand Down
12 changes: 10 additions & 2 deletions src/select-menu/src/SelectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ export default class SelectMenu extends PureComponent {
* Can be a function that returns a node, or a node itself, that is
* rendered instead of the options list when there are no options.
*/
emptyView: PropTypes.oneOfType([PropTypes.func, PropTypes.node])
emptyView: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),

/*
* When true, menu closes on option selection.
*/
closeOnSelect: PropTypes.bool
}

static defaultProps = {
Expand All @@ -116,7 +121,8 @@ export default class SelectMenu extends PureComponent {
position: Position.BOTTOM_LEFT,
isMultiSelect: false,
filterPlaceholder: 'Filter...',
filterIcon: 'search'
filterIcon: 'search',
closeOnSelect: false
}

getDetailView = (close, detailView) => {
Expand Down Expand Up @@ -163,6 +169,7 @@ export default class SelectMenu extends PureComponent {
emptyView,
titleView,
isMultiSelect,
closeOnSelect,
...props
} = this.props

Expand Down Expand Up @@ -196,6 +203,7 @@ export default class SelectMenu extends PureComponent {
close={close}
{...this.getDetailView(close, detailView)}
{...this.getEmptyView(close, emptyView)}
closeOnSelect={closeOnSelect}
/>
)}
{...props}
Expand Down
9 changes: 8 additions & 1 deletion src/select-menu/src/SelectMenuContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export default class SelectMenuContent extends PureComponent {
*/
isMultiSelect: PropTypes.bool,

/*
* When true, menu closes on option selection.
*/
closeOnSelect: PropTypes.bool,

/**
* Node that is placed in the header section, above the options.
*/
Expand Down Expand Up @@ -84,7 +89,8 @@ export default class SelectMenuContent extends PureComponent {
titleView,
detailView,
emptyView,
isMultiSelect
isMultiSelect,
closeOnSelect
} = this.props

const headerHeight = 40
Expand Down Expand Up @@ -114,6 +120,7 @@ export default class SelectMenuContent extends PureComponent {
options={options}
isMultiSelect={isMultiSelect}
close={close}
closeOnSelect={closeOnSelect}
{...listProps}
/>
)}
Expand Down
13 changes: 13 additions & 0 deletions src/select-menu/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ storiesOf('select-menu', module).add('SelectMenu', () => (
</SelectMenu>
)}
</Manager>
<Manager>
{({ setState, state }) => (
<SelectMenu
title="Select name"
options={options}
selected={state.selected}
onSelect={item => setState({ selected: item.value })}
closeOnSelect
>
<Button>Menu will close on select</Button>
</SelectMenu>
)}
</Manager>
<Manager>
{({ setState, state }) => (
<Pane display="inline-block">
Expand Down

0 comments on commit a3b44d1

Please sign in to comment.