From 3124e5c3868e9ecea7c7108d54c896f837fe828f Mon Sep 17 00:00:00 2001 From: Alexander Fedyashov Date: Wed, 31 May 2017 19:14:12 +0300 Subject: [PATCH] wip --- .../Types/ContainerExampleContainer.js | 22 +++++--- src/modules/Dropdown/Dropdown.js | 53 +++++++++++-------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/docs/app/Examples/elements/Container/Types/ContainerExampleContainer.js b/docs/app/Examples/elements/Container/Types/ContainerExampleContainer.js index 3f45a31c48..e5a90e5e69 100644 --- a/docs/app/Examples/elements/Container/Types/ContainerExampleContainer.js +++ b/docs/app/Examples/elements/Container/Types/ContainerExampleContainer.js @@ -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 = () => ( - -

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.

-
+const DropdownExampleSearchSelection = () => ( +
+ +
+ +
) -export default ContainerExampleContainer +export default DropdownExampleSearchSelection \ No newline at end of file diff --git a/src/modules/Dropdown/Dropdown.js b/src/modules/Dropdown/Dropdown.js index 5e4fd12e98..34704e5647 100644 --- a/src/modules/Dropdown/Dropdown.js +++ b/src/modules/Dropdown/Dropdown.js @@ -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() } if (!this.state.open) { document.addEventListener('keydown', this.openOnArrow) @@ -622,29 +624,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) } 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) } - handleClick = (e) => { + handleClick = e => { debug('handleClick()', e) - const { onClick } = this.props - if (onClick) onClick(e, this.props) + + const { minCharacters, onClick, 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) } handleItemClick = (e, item) => { @@ -681,10 +688,11 @@ 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 }) } @@ -717,16 +725,17 @@ export default class Dropdown extends Component { const newQuery = e.target.value if (onSearchChange) onSearchChange(e, newQuery) + this.setState({ + selectedIndex: 0, + searchQuery: newQuery, + }) - if (newQuery.length >= minCharacters) { - // open search dropdown on search query - if (search && newQuery && !open) this.open() - - this.setState({ - selectedIndex: 0, - searchQuery: newQuery, - }) + if(search && newQuery && open && newQuery.length < minCharacters) { + this.close() + return } + // open search dropdown on search query + if (search && newQuery && !open && newQuery.length >= minCharacters) this.open() } // ----------------------------------------