Created
July 18, 2019 12:22
-
-
Save fufufukakaka/a9c68f6229c84f24d2adf43e8feb97b7 to your computer and use it in GitHub Desktop.
This file contains 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
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