Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 1x 13x 13x 13x 13x 13x 4x 4x 8x 4x 4x 4x 4x 13x 1x 1x 1x 1x 1x 1x 13x 5x 1x 1x 1x 1x 13x 6x 5x 5x 9x 9x 9x 5x 9x 9x 1x 13x 1x 1x 2x | import React, { useState } from 'react'; import PropTypes from "prop-types"; import Input from './Input'; const DataSuggest = ({ data, select, placeholder, noMatchesText, limit, labelKey, valueKey, ...props }) => { const [active, setActive] = useState(0); const [filteredData, setFilteredData] = useState([]); const [showResults, setShowResults] = useState(false); const [dataInput, setDataInput] = useState(''); const onChange = e => { const dataInput = e.currentTarget.value; const _filteredData = data.filter( data => data[labelKey].toLowerCase().indexOf(dataInput.toLowerCase()) > -1 ); setActive(0); setFilteredData(_filteredData); setShowResults(true); setDataInput(e.currentTarget.value); }; const onClick = e => { e.stopPropagation(); setDataInput(filteredData[e.currentTarget.dataset.key][labelKey]); select(filteredData[e.currentTarget.dataset.key]); setActive(0); setFilteredData([]); setShowResults(false); }; const onKeyDown = e => { /* istanbul ignore else */ if (e.keyCode === 13) { setDataInput(filteredData[active][labelKey]); select(filteredData[active]); setShowResults(false); setActive(0); } else if (e.keyCode === 38) { if (active === 0) return; setActive(active - 1); } else if (e.keyCode === 40) { if (active + 1 === filteredData.length) return; setActive(active + 1); } }; let dataListComponent; if (showResults && dataInput) { if (filteredData.length) { let count = 1; dataListComponent = ( <ul className="absolute inset-x-0 bg-white dark:bg-gray-700 rounded-b border border-gray-200 dark:border-gray-600 text-gray-900 text-sm font-medium dark:text-white cursor-pointer"> { filteredData.map((data, index) => { /* istanbul ignore else */ if (limit === 0 || count <= limit) { let className = "flex flex-row gap-x-2 px-4 py-2 hover:bg-gray-200 dark:hover:bg-gray-900"; if (index === active) { className = "flex flex-row gap-x-2 px-4 py-2 bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-900"; } count = count + 1; return <li className={className} key={data[valueKey]} data-key={index} onClick={onClick}>{data[labelKey]}</li> } }) } </ul> ); } else { dataListComponent = ( <ul className="absolute inset-x-0 bg-white dark:bg-gray-700 rounded-b border border-gray-200 dark:border-gray-600 text-gray-900 text-sm font-medium dark:text-white cursor-pointer"> <li className="px-4 py-2 text-center"><em>{noMatchesText}</em></li> </ul> ); } } return ( <div className="relative mb-6"> <Input type="text" onChange={onChange} onKeyDown={onKeyDown} value={dataInput} placeholder={placeholder} autoComplete="no" wrapperClassName="" {...props} /> {dataListComponent} </div> ); } DataSuggest.propTypes = { data: PropTypes.instanceOf(Array), select: PropTypes.instanceOf(Function), valueKey: PropTypes.string, labelKey: PropTypes.string, placeholder: PropTypes.string, noMatchesText: PropTypes.string, limit: PropTypes.number, }; DataSuggest.defaultProps = { data: [], select: function () { return true; }, valueKey: 'value', labelKey: 'label', placeholder: 'Start typing to search...', noMatchesText: 'No matching results found!', limit: 0 }; export default DataSuggest; |