Skip to content

Commit

Permalink
feat(Dropdown): Add minCharacters props. Fixes Semantic-Org#1124 (Sem…
Browse files Browse the repository at this point in the history
…antic-Org#1690)

* feat(Dropdown): Add minCharacters props.

* feat(Dropdown): Add minCharacters props. Fixes Semantic-Org#1124
  • Loading branch information
dyesseyumba authored and levithomason committed May 22, 2017
1 parent c0ade9c commit 46e1bfe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 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 @@ -88,6 +88,9 @@ export interface DropdownProps {
/** A dropdown can show that it is currently loading data. */
loading?: boolean;

/** The minimum characters for a search to begin showing results. */
minCharacters?: number;

/** A selection dropdown can allow multiple selections. */
multiple?: boolean;

Expand Down
20 changes: 13 additions & 7 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export default class Dropdown extends Component {
/** A dropdown can show that it is currently loading data. */
loading: PropTypes.bool,

/** The minimum characters for a search to begin showing results. */
minCharacters: PropTypes.number,

/** A selection dropdown can allow multiple selections. */
multiple: PropTypes.bool,

Expand Down Expand Up @@ -335,6 +338,7 @@ export default class Dropdown extends Component {
selectOnBlur: true,
openOnFocus: true,
closeOnBlur: true,
minCharacters: 1,
}

static autoControlledProps = [
Expand Down Expand Up @@ -708,19 +712,21 @@ export default class Dropdown extends Component {
debug(e.target.value)
// prevent propagating to this.props.onChange()
e.stopPropagation()
const { search, onSearchChange } = this.props
const { search, onSearchChange, minCharacters } = this.props
const { open } = this.state
const newQuery = e.target.value

if (onSearchChange) onSearchChange(e, newQuery)

// open search dropdown on search query
if (search && newQuery && !open) this.open()
if (newQuery.length >= minCharacters) {
// open search dropdown on search query
if (search && newQuery && !open) this.open()

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

// ----------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,19 @@ describe('Dropdown', () => {
dropdownMenuIsOpen()
})

it("Don't open the menu on change if query's length is less than minCharacters", () => {
wrapperMount(<Dropdown options={options} selection search minCharacters={4} />)

dropdownMenuIsClosed()

// simulate search with query's length is less than minCharacters
wrapper
.find('input.search')
.simulate('change', { target: { value: faker.hacker.noun().substring(0, 1) } })

dropdownMenuIsClosed()
})

it('does not call onChange on query change', () => {
const onChangeSpy = sandbox.spy()
wrapperMount(<Dropdown options={options} selection search onChange={onChangeSpy} />)
Expand Down

0 comments on commit 46e1bfe

Please sign in to comment.