Skip to content

Instantly share code, notes, and snippets.

@schnerd
Last active October 15, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schnerd/30c1415b7621d0e71352aa0c0184f175 to your computer and use it in GitHub Desktop.
Save schnerd/30c1415b7621d0e71352aa0c0184f175 to your computer and use it in GitHub Desktop.
// 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