reactjs - 在父组件的每个渲染上为输入设置自动对焦
问题描述
我有一个应用了样式的输入组件,称为 StyledInput,它位于 NumericInputComponent 中,该组件在 PriceComponent 中使用。这是一行的一部分。每当我在两行之间切换时,我都想专注于这个输入。自动对焦仅适用于第一次渲染,因此当我切换时,它不会再次自动对焦。我如何强制每次切换的焦点。
我尝试强制重新渲染价格组件,但这对焦点没有帮助。
在数字输入中
const StyledInput = styled('input')`
transition: all 150ms;
&:hover {
border: 1px solid ${Colors.brands['04']};
}
`;
<StyledInput
type='tel'
autoComplete='off'
autoFocus={this.props.autoFocus}
min={this.props.min}
max={this.props.max}
step={this.props.step}
value={this.props.value}
onFocus={this.props.onFocus}
disabled={this.props.disabled}
onClick={this.stopPropagation}
required={this.props.required}
onChange={this.handleInputChange}
className={this.props.className}
onBlur={this.props.onBlur}
ref={input => input && input.focus()}
/>
PriceComponenet 内部
const StyledNumericInput = styled(NumericInput)`
padding: 0; // remove mi-form-control padding
text-align: center;
&:required:focus {
background-image: none;
}
`;
<StyledNumericInput
autoFocus={this.shouldAutoFocus(this.props)}
className='mi-class'
value={this.state.value}
onChange={this.handleInputChange}
min={this.props.line ? -99.9 : 0.0}
id={'priceEdit' + this.props.index}
max={99.9}
step={0.1}
onFocus={Utilities.selectAllText}
required
/>
Say I have 3 lines, everytime I swtich between lines (which will have name, qty and price), it should always focus on price, even when the name and qty does not change.
解决方案
推荐阅读
- python - Python:QuickBooks API 集成
- python - 在 Python 中使用 for 循环将数字列表添加到空列表
- sql - 每当使用特定条件添加新记录时,脚本会根据特定条件在表中自动插入特定记录
- sqlite - 带有 Flutter 桌面 Windows 的 Sqlite?
- reactjs - 为什么这些功能不能从模式窗口工作?
- android - Firebase 推送通知,但未显示图像
- azure-ad-b2c - 是否可以使用声明转换从 stringCollection 中检索指定索引中的单个项目?
- c# - UniRx 如何通过将自身与另一个流组合来在流中发布新值
- python - 随机模块的随机random.pyi文件中没有任何函数定义的多个def?
- oracle - 在 Oracle 物化视图中用某个字符串替换空列