用鼠标点击一切正常,但我希望它能与键盘配合使用
当我坐在输入组件中按下键时,我希望输入没有聚焦/模糊。
这是我的输入组件
import { Input } from 'semantic-ui-react';
<Input
focus={this.state.inputFocus}
ref='search'
placeholder='Search...' />
在进入输入时,使用按键,此代码有助于集中输入
this.refs.search.focus();
this.setState({ inputFocus: true });
但是当从输入框离开时,闪烁的键指示灯不会被移除,并且该框仍然显示为聚焦,
尝试的代码不起作用
this.refs.search.blur(); // generates error
this.setState({ inputFocus: false }); //changes state var but ui isn't changed
模糊错误
如果你真的需要手动控制模糊/焦点,那么你可以听onKeyDown
事件并检查向下箭头代码。
满足条件时,您可以在.blur()
上触发event.target
事件或根据需要设置状态:
shouldBlur = (e) => {
if (e.keyCode === 40) {
e.target.blur();
// or set the state as you wish
}
}
并像这样使用这个处理程序:
<Input value={value} onKeyDown={this.shouldBlur} onChange={this.handleChange} />
这是一个运行的例子:
const { Input, Ref, Button } = semanticUIReact;
class App extends React.Component {
state = { value: 'initial value' }
handleChange = (e, { value }) => this.setState(prev => ({ value }));
focus = () => {
this.inputRef.focus();
}
shouldBlur = (e) => {
if (e.keyCode === 40) {
e.target.blur();
// or set the state as you wish
}
}
render() {
const { value } = this.state;
return (
<div >
<div>click the down arrow for blur</div>
<hr/>
<Input
value={value}
onKeyDown={this.shouldBlur}
onChange={this.handleChange}
ref={ref => this.inputRef = ref}
/>
<Button onClick={this.focus}>Focus</Button>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/semantic-ui-react.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css"/>
<div id="root"></div>