Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

[added] Add renderMenuWrapper prop #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ and keyboard navigation logic will break. `styles` will contain
{ top, left, minWidth } which are the coordinates of the top-left corner
and the width of the dropdown menu.

#### `renderMenuWrapper: Function` (optional)
Default value:
```jsx
function(menu) {
return menu;
}
```

Arguments: `menu: React element`

Invoked to generate a wrapper around the menu component. Use this if you
want to create a React Portal around the menu, e.g.:
```jsx
function(menu) {
return createPortal(menu, document.body);
}
```

#### `selectOnBlur: Boolean` (optional)
Default value: `false`

Expand Down
13 changes: 12 additions & 1 deletion lib/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ class Autocomplete extends React.Component {
* and the width of the dropdown menu.
*/
renderMenu: PropTypes.func,
/**
* Arguments: `menu: React element`
*
* Invoked to generate a wrapper around the menu component. Use this if you
* want to create a React Portal around the menu, e.g.:
* `(menu) => createPortal(menu, document.body)`
*/
renderMenuWrapper: PropTypes.func,
/**
* Styles that are applied to the dropdown menu in the default `renderMenu`
* implementation. If you override `renderMenu` and you want to use
Expand Down Expand Up @@ -175,6 +183,9 @@ class Autocomplete extends React.Component {
renderMenu(items, value, style) {
return <div style={{ ...style, ...this.menuStyle }} children={items}/>
},
renderMenuWrapper(menu) {
return menu
},
menuStyle: {
borderRadius: '3px',
boxShadow: '0 2px 12px rgba(0, 0, 0, 0.1)',
Expand Down Expand Up @@ -586,7 +597,7 @@ class Autocomplete extends React.Component {
onClick: this.composeEventHandlers(this.handleInputClick, inputProps.onClick),
value: this.props.value,
})}
{open && this.renderMenu()}
{open && this.props.renderMenuWrapper(this.renderMenu())}
{this.props.debug && (
<pre style={{ marginLeft: 300 }}>
{JSON.stringify(this._debugStates.slice(Math.max(0, this._debugStates.length - 5), this._debugStates.length), null, 2)}
Expand Down
17 changes: 17 additions & 0 deletions lib/__tests__/Autocomplete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,23 @@ describe('Autocomplete#renderMenu', () => {
})
})

describe('Autocomplete#renderMenuWrapper', () => {
it('should be invoked in `render` to create a wrapper around the menu', () => {
class MenuWrapper extends React.Component {
render() {
return this.props.children
}
}
const tree = mount(AutocompleteComponentJSX({
renderMenuWrapper(menu) {
return <MenuWrapper>{menu}</MenuWrapper>
}
}))
tree.setState({ isOpen: true })
expect(tree.find(MenuWrapper).length).toBe(1)
})
})

describe('Autocomplete isItemSelectable', () => {
const autocompleteWrapper = mount(AutocompleteComponentJSX({
open: true,
Expand Down