Skip to content

Commit

Permalink
Code review fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Dinculescu authored and Mihai Dinculescu committed May 22, 2018
1 parent c6e9dc5 commit 271bb01
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 63 deletions.

This file was deleted.

5 changes: 0 additions & 5 deletions docs/app/Examples/modules/Dropdown/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ const DropdownUsageExamples = () => (
/>
<ComponentExample examplePath='modules/Dropdown/Usage/DropdownExampleUpwardInline' />
<ComponentExample examplePath='modules/Dropdown/Usage/DropdownExampleUpward' />
<ComponentExample
title='Keep In Viewport'
description='A dropdown can open its menu upward when there is not enough space downward.'
examplePath='modules/Dropdown/Usage/DropdownExampleKeepInViewPortSelection'
/>
<ComponentExample
title='Wrap Selection'
description={[
Expand Down
8 changes: 4 additions & 4 deletions src/modules/Dropdown/Dropdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export interface DropdownProps {
/** Currently selected label in multi-select. */
defaultSelectedLabel?: number | string;

/** Initial value of upward. */
defaultUpward?: boolean;

/** Initial value or value array if multiple. */
defaultValue?: string | number | Array<number | string>;

Expand Down Expand Up @@ -92,9 +95,6 @@ export interface DropdownProps {
/** A dropdown can be formatted as a Menu item. */
item?: boolean;

/** A dropdown can open its menu upward when there is not enough space downward. */
keepInViewPort?: boolean;

/** A dropdown can be labeled. */
labeled?: boolean;

Expand Down Expand Up @@ -272,7 +272,7 @@ export interface DropdownProps {
/** Current value or value array if multiple. Creates a controlled component. */
value?: boolean | number | string | Array<boolean | number | string>;

/** A dropdown can open upward. */
/** Controls whether the dropdown will open upward. */
upward?: boolean;

/**
Expand Down
35 changes: 13 additions & 22 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export default class Dropdown extends Component {
PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
]),

/** Initial value of upward. */
defaultUpward: PropTypes.bool,

/** Initial value or value array if multiple. */
defaultValue: PropTypes.oneOfType([
PropTypes.number,
Expand Down Expand Up @@ -137,9 +140,6 @@ export default class Dropdown extends Component {
/** A dropdown can be formatted as a Menu item. */
item: PropTypes.bool,

/** A dropdown can open its menu upward when there is not enough space downward. */
keepInViewPort: customPropTypes.every([customPropTypes.disallow(['upward']), PropTypes.bool]),

/** A dropdown can be labeled. */
labeled: PropTypes.bool,

Expand Down Expand Up @@ -338,8 +338,8 @@ export default class Dropdown extends Component {
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.number])),
]),

/** A dropdown can open upward. */
upward: customPropTypes.every([customPropTypes.disallow(['keepInViewPort']), PropTypes.bool]),
/** Controls whether the dropdown will open upward. */
upward: PropTypes.bool,

/**
* A dropdown will go to the last element when ArrowUp is pressed on the first,
Expand All @@ -354,7 +354,6 @@ export default class Dropdown extends Component {
closeOnBlur: true,
deburr: false,
icon: 'dropdown',
keepInViewPort: false,
minCharacters: 1,
noResultsMessage: 'No results found.',
openOnFocus: true,
Expand All @@ -365,7 +364,7 @@ export default class Dropdown extends Component {
wrapSelection: true,
}

static autoControlledProps = ['open', 'searchQuery', 'selectedLabel', 'value']
static autoControlledProps = ['open', 'searchQuery', 'selectedLabel', 'value', 'upward']

static _meta = {
name: 'Dropdown',
Expand Down Expand Up @@ -476,8 +475,8 @@ export default class Dropdown extends Component {
if (!prevState.open && this.state.open) {
debug('dropdown opened')
this.attachHandlersOnOpen()
this.scrollSelectedItemIntoView()
this.setOpenDirection()
this.scrollSelectedItemIntoView()
} else if (prevState.open && !this.state.open) {
debug('dropdown closed')
this.handleClose()
Expand Down Expand Up @@ -1095,24 +1094,17 @@ export default class Dropdown extends Component {
setOpenDirection = () => {
if (!this.ref) return

const { keepInViewPort } = this.props
if (!keepInViewPort) return

const menu = this.ref.querySelector('.menu.visible')

if (!menu) return

const dropdownRect = this.ref.getBoundingClientRect()
const itemsHeight = menu.clientHeight
const menuHeight = menu.clientHeight
const spaceAtTheBottom =
document.documentElement.clientHeight - dropdownRect.y - dropdownRect.height - itemsHeight
const spaceAtTheTop = dropdownRect.y - itemsHeight
document.documentElement.clientHeight - dropdownRect.y - dropdownRect.height - menuHeight
const spaceAtTheTop = dropdownRect.y - menuHeight

if (spaceAtTheBottom < 0 && spaceAtTheTop > 0) {
this.setState({ shouldOpenUpward: true })
} else {
this.setState({ shouldOpenUpward: false })
}
this.trySetState({ upward: spaceAtTheBottom < 0 && spaceAtTheTop > spaceAtTheBottom })
}

open = (e) => {
Expand Down Expand Up @@ -1309,9 +1301,8 @@ export default class Dropdown extends Component {
scrolling,
simple,
trigger,
upward,
} = this.props
const { open, shouldOpenUpward } = this.state
const { open, upward } = this.state

// Classes
const classes = cx(
Expand All @@ -1338,7 +1329,7 @@ export default class Dropdown extends Component {
useKeyOnly(selection, 'selection'),
useKeyOnly(simple, 'simple'),
useKeyOnly(scrolling, 'scrolling'),
useKeyOnly(upward || shouldOpenUpward, 'upward'),
useKeyOnly(upward, 'upward'),

useKeyOrValueAndKey(pointing, 'pointing'),
'dropdown',
Expand Down
22 changes: 0 additions & 22 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2578,26 +2578,4 @@ describe('Dropdown', () => {
.should.have.prop('selected', true)
})
})

describe('keepInViewPort', () => {
it('is false by default', () => {
expect(Dropdown.defaultProps.keepInViewPort).to.equal(false)
})

it('will open downwards when false', () => {
wrapperMount(<Dropdown options={options} />)

wrapper.simulate('click')

wrapper.should.not.have.className('upward')
})

it('will open downwards when true and there is plenty of space to scroll', () => {
wrapperMount(<Dropdown options={options} keepInViewPort />)

wrapper.simulate('click')

wrapper.should.not.have.className('upward')
})
})
})

0 comments on commit 271bb01

Please sign in to comment.