我正在使用实质性的UI Select组件,并且试图在内部构建过滤器以仅显示与用户输入的内容匹配的项目。
我为正在开发的内容构建了一个最小的示例。
function App() { const [selectedOption, setSelectedOption] = React.useState(""); const [filterExpression, setFilterExpression] = React.useState(""); const onChangeSelection = ( event: React.ChangeEvent<{ name?: string | undefined; value: unknown }>, child: React.ReactNode ) => { const value = event.target.value.toString(); setSelectedOption(value); }; const onChangeExpression = ( event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement> ) => { const value = event.target.value.toString(); console.log(`value:`, value); setFilterExpression(value); }; const stopImmediatePropagation = (e: any) => { e.stopPropagation(); e.preventDefault(); }; return ( <div className="App"> <Select onChange={onChangeSelection} value={selectedOption}> <MenuItem dense divider value={""} onClickCapture={stopImmediatePropagation} > <TextField value={filterExpression} onChange={onChangeExpression} /> </MenuItem> <MenuItem dense key={"One"} value={"One"}> One </MenuItem> <MenuItem dense key={"Two"} value={"Two"}> Two </MenuItem> <MenuItem dense key={"Three"} value={"Three"}> Three </MenuItem> </Select> </div> ); }
现在我的问题是,默认情况下,选择组件允许用户按下任何字母,并且如果存在第一个字母与用户输入匹配的选项,它将选择该选项。
所以,如果我有3个选项(One,Two和Three)和用户类型O选择部件将选择One选项,我的文本字段不会改变的价值。但是,如果用户键入,F则文本字段将更新为F。
One
Two
Three
O
F
我想禁用此行为,所以,我一直在努力阻止类似事件的传播onKeyUpCapture,onChangeCapture,onKeyDownCapture但我没有”能避免这种情况。
onKeyUpCapture
onChangeCapture
onKeyDownCapture
您有任何解决建议吗?您可以在此处查看功能示例:
文本焦点导航功能在的onKeyDown中实现MenuList(我大约在6个月前实现了)。停止该事件在上的传播MenuItem(也TextField可以阻止该事件在上的传播)防止事件到达MenuList。
onKeyDown
MenuList
MenuItem
TextField
import * as React from "react"; import { render } from "react-dom"; import TextField from "@material-ui/core/TextField"; import Select from "@material-ui/core/Select"; import MenuItem from "@material-ui/core/MenuItem"; import "./styles.css"; function App() { const [selectedOption, setSelectedOption] = React.useState(""); const [filterExpression, setFilterExpression] = React.useState(""); const onChangeSelection = ( event: React.ChangeEvent<{ name?: string | undefined; value: unknown }>, child: React.ReactNode ) => { const value = event.target.value.toString(); setSelectedOption(value); }; const onChangeExpression = ( event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement> ) => { const value = event.target.value.toString(); console.log(`value:`, value); setFilterExpression(value); }; const stopImmediatePropagation = (e: any) => { e.stopPropagation(); e.preventDefault(); }; return ( <div className="App"> <Select onChange={onChangeSelection} value={selectedOption}> <MenuItem dense divider value={""} onClickCapture={stopImmediatePropagation} onKeyDown={e => e.stopPropagation()} > <TextField value={filterExpression} onChange={onChangeExpression} /> </MenuItem> <MenuItem dense key={"One"} value={"One"}> One </MenuItem> <MenuItem dense key={"Two"} value={"Two"}> Two </MenuItem> <MenuItem dense key={"Three"} value={"Three"}> Three </MenuItem> </Select> </div> ); } const rootElement = document.getElementById("root"); render(<App />, rootElement);