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

feat(Dropdown): add openOnFocus and closeOnBlur #1101

Merged
merged 10 commits into from
Jan 11, 2017
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { Dropdown } from 'semantic-ui-react'
import { friendOptions } from '../common'

const DropdownExampleCloseOnBlur = () => (
<div>
<Dropdown placeholder='I close on blur' closeOnBlur fluid selection options={friendOptions} />
<br />
Copy link
Member

Choose a reason for hiding this comment

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

Let's drop the fluid prop and go with {' '} here so these are inline elements rather than full page vertically stacked elements. Just helps keep the docs more concise.

<Dropdown placeholder='I stay open on blur' closeOnBlur={false} fluid selection options={friendOptions} />
</div>
)

export default DropdownExampleCloseOnBlur
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { Dropdown } from 'semantic-ui-react'
import { friendOptions } from '../common'

const DropdownExampleOpenOnFocus = () => (
<div>
<Dropdown placeholder='I stay open on focus' openOnFocus fluid selection options={friendOptions} />
Copy link
Member

Choose a reason for hiding this comment

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

-I stay open on focus
+I open on focus

<br />
<Dropdown placeholder='I close when not focussed' openOnFocus={false} fluid selection options={friendOptions} />
Copy link
Member

Choose a reason for hiding this comment

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

-I close when not focussed
+I do not open on focus

</div>
)

export default DropdownExampleOpenOnFocus
12 changes: 12 additions & 0 deletions docs/app/Examples/modules/Dropdown/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const DropdownUsageExamples = () => (
<ExampleSection title='Usage'>
<ComponentExample
title='Open On Focus'
description='A dropdown that opens when it is focussed on.'
examplePath='modules/Dropdown/Usage/DropdownExampleOpenOnFocus'
/>

<ComponentExample
title='Close On Blur'
description='A dropdown that closes when it blurs'
examplePath='modules/Dropdown/Usage/DropdownExampleCloseOnBlur'
/>

<ComponentExample
title='Uncontrolled'
description='A dropdown can behave like an uncontrolled input.'
Expand Down
14 changes: 12 additions & 2 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ export default class Dropdown extends Component {
/** The text displayed in the dropdown, usually for the active item. */
text: PropTypes.string,

/** Whether or not the menu should open when the dropdown is focused. */
openOnFocus: PropTypes.bool,
Copy link
Member

Choose a reason for hiding this comment

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

Let's add some descriptions to these:

"Whether or not the menu should open when the dropdown is focused."

And:

"Whether or not the menu should close when the dropdown is blurred."


/** Whether or not the menu should close when the dropdown is blurred. */
closeOnBlur: PropTypes.bool,

/** Custom element to trigger the menu to become visible. Takes place of 'text'. */
trigger: customPropTypes.every([
customPropTypes.disallow(['selection', 'text']),
Expand All @@ -320,6 +326,8 @@ export default class Dropdown extends Component {
noResultsMessage: 'No results found.',
renderLabel: ({ text }) => text,
selectOnBlur: true,
openOnFocus: true,
closeOnBlur: true,
}

static autoControlledProps = [
Expand Down Expand Up @@ -395,8 +403,9 @@ export default class Dropdown extends Component {
if (!prevState.focus && this.state.focus) {
debug('dropdown focused')
if (!this.isMouseDown) {
const { openOnFocus } = this.props
debug('mouse is not down, opening')
this.open()
if (openOnFocus) this.open()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this line look fine to you, logically?

Copy link
Member

Choose a reason for hiding this comment

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

👍

}
if (!this.state.open) {
document.addEventListener('keydown', this.openOnArrow)
Expand All @@ -409,8 +418,9 @@ export default class Dropdown extends Component {
} else if (prevState.focus && !this.state.focus) {
debug('dropdown blurred')
if (!this.isMouseDown) {
Copy link

Choose a reason for hiding this comment

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

Hey guys I know I'm late to the party. I wanted to discuss this line with you. So I'm doing a search Dropdown and I don't want it to open on focus. This works fine while tabbing through the form, but clicking on a form field is also focusing on it, yet it'll still open.

It makes sense that the menu opens if you click on the arrow, but I think for search it's a bit unintuitive if it opens when one clicks on the input.

Thoughts?

const { closeOnBlur } = this.props
debug('mouse is not down, closing')
this.close()
if (closeOnBlur) this.close()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this line look fine to you, logically?

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, hmm. I'm seeing something weird. When I go to the dropdown with closeOnBlur being true, and tab out of it to the adjacent one, the first one closes. It shouldn't right?

}
document.removeEventListener('keydown', this.openOnArrow)
document.removeEventListener('keydown', this.openOnSpace)
Expand Down