-
Notifications
You must be signed in to change notification settings - Fork 392
WIP: Autocomplete component #190
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
Closed
danseethaler
wants to merge
10
commits into
patternfly:master
from
danseethaler:autocomplete-component
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
daea6f2
feat(AutoComplete): Add AutoComplete component to lib
danseethaler 49f0eeb
Add support for headers, dividers, and disabled items
danseethaler 2e42fa0
Add support for tab and enter keys
danseethaler 9470522
Isolate Items to seperate file
danseethaler 330d5a1
Extract key_codes
danseethaler 88bba81
Adding tests
danseethaler 2074363
Recommended updates
danseethaler 3118d86
Adding storybook info
danseethaler 6309069
Test patch
danseethaler d077833
Abstracting styles
danseethaler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .autocomplete-dropdown { | ||
| display: block; | ||
| position: absolute; | ||
| left: inherit; | ||
| margin-top: 0; | ||
| top: auto; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /** | ||
| Patternfly React Specific Extensions | ||
| */ | ||
| @import 'autocomplete'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .autocomplete-dropdown { | ||
| display: block; | ||
| position: absolute; | ||
| left: inherit; | ||
| margin-top: 0; | ||
| top: auto; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /** | ||
| Patternfly React Partials | ||
| */ | ||
| @import 'autocomplete'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| const colors = [ | ||
| 'Red', | ||
| 'Orange', | ||
| 'Yellow', | ||
| 'Green', | ||
| 'Cyan', | ||
| 'Blue', | ||
| 'Indigo', | ||
| 'Violet' | ||
| ]; | ||
|
|
||
| const adjectives = [ | ||
| 'agreeable', | ||
| 'ambitious', | ||
| 'calm', | ||
| 'delightful', | ||
| 'interesting', | ||
| 'pleasant', | ||
| 'simple', | ||
| 'wonderful' | ||
| ]; | ||
|
|
||
| const autoCompleteTerms = colors | ||
| .map(color => adjectives.map(adj => `${color} is ${adj}`)) | ||
| .reduce((terms, colorItems) => terms.concat(colorItems)); | ||
|
|
||
| const limitWordCount = (searchTerm, i) => { | ||
| const wordCount = searchTerm.split(' ').length; | ||
| return i | ||
| .split(' ') | ||
| .slice(0, wordCount) | ||
| .join(' '); | ||
| }; | ||
|
|
||
| const removeDuplicates = (item, index, array) => array.indexOf(item) === index; | ||
|
|
||
| const includesTerm = (searchTerm, i) => | ||
| !searchTerm || i.toLowerCase().includes(searchTerm.toLowerCase()); | ||
|
|
||
| const limitResults = (i, index) => index < 10; | ||
|
|
||
| const stringsToConfig = item => ({ | ||
| text: item, | ||
| disabled: item.includes('Red') | ||
| }); | ||
|
|
||
| export const getSearchItems = (searchTerm = '') => { | ||
| const items = autoCompleteTerms | ||
| .map(limitWordCount.bind(null, searchTerm)) | ||
| .filter(removeDuplicates) | ||
| .filter(includesTerm.bind(null, searchTerm)) | ||
| .filter(limitResults) | ||
| .map(stringsToConfig); | ||
|
|
||
| if (items.length > 2) { | ||
| items.splice( | ||
| 2, | ||
| 0, | ||
| { type: 'header', text: 'This is a Header' }, | ||
| { type: 'divider' } | ||
| ); | ||
| } | ||
|
|
||
| return items; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import React, { Component } from 'react'; | ||
| import Downshift from 'downshift'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| import { InputGroup, Button } from '../../index'; | ||
| import AutoCompleteInput from './AutoCompleteInput'; | ||
| import AutoCompleteItems from './AutoCompleteItems'; | ||
| import { KEY_CODES } from '../../common/helpers'; | ||
| import { getActiveItems } from './helpers'; | ||
|
|
||
| class AutoComplete extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| inputValue: '' | ||
| }; | ||
| } | ||
|
|
||
| handleStateChange = ({ inputValue, ...rest }) => { | ||
| if (typeof inputValue === 'string') { | ||
| this.props.onInputUpdate(inputValue); | ||
| this.setState({ inputValue }); | ||
| } | ||
| }; | ||
|
|
||
| render() { | ||
| const { | ||
| onSearch, | ||
| labelText, | ||
| onInputUpdate, | ||
| items, | ||
| actionText, | ||
| ...rest | ||
| } = this.props; | ||
|
|
||
| const activeItems = getActiveItems(items); | ||
|
|
||
| return ( | ||
| <Downshift | ||
| onStateChange={this.handleStateChange} | ||
| defaultHighlightedIndex={0} | ||
| selectedItem={this.state.inputValue} | ||
| {...rest} | ||
| render={({ | ||
| getInputProps, | ||
| getItemProps, | ||
| getLabelProps, | ||
| isOpen, | ||
| inputValue, | ||
| highlightedIndex, | ||
| getRootProps, | ||
| selectedItem, | ||
| selectItem | ||
| }) => { | ||
| const shouldShowItems = isOpen && items.length > 0; | ||
| const autoCompleteItemsProps = { | ||
| items, | ||
| highlightedIndex, | ||
| selectedItem, | ||
| getItemProps, | ||
| activeItems | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| {labelText && <label {...getLabelProps()}>{labelText}</label>} | ||
| <InputGroup> | ||
| <AutoCompleteInput | ||
| onKeyPress={e => { | ||
| switch (e.keyCode) { | ||
| case KEY_CODES.TAB_KEY: | ||
| if (isOpen && activeItems[highlightedIndex]) { | ||
| selectItem(activeItems[highlightedIndex]); | ||
| e.preventDefault(); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case KEY_CODES.ENTER_KEY: | ||
| if (!isOpen || !activeItems[highlightedIndex]) { | ||
| onSearch(this.state.inputValue); | ||
| e.preventDefault(); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
| }} | ||
| passedProps={getInputProps()} | ||
| /> | ||
| <InputGroup.Button> | ||
| <Button onClick={() => onSearch(inputValue)}> | ||
| {actionText} | ||
| </Button> | ||
| </InputGroup.Button> | ||
| </InputGroup> | ||
|
|
||
| {shouldShowItems && ( | ||
| <AutoCompleteItems {...autoCompleteItemsProps} /> | ||
| )} | ||
| </div> | ||
| ); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| AutoComplete.propTypes = { | ||
| items: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| /* text to display in MenuItem */ | ||
| text: PropTypes.string, | ||
| /* item can be a header or divider or undefined for regular item */ | ||
| type: PropTypes.oneOf(['header', 'divider']), | ||
| /* optionally disable a regular item */ | ||
| disabled: PropTypes.bool | ||
| }) | ||
| ).isRequired, | ||
| onInputUpdate: PropTypes.func.isRequired, | ||
| onSearch: PropTypes.func.isRequired, | ||
| labelText: PropTypes.string, | ||
| actionText: PropTypes.string | ||
| }; | ||
|
|
||
| AutoComplete.defaultProps = { | ||
| labelText: '', | ||
| actionText: 'Search' | ||
| }; | ||
|
|
||
| export default AutoComplete; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import React from 'react'; | ||
| import { storiesOf } from '@storybook/react'; | ||
| import { withInfo } from '@storybook/addon-info'; | ||
| import { inlineTemplate } from '../../../storybook/decorators/storyTemplates'; | ||
|
|
||
| import AutoComplete from './AutoComplete'; | ||
| import { | ||
| MockAutoComplete, | ||
| basicExampleSource | ||
| } from './__mocks__/mockAutoComplete'; | ||
|
|
||
| storiesOf('AutoComplete', module).add( | ||
| 'AutoComplete', | ||
| withInfo({ | ||
| source: false, | ||
| propTables: [AutoComplete], | ||
| propTablesExclude: [MockAutoComplete], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @priley86 This approach can fix some problems we are facing with the storybook? @gilad215 /cc
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 yes - that is the suggestion right now... nice job. |
||
| text: ( | ||
| <div> | ||
| <h1>Story Source</h1> | ||
| <pre>{basicExampleSource}</pre> | ||
| </div> | ||
| ) | ||
| })(() => { | ||
| let story = <MockAutoComplete />; | ||
| return inlineTemplate({ | ||
| title: 'AutoComplete Example', | ||
| story | ||
| }); | ||
| }) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import React from 'react'; | ||
| import renderer from 'react-test-renderer'; | ||
|
|
||
| import AutoComplete from './AutoComplete'; | ||
| import { getActiveItems } from './helpers'; | ||
| import AutoCompleteItems from './AutoCompleteItems'; | ||
| import { getSearchItems } from './AutoComplete.fixtures'; | ||
|
|
||
| describe('Autocomplete component', () => { | ||
| it('should render input but not items on initial render', () => { | ||
| const component = renderer.create( | ||
| <AutoComplete | ||
| items={getSearchItems()} | ||
| onInputUpdate={() => {}} | ||
| onSearch={() => {}} | ||
| /> | ||
| ); | ||
|
|
||
| const tree = component.toJSON(); | ||
| expect(tree).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('renderItems should render the items', () => { | ||
| const items = getSearchItems(); | ||
| const activeItems = getActiveItems(items); | ||
|
|
||
| const component = renderer.create( | ||
| <AutoCompleteItems | ||
| {...{ | ||
| items, | ||
| activeItems, | ||
| highlightedIndex: items.indexOf(activeItems[0]), | ||
| selectedItem: '', | ||
| getItemProps: () => {} | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| const tree = component.toJSON(); | ||
| expect(tree).toMatchSnapshot(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import React, { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { FormControl } from '../../index'; | ||
|
|
||
| class AutoCompleteInput extends Component { | ||
| componentDidMount() { | ||
| this.ref && this.ref.addEventListener('keydown', this.handleKeyPress); | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.ref && this.ref.removeEventListener('keydown', this.handleKeyPress); | ||
| } | ||
|
|
||
| handleKeyPress = e => { | ||
| this.props.onKeyPress(e); | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <FormControl | ||
| inputRef={ref => { | ||
| this.ref = ref; | ||
| }} | ||
| type="text" | ||
| {...this.props.passedProps} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| AutoCompleteInput.propTypes = { | ||
| passedProps: PropTypes.object.isRequired, | ||
| onKeyPress: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| export default AutoCompleteInput; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this function to be apart of the class like you did with
handleStateChange?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we don't have the render prop props (say that three times fast :) in a class function so we'd have to do some pass-throughs which I'm not a fan of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I understand your point now, it will actually be really messy...