Skip to content
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

Separate menu placement logic from menu primitive #2975

Merged
merged 4 commits into from
Aug 28, 2018
Merged
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
12 changes: 12 additions & 0 deletions docs/examples/MenuBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import Select from '../../src';

import { colourOptions } from '../data';

export default () => (
<Select
defaultValue={colourOptions[0]}
options={colourOptions}
styles={{ menu: base => ({ ...base, marginBottom: 76 }) }}
/>
);
1 change: 1 addition & 0 deletions docs/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ export { default as StyledMulti } from './StyledMulti';
export { default as StyledSingle } from './StyledSingle';
export { default as OnSelectResetsInput } from './OnSelectResetsInput';
export { default as AsyncMulti } from './AsyncMulti';
export { default as MenuBuffer } from './MenuBuffer';
export { default as MenuPortal } from './MenuPortal';
export { default as Theme } from './Theme';
10 changes: 10 additions & 0 deletions docs/pages/advanced/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
CustomIsOptionDisabled,
Experimental,
Popout,
MenuBuffer,
MenuPortal,
} from '../../examples';

Expand Down Expand Up @@ -101,6 +102,15 @@ export default function Advanced() {
</ExampleWrapper>
)}

${(
<ExampleWrapper
label="Using the style API to replace `menuBuffer`"
raw={require('!!raw-loader!../../examples/MenuBuffer.js')}
>
<MenuBuffer />
</ExampleWrapper>
)}

## Methods

These two methods sit as callable methods on the component. They are designed
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/upgradeGuide/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const propChangeData = [
['labelKey', 'removed'],
['matchPos', 'removed', md`see \`createFilter()\``],
['matchProp', 'removed', md`see \`createFilter()\``],
['menuBuffer', 'components'],
['menuBuffer', 'styles'],
['menuContainerStyle', 'styles'],
['menuRenderer', 'components'],
['menuStyle', 'styles'],
Expand Down
78 changes: 43 additions & 35 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { Component, type ElementRef, type Node } from 'react';

import memoizeOne from 'memoize-one';
import { MenuPlacer } from './components/Menu';
import isEqual from './internal/react-fast-compare';

import { createFilter } from './filters';
Expand Down Expand Up @@ -899,7 +900,10 @@ export default class Select extends Component<Props, State> {
};
onScroll = (event: Event) => {
if (typeof this.props.closeMenuOnScroll === 'boolean') {
if (event.target instanceof HTMLElement && isDocumentElement(event.target)) {
if (
event.target instanceof HTMLElement &&
isDocumentElement(event.target)
) {
this.props.onMenuClose();
}
} else if (typeof this.props.closeMenuOnScroll === 'function') {
Expand Down Expand Up @@ -1562,9 +1566,7 @@ export default class Select extends Component<Props, State> {
// for performance, the menu options in state aren't changed when the
// focused option changes so we calculate additional props based on that
const isFocused = focusedOption === props.data;
props.innerRef = isFocused
? this.getFocusedOptionRef
: undefined;
props.innerRef = isFocused ? this.getFocusedOptionRef : undefined;

return (
<Option {...commonProps} {...props} isFocused={isFocused}>
Expand Down Expand Up @@ -1609,38 +1611,44 @@ export default class Select extends Component<Props, State> {
}

const menuElement = (
<div>
<Menu
{...commonProps}
innerProps={{
onMouseDown: this.onMenuMouseDown,
onMouseMove: this.onMenuMouseMove,
}}
isLoading={isLoading}
minMenuHeight={minMenuHeight}
maxMenuHeight={maxMenuHeight}
menuPlacement={menuPlacement}
menuPosition={menuPosition}
menuShouldScrollIntoView={menuShouldScrollIntoView}
>
<ScrollCaptor
isEnabled={captureMenuScroll}
onTopArrive={onMenuScrollToTop}
onBottomArrive={onMenuScrollToBottom}
<MenuPlacer
{...commonProps}
minMenuHeight={minMenuHeight}
maxMenuHeight={maxMenuHeight}
menuPlacement={menuPlacement}
menuPosition={menuPosition}
menuShouldScrollIntoView={menuShouldScrollIntoView}
>
{({ ref, placerProps: { placement, maxHeight } }) => (
<Menu
{...commonProps}
innerProps={{
ref,
onMouseDown: this.onMenuMouseDown,
onMouseMove: this.onMenuMouseMove,
}}
isLoading={isLoading}
placement={placement}
>
<ScrollBlock isEnabled={menuShouldBlockScroll}>
<MenuList
{...commonProps}
innerRef={this.getMenuListRef}
isLoading={isLoading}
maxHeight={maxMenuHeight}
>
{menuUI}
</MenuList>
</ScrollBlock>
</ScrollCaptor>
</Menu>
</div>
<ScrollCaptor
isEnabled={captureMenuScroll}
onTopArrive={onMenuScrollToTop}
onBottomArrive={onMenuScrollToBottom}
>
<ScrollBlock isEnabled={menuShouldBlockScroll}>
<MenuList
{...commonProps}
innerRef={this.getMenuListRef}
isLoading={isLoading}
maxHeight={maxHeight}
>
{menuUI}
</MenuList>
</ScrollBlock>
</ScrollCaptor>
</Menu>
)}
</MenuPlacer>
);

// positioning behaviour is almost identical for portalled and fixed,
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ cases(
cases(
'menuIsOpen prop',
({ props = BASIC_PROPS }) => {
let selectWrapper = shallow(<Select {...props} />);
let selectWrapper = mount(<Select {...props} />);
expect(selectWrapper.find(Menu).exists()).toBeFalsy();

selectWrapper.setProps({ menuIsOpen: true });
Expand All @@ -198,7 +198,7 @@ cases(
cases(
'filterOption() prop - should filter only if function returns truthy for value',
({ props, searchString, expectResultsLength }) => {
let selectWrapper = shallow(<Select {...props} />);
let selectWrapper = mount(<Select {...props} />);
selectWrapper.setProps({ inputValue: searchString });
expect(selectWrapper.find(Option).length).toBe(expectResultsLength);
},
Expand Down Expand Up @@ -230,7 +230,7 @@ cases(
cases(
'filterOption prop is null',
({ props, searchString, expectResultsLength }) => {
let selectWrapper = shallow(<Select {...props} />);
let selectWrapper = mount(<Select {...props} />);
selectWrapper.setProps({ inputValue: searchString });
expect(selectWrapper.find(Option).length).toBe(expectResultsLength);
},
Expand Down Expand Up @@ -262,7 +262,7 @@ cases(
cases(
'no option found on search based on filterOption prop',
({ props, searchString }) => {
let selectWrapper = shallow(<Select {...props} />);
let selectWrapper = mount(<Select {...props} />);
selectWrapper.setProps({ inputValue: searchString });
expect(selectWrapper.find(NoOptionsMessage).exists()).toBeTruthy();
},
Expand All @@ -289,7 +289,7 @@ cases(
cases(
'noOptionsMessage() function prop',
({ props, expectNoOptionsMessage, searchString }) => {
let selectWrapper = shallow(<Select {...props} />);
let selectWrapper = mount(<Select {...props} />);
selectWrapper.setProps({ inputValue: searchString });
expect(selectWrapper.find(NoOptionsMessage).props().children).toBe(
expectNoOptionsMessage
Expand Down
Loading