Skip to content

Commit

Permalink
feat(Dropdown): lazyLoad prop for menu items (#1918)
Browse files Browse the repository at this point in the history
* feat(Dropdown): lazy load options

* test(Dropdown): update for lazy loaded options

* feat(Dropdown): add lazyLoad prop

* test(Dropdown): restore tests

* fix(Dropdown): make lazyLoad optional
  • Loading branch information
dsirnk authored and levithomason committed Jun 26, 2018
1 parent 9af142e commit 95429e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/modules/Dropdown/Dropdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export interface DropdownProps {
/** A dropdown can be labeled. */
labeled?: boolean

/** A dropdown can defer rendering its options until it is open. */
lazyLoad?: boolean

/** A dropdown can show that it is currently loading data. */
loading?: boolean

Expand Down
11 changes: 9 additions & 2 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ export default class Dropdown extends Component {
/** A dropdown can be labeled. */
labeled: PropTypes.bool,

/** A dropdown can defer rendering its options until it is open. */
lazyLoad: PropTypes.bool,

/** A dropdown can show that it is currently loading data. */
loading: PropTypes.bool,

Expand Down Expand Up @@ -1228,8 +1231,12 @@ export default class Dropdown extends Component {
}

renderOptions = () => {
const { multiple, search, noResultsMessage } = this.props
const { selectedIndex, value } = this.state
const { lazyLoad, multiple, search, noResultsMessage } = this.props
const { open, selectedIndex, value } = this.state

// lazy load, only render options when open
if (lazyLoad && !open) return null

const options = this.getMenuOptions()

if (noResultsMessage !== null && search && _.isEmpty(options)) {
Expand Down
23 changes: 17 additions & 6 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ describe('Dropdown', () => {
.first()
.simulate('click')
.should.have.prop('active', true)

wrapper.should.have.state('value', options[0].value)

dropdownMenuIsClosed()
Expand Down Expand Up @@ -914,11 +915,7 @@ describe('Dropdown', () => {
wrapper
.find('DropdownItem')
.at(1)
.should.have.prop('selected', false)
wrapper
.find('DropdownItem')
.at(1)
.should.have.prop('active', false)
.should.have.props({ selected: false, active: false })

// select and make active
domEvent.keyDown(document, { key: 'ArrowDown' })
Expand All @@ -928,7 +925,7 @@ describe('Dropdown', () => {
wrapper
.find('DropdownItem')
.at(1)
.should.have.prop('active', true)
.should.have.props({ selected: true, active: true })
})
it('closes the menu', () => {
wrapperMount(<Dropdown options={options} selection />).simulate('click')
Expand Down Expand Up @@ -2140,6 +2137,20 @@ describe('Dropdown', () => {
})
})

describe('lazyLoad', () => {
it('does not render options when closed', () => {
wrapperShallow(<Dropdown options={options} lazyLoad />).should.not.have.descendants(
'DropdownItem',
)
})

it('renders options when open', () => {
wrapperShallow(<Dropdown options={options} lazyLoad open />).should.have.descendants(
'DropdownItem',
)
})
})

describe('Dropdown.Menu child', () => {
it('renders child passed', () => {
wrapperShallow(
Expand Down

0 comments on commit 95429e6

Please sign in to comment.