Last active
October 15, 2019 13:57
-
-
Save schnerd/30c1415b7621d0e71352aa0c0184f175 to your computer and use it in GitHub Desktop.
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
// Autocomplete.js | |
import React from 'react'; | |
import * as defaultComponents from './styled-elements'; | |
class Autocomplete extends React.Component { | |
// Setup & handlers omitted to keep this example short | |
getSharedStyleProps() { | |
const {isOpen, isError} = this.state; | |
return { | |
$isOpen: isOpen | |
$isError: isError | |
}; | |
} | |
render() { | |
const {isOpen, query, value} = this.state; | |
const {options, overrides} = this.props; | |
const { | |
Root: {component: Root, props: rootProps}, | |
Input: {component: Input, props: inputProps}, | |
List: {component: List, props: listProps}, | |
Option: {component: Option, props: optionProps}, | |
} = getComponents(defaultComponents, overrides); | |
const sharedProps = this.getSharedStyleProps(); | |
return ( | |
<Root {...sharedProps} {...rootProps}> | |
<Input | |
type="text" | |
value={query} | |
onChange={this.onInputChange} | |
{...sharedProps} | |
{...inputProps} | |
/> | |
{isOpen && ( | |
<List {...sharedProps} {...listProps}> | |
{options.map(option => { | |
<Option | |
onClick={this.onOptionClick.bind(this, option)} | |
$option={option} | |
{...sharedProps} | |
{...optionProps} | |
> | |
{option.label} | |
</Option> | |
})} | |
</List> | |
)} | |
</Root> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment