https://stackblitz.com/edit/react-o1ra7c
用户搜索结果后,用户尝试使用键盘事件箭头需要向下移动。
<div>
<input onChange={handleSearchChange}/>
<div css={countryDataCss}>
{countryList?.map((country) => (
<div key={country.countryCode}>
<span css={countryCss}>{country.countryName}</span>
<span css={countryDialCodeCss}>
{country.countryDialNo}
</span>
</div>
))}
</div>
</div>
onkeydown
,对于 React 来说是 onKeyDown
,它提供了所需的行为,请检查下面的 stackblitz!
import React from 'react';
import './style.css';
import { useState } from 'react';
export default function App() {
const [searchText, setSearchText] = useState('');
const [selectedCountry, setSelectedCountry] = useState('SG');
const [activeIndex, setActiveIndex] = useState(0);
const [selectedDialCode, setSelectedDialCode] = useState('+65');
const countryCodeListResponse = [
{ countryCode: 'IND', countryDialNo: '+91', countryName: 'India' },
{ countryCode: 'SG', countryDialNo: '+65', countryName: 'Singpare' },
];
const [countryList, setCountryList] = useState(countryCodeListResponse);
function handleSearchChange(e) {
const searchText = e.target.value.toLowerCase();
if (countryCodeListResponse) {
const updatedList = countryCodeListResponse
.filter((el) => el.countryName.toLowerCase().includes(searchText))
.sort(
(a, b) =>
a.countryName.toLowerCase().indexOf(searchText) -
b.countryName.toLowerCase().indexOf(searchText)
);
setSearchText(searchText);
setCountryList(updatedList);
}
}
const onSelectCountry = (code, dialCode) => {
setSelectedCountry(code);
setSelectedDialCode(dialCode);
setSearchText('');
setCountryList(countryCodeListResponse);
};
const onKeyDown = (event) => {
console.log(event);
switch (event.keyCode) {
case 38: // arrow up
if (activeIndex > 0 && activeIndex <= countryList.length - 1) {
setActiveIndex((prev) => prev - 1);
}
break;
case 40: // arrow down
if (activeIndex >= 0 && activeIndex < countryList.length - 1) {
setActiveIndex((prev) => prev + 1);
}
break;
case 13:
const country = countryList[activeIndex];
onSelectCountry(country.countryCode, country.countryDialNo);
break;
default:
break;
}
};
return (
<div class="dropdown">
<div class="search">
<div>
<span>{selectedCountry}</span>
<span>{selectedDialCode}</span>
</div>
<input
class="search-input"
onKeyDown={(e) => onKeyDown(e)}
placeholder="Search"
value={searchText}
onChange={handleSearchChange}
/>
</div>
<div class="list">
{countryList?.map((country, index) => (
<div
onKeyDown={(e) => onKeyDown(e)}
className={index === activeIndex ? 'active' : ''}
tabIndex="0"
key={country.countryCode}
onClick={() =>
onSelectCountry(country.countryCode, country.countryDialNo)
}
>
<span>{country.countryName}</span>
<span>{country.countryDialNo}</span>
</div>
))}
</div>
</div>
);
}