Skip to content

Instantly share code, notes, and snippets.

@fufufukakaka
Created July 18, 2019 12:22
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 fufufukakaka/a9c68f6229c84f24d2adf43e8feb97b7 to your computer and use it in GitHub Desktop.
Save fufufukakaka/a9c68f6229c84f24d2adf43e8feb97b7 to your computer and use it in GitHub Desktop.
import Select from 'react-select'
import { List } from 'react-virtualized'
import React, { useState, Component } from 'react'
import { filter } from 'lodash/fp'
const MenuList = props => {
const rows = props.children
const rowRenderer = ({ key, index, isScrolling, isVisible, style }) => (
<div key={key} style={style}>
{rows[index]}
</div>
)
return (
<List
style={{ width: '100%' }}
width={500}
height={300}
rowHeight={40}
rowCount={rows.length}
rowRenderer={rowRenderer}
/>
)
}
const VirtualizedSelect = ({ allOpts, maxDisplayed, onChange }) => {
const [inputValue, setInputValue] = useState('')
const [displayedOpts, setDisplayedOpts] = useState(
allOpts.slice(0, maxDisplayed)
)
const handleInputChange = inputValue => {
if (inputValue === '') {
return
}
const displayedOpts = filter(
({ label }) => label.toLowerCase().includes(inputValue.toLowerCase()),
allOpts
).slice(0, maxDisplayed)
setInputValue(inputValue)
setDisplayedOpts(displayedOpts)
}
return (
<div style={{ width: '500px' }}>
<Select
components={{ MenuList }}
options={displayedOpts}
onInputChange={handleInputChange}
onChange={onChange}
/>
</div>
)
}
export default VirtualizedSelect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment