Skip to content

Instantly share code, notes, and snippets.

@foo4foo
Last active March 9, 2019 13:12
Show Gist options
  • Save foo4foo/0fd723472a96e8260a9753fc7197a707 to your computer and use it in GitHub Desktop.
Save foo4foo/0fd723472a96e8260a9753fc7197a707 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import Typography from '@material-ui/core/Typography'
import Checkbox from '@material-ui/core/Checkbox'
import AsyncSelect from 'react-select/lib/Async'
import { components } from 'react-select'
import { colourOptions } from './data'
import './App.css'
const filterColors = (inputValue: string) => {
return colourOptions.filter((i) =>
i.label.toLowerCase().includes(inputValue.toLowerCase())
)
}
const promiseOptions = (inputValue) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(filterColors(inputValue))
}, 1000)
})
const selectComponents = {
Option: (innerProps) => (
<components.Option {...innerProps}>
<div
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between'
}}
>
<Checkbox checked={innerProps.isSelected} />
<Typography>{innerProps.value}</Typography>
</div>
</components.Option>
),
MultiValue: (innerProps) => null
}
class App extends Component {
state = {
selectedOptions: []
}
handleChange = (selectedOptions) => {
this.setState((prevState) => ({
selectedOptions
}))
}
render() {
const { selectedOptions } = this.state
const ValueContainer = (props) => (
<components.ValueContainer {...props}>
<p
style={{
overflow: 'hidden',
width: '10%',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
textAlign: 'left'
}}
>
{selectedOptions.map((option) => option.label).join(', ')}
</p>
</components.ValueContainer>
)
return (
<div className="App">
<AsyncSelect
placeholder="Select Color"
components={{
...selectComponents,
ValueContainer:
selectedOptions.length > 0
? ValueContainer
: components.ValueContainer
}}
isMulti
cacheOptions
closeMenuOnSelect={false}
hideSelectedOptions={false}
defaultOptions={colourOptions}
loadOptions={promiseOptions}
onChange={this.handleChange}
/>
</div>
)
}
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment