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

fix(Dropdown): do not close on click when search is empty #1490

Merged
merged 3 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,18 @@ export default class Dropdown extends Component {
this.setState({ focus: hasFocus })
}

toggle = (e) => this.state.open ? this.close(e) : this.open(e)
toggle = (e) => {
if (this.state.open) {
const { search } = this.props
const options = this.getMenuOptions()
if (search && _.isEmpty(options)) {
e.preventDefault()
} else {
return this.close(e)
}
}
return this.open(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify logic there?

toggle = (e) => {
  if (this.state.open) {
    this.open(e)
    return
  }

  const { search } = this.props
  const options = this.getMenuOptions()

  if (search && _.isEmpty(options)) {
    e.preventDefault()
    return
  }

  this.close(e)
}

It looks more readable to me.

}

// ----------------------------------------
// Render
Expand Down
11 changes: 11 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ describe('Dropdown', () => {
.should.have.been.calledOnce()
})

it('does not close on click when search is true and options are empty', () => {
wrapperMount(<Dropdown options={{}} search selection defaultOpen />)

const instance = wrapper.instance()
sandbox.spy(instance.ref, 'blur')

dropdownMenuIsOpen()
wrapper.simulate('click')
dropdownMenuIsOpen()
})

it('opens on focus', () => {
wrapperMount(<Dropdown options={options} />)

Expand Down