Skip to content

Commit

Permalink
fix(Dropdown): resize search input on change
Browse files Browse the repository at this point in the history
  • Loading branch information
levithomason committed Aug 4, 2016
1 parent 2aff2f4 commit df75f73
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash'
import cx from 'classnames'
import React, { cloneElement, PropTypes } from 'react'
import { findDOMNode } from 'react-dom'

import META from '../../utils/Meta'
import { customPropTypes, getUnhandledProps, useKeyOnly, useKeyOrValueAndKey } from '../../utils/propUtils'
Expand Down Expand Up @@ -460,9 +461,19 @@ export default class Dropdown extends Component {
// open search dropdown on search query
if (search && newQuery && !open) this.open()

// resize the search input, temporarily show the sizer so we can measure it
let searchWidth
if (newQuery) {
const sizer = findDOMNode(this.refs.sizer)
sizer.style.display = 'inline'
searchWidth = Math.ceil(sizer.getBoundingClientRect().width)
sizer.style.removeProperty('display')
}

this.setState({
selectedIndex: 0,
searchQuery: newQuery,
searchWidth,
})
}

Expand Down Expand Up @@ -667,7 +678,7 @@ export default class Dropdown extends Component {

renderSearchInput = () => {
const { search } = this.props
const { searchQuery } = this.state
const { searchQuery, searchWidth } = this.state

return !search ? null : (
<input
Expand All @@ -678,11 +689,19 @@ export default class Dropdown extends Component {
className='search'
autoComplete='off'
tabIndex='0'
style={{ width: searchWidth }}
ref='search'
/>
)
}

renderSearchSizer = () => {
const { search } = this.props
const { searchQuery } = this.state

return !search ? null : <span className='sizer' ref='sizer'>{searchQuery}</span>
}

renderLabels = () => {
debug('renderLabels()')
const { multiple } = this.props
Expand Down Expand Up @@ -822,6 +841,7 @@ export default class Dropdown extends Component {
{this.renderHiddenInput()}
{this.renderLabels()}
{this.renderSearchInput()}
{this.renderSearchSizer()}
{this.renderText()}
<Icon name={'dropdown'} />
{this.renderMenu()}
Expand Down

0 comments on commit df75f73

Please sign in to comment.