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): fix minCharacter prop behaviour #1722

Merged
merged 3 commits into from
Jun 22, 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/* eslint-disable max-len */

import React from 'react'
import { Container } from 'semantic-ui-react'
import { Dropdown } from 'semantic-ui-react'

const countryOptions = [
{ key: 'af', value: 'af', flag: 'af', text: 'Afghanistan' },
{ key: 'ae', value: 'ae', flag: 'ae', text: 'United Arab Emirates' },
{ key: 'us', value: 'us', flag: 'us', text: 'United States' },
]

const ContainerExampleContainer = () => (
<Container>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa strong. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede link mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi.</p>
</Container>
const DropdownExampleSearchSelection = () => (
Copy link
Member

Choose a reason for hiding this comment

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

Whoops, this looks like the wrong content for this file.

Copy link
Member

Choose a reason for hiding this comment

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

Whoops, looks like I missed the big fat WIP 😊

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, I will revert this before merge 😄

<div>
<Dropdown placeholder='Min 1' fluid search selection options={countryOptions} />
<br />
<Dropdown placeholder='Min 3' fluid search selection minCharacters={3} options={countryOptions} />
</div>
)

export default ContainerExampleContainer
export default DropdownExampleSearchSelection
82 changes: 47 additions & 35 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,19 @@ export default class Dropdown extends Component {
static defaultProps = {
additionLabel: 'Add ',
additionPosition: 'top',
closeOnBlur: true,
icon: 'dropdown',
minCharacters: 1,
noResultsMessage: 'No results found.',
openOnFocus: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Only prop sort there.

renderLabel: ({ text }) => text,
selectOnBlur: true,
openOnFocus: true,
closeOnBlur: true,
minCharacters: 1,
}

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

static _meta = {
Expand Down Expand Up @@ -418,9 +418,11 @@ export default class Dropdown extends Component {
if (!prevState.focus && this.state.focus) {
debug('dropdown focused')
if (!this.isMouseDown) {
const { openOnFocus } = this.props
const { minCharacters, openOnFocus, search } = this.props
const openable = !search || (search && minCharacters === 1)

debug('mouse is not down, opening')
if (openOnFocus) this.open()
if (openOnFocus && openable) this.open()
Copy link
Member Author

Choose a reason for hiding this comment

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

Defines behaviour with focus.

}
if (!this.state.open) {
document.addEventListener('keydown', this.openOnArrow)
Expand Down Expand Up @@ -578,11 +580,13 @@ export default class Dropdown extends Component {
}

selectItemOnEnter = (e) => {
debug('selectItemOnEnter()')
debug(keyboardKey.getName(e))
debug('selectItemOnEnter()', keyboardKey.getName(e))
const { search } = this.props

if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
e.preventDefault()

if (search && _.isEmpty(this.getMenuOptions())) return
Copy link
Member Author

Choose a reason for hiding this comment

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

Disables Enter key when nothing find.

this.makeSelectedItemActive(e)
this.closeOnChange(e)
}
Expand Down Expand Up @@ -622,29 +626,34 @@ export default class Dropdown extends Component {

handleMouseDown = (e) => {
debug('handleMouseDown()')
const { onMouseDown } = this.props
if (onMouseDown) onMouseDown(e, this.props)

this.isMouseDown = true
_.invoke(this.props, 'onMouseDown', e, this.props)
// Do not access document when server side rendering
if (!isBrowser) return
document.addEventListener('mouseup', this.handleDocumentMouseUp)
if (isBrowser) document.addEventListener('mouseup', this.handleDocumentMouseUp)
Copy link
Member Author

Choose a reason for hiding this comment

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

Minor refactor.

}

handleDocumentMouseUp = () => {
debug('handleDocumentMouseUp()')

this.isMouseDown = false
// Do not access document when server side rendering
if (!isBrowser) return
document.removeEventListener('mouseup', this.handleDocumentMouseUp)
if (isBrowser) document.removeEventListener('mouseup', this.handleDocumentMouseUp)
Copy link
Member Author

Choose a reason for hiding this comment

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

Minor refactor.

}

handleClick = (e) => {
handleClick = e => {
debug('handleClick()', e)
const { onClick } = this.props
if (onClick) onClick(e, this.props)

const { minCharacters, search } = this.props
const { open, searchQuery } = this.state

_.invoke(this.props, 'onClick', e, this.props)
// prevent closeOnDocumentClick()
e.stopPropagation()
this.toggle(e)

if (!search) return this.toggle(e)
if (open) return
if (searchQuery.length >= minCharacters || minCharacters === 1) this.open(e)
Copy link
Member Author

Choose a reason for hiding this comment

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

Update behaviour with click on search input.

}

handleItemClick = (e, item) => {
Expand Down Expand Up @@ -681,14 +690,15 @@ export default class Dropdown extends Component {

handleFocus = (e) => {
debug('handleFocus()')
const { onFocus } = this.props
const { focus } = this.state

if (focus) return
if (onFocus) onFocus(e, this.props)

_.invoke(this.props, 'onFocus', e, this.props)
this.setState({ focus: true })
}

handleBlur = (e) => {
handleBlur = e => {
debug('handleBlur()')

// Heads up! Don't remove this.
Expand All @@ -707,26 +717,28 @@ export default class Dropdown extends Component {
this.setState({ focus: false, searchQuery: '' })
}

handleSearchChange = (e) => {
debug('handleSearchChange()')
debug(e.target.value)
handleSearchChange = e => {
debug('handleSearchChange()', e)
// prevent propagating to this.props.onChange()
e.stopPropagation()
const { search, onSearchChange, minCharacters } = this.props
const { open } = this.state
const newQuery = e.target.value

if (onSearchChange) onSearchChange(e, newQuery)
const { minCharacters } = this.props
const { open } = this.state
const newQuery = _.get(e, 'target.value', '')

if (newQuery.length >= minCharacters) {
// open search dropdown on search query
if (search && newQuery && !open) this.open()
_.invoke(this.props, 'onSearchChange', e, newQuery)
this.setState({
selectedIndex: 0,
searchQuery: newQuery,
})

this.setState({
selectedIndex: 0,
searchQuery: newQuery,
})
// open search dropdown on search query
if (!open && newQuery.length >= minCharacters) {
this.open()
return
}
// close search dropdown if search query is too small
if (open && minCharacters !== 1 && newQuery.length < minCharacters) this.close()
}

// ----------------------------------------
Expand Down