Skip to content

Commit

Permalink
FIX (dropdownPosition): support dropdown position 'auto', 'top' and '…
Browse files Browse the repository at this point in the history
…bottom'
  • Loading branch information
sanusart committed Jan 26, 2019
1 parent 5f87e82 commit 9fc4980
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 126 deletions.
101 changes: 51 additions & 50 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/src/examples/OpenOnTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const OpenOnTop = ({ options, title }) => (
</Title>
<Select
multi
openOnTop
dropdownPosition="top"
options={options}
values={[]}
onChange={(value) =>
Expand Down
98 changes: 49 additions & 49 deletions docs/src/pages/api.md

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions docs/src/pages/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Demo extends React.Component {
color: '#0074D9',
keepSelectedInList: true,
closeOnSelect: false,
openOnTop: false,
dropdownPosition: 'auto',
dropdownHeight: '300px'
};
}
Expand Down Expand Up @@ -168,7 +168,7 @@ export class Demo extends React.Component {
noDataLabel="No matches found"
closeOnSelect={this.state.closeOnSelect}
noDataRenderer={this.state.noDataRenderer ? () => this.noDataRenderer() : undefined}
openOnTop={this.state.openOnTop}
dropdownPosition={this.state.dropdownPosition}
itemRenderer={
this.state.itemRenderer
? (item, itemIndex, props, state, methods) =>
Expand Down Expand Up @@ -224,16 +224,14 @@ export class Demo extends React.Component {
/>{' '}
Disabled
<br />
<input
type="checkbox"
checked={this.state.openOnTop}
onChange={() =>
this.setState({
openOnTop: !this.state.openOnTop
})
}
/>{' '}
Open on top of select
Dropdown position
<StyledHtmlSelect
defaultValue={this.state.dropdownPosition}
onChange={(event) => this.setState({ dropdownPosition: event.target.value })}>
<option value="auto">auto</option>
<option value="top">top</option>
<option value="bottom">bottom</option>
</StyledHtmlSelect>
<br />
<input
type="checkbox"
Expand Down Expand Up @@ -369,18 +367,18 @@ export class Demo extends React.Component {
Custom color
<br />
Search by field:{' '}
<select
<StyledHtmlSelect
defaultValue={this.state.searchBy}
onChange={(event) => this.setState({ searchBy: event.target.value })}>
{Object.keys(options[0]).map((f) => (
<option key={f} value={f}>
{f}
</option>
))}
</select>
</StyledHtmlSelect>
<br />
Label field:{' '}
<select
<StyledHtmlSelect
defaultValue={this.state.labelField}
onChange={(event) =>
this.setState({
Expand All @@ -393,18 +391,18 @@ export class Demo extends React.Component {
{f}
</option>
))}
</select>
</StyledHtmlSelect>
<br />
Value field:{' '}
<select
<StyledHtmlSelect
defaultValue={this.state.valueField}
onChange={(event) => this.setState({ valueField: event.target.value })}>
{Object.keys(options[0]).map((f) => (
<option key={f} value={f}>
{f}
</option>
))}
</select>
</StyledHtmlSelect>
</p>

<details>
Expand Down Expand Up @@ -512,4 +510,13 @@ const Button = styled.button`
}
`;

const StyledHtmlSelect = styled.select`
padding: 0;
margin: 0 0 0 10px;
height: 23px !important;
color: #0071dc;
background: #fff;
border: 1px solid #0071dc;
`;

export default Demo;
6 changes: 3 additions & 3 deletions docs/src/pages/demos/open-on-top.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
path: '/prop/open-on-top'
path: '/prop/dropdown-position'
date: '2017-11-07'
title: 'openOnTop'
title: 'dropdownPosition'
demo: 8z0mw4z4j0
---

Expand All @@ -12,7 +12,7 @@ Open dropdown above the select
```
<Select
multi
openOnTop
dropdownPosition="top"
options={options}
onChange={(values) => this.onChange(values)}
/>
Expand Down
27 changes: 23 additions & 4 deletions src/components/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@ import Item from '../components/Item';

import { valueExistInSelected, hexToRGBA } from '../util';

const dropdownPosition = (props, methods) => {
const DropdownBoundingClientRect = methods.getSelectRef().getBoundingClientRect();
const dropdownHeight =
DropdownBoundingClientRect.bottom + parseInt(props.dropdownHeight) + props.dropdownGap;

if (props.dropdownPosition !== 'auto') {
return props.dropdownPosition;
}

if (dropdownHeight > window.innerHeight && dropdownHeight > DropdownBoundingClientRect.top) {
return 'top';
}

return 'bottom';
};

const Dropdown = ({ props, state, methods }) => (
<DropDown
tabIndex="-1"
aria-expanded="true"
role="list"
openOnTop={props.openOnTop}
dropdownPosition={dropdownPosition(props, methods)}
selectBounds={state.selectBounds}
portal={props.portal}
dropdownGap={props.dropdownGap}
dropdownHeight={props.dropdownHeight}
className="react-dropdown-select-dropdown">
className={`react-dropdown-select-dropdown react-dropdown-select-dropdown-position-${dropdownPosition(
props,
methods
)}`}>
{props.dropdownRenderer ? (
props.dropdownRenderer(props, state, methods)
) : (
Expand Down Expand Up @@ -54,8 +73,8 @@ const Dropdown = ({ props, state, methods }) => (

const DropDown = styled.div`
position: absolute;
${({ selectBounds, dropdownGap, openOnTop }) =>
openOnTop
${({ selectBounds, dropdownGap, dropdownPosition }) =>
dropdownPosition === 'top'
? `bottom: ${selectBounds.height + 2 + dropdownGap}px`
: `top: ${selectBounds.height + 2 + dropdownGap}px`};
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ Select.defaultProps = {
color: '#0074D9',
keepSelectedInList: true,
closeOnSelect: false,
openOnTop: false,
dropdownPosition: 'auto',
dropdownHeight: '300px',
autoFocus: true,
portal: null,
Expand Down

0 comments on commit 9fc4980

Please sign in to comment.