【发布时间】:2017-08-05 13:25:19
【问题描述】:
我有一个组件,当用户点击提交(或输入)一个带有文本输入的新问题时,用户输入一个答案并点击提交(这会重复直到最后一个问题得到回答)。
除了添加 ref 或 autofocus 都无法对焦之外,其他所有功能都有效。我不确定此时该做什么,并且无论我尝试什么似乎都无法让它发挥作用。
我能否获得一些帮助,当用户点击提交时,它会专注于下一个文本输入。我在我的代码中使用下面的样式组件,因此文本输入将是 inputStyle
class NameForm extends React.Component {
constructor(props) {
super(props);
this.focus = this.focus.bind(this);
this.state = {
value1: '',
value2: '',
value3: '',
value4: '',
value5: '',
value6: '',
value7: '',
newValue: '',
submitted: false,
input1: 0,
input2: 0,
input3: 0,
input4: 0,
input5: 0,
input6: 0,
input7: 0,
display: 'block',
currentStep: 1,
whitebox: 'block'
};
this.handleFirstChange = event => this.handleChange(event, 'value1');
this.handleSecondChange = event => this.handleChange(event, 'value2');
this.handleThirdChange = event => this.handleChange(event, 'value3');
this.handleFourthChange = event => this.handleChange(event, 'value4');
this.handleFifthChange = event => this.handleChange(event, 'value5');
this.handleSixthChange = event => this.handleChange(event, 'value6');
this.handleSeventhChange = event => this.handleChange(event, 'value7');
this.handleSubmit = event => this._handleSubmit(event);
}
handleChange(event, type) {
let newState = {};
newState[type] = event.target.value;
this.setState(newState);
}
_handleSubmit(event) {
event.preventDefault();
if (this.state.currentStep > 6) {
this.setState({ visible: !this.state.visible });
this.setState({ display: 'none' });
this.setState({ whitebox: 'none' });
} else {
this.setState({ currentStep: this.state.currentStep + 1 });
}
}
inputHolderStyle(style, step) {
const displayProp = step === this.state.currentStep ? 'block' : 'none';
return {
// background: `url(${style}) no-repeat center center`,
// backgroundSize: 'cover',
// border: 'white 1px solid',
// background: '#00B5DE',
display: displayProp
};
}
focus() {
// Explicitly focus the text input using the raw DOM API
this.textInput.focus();
}
render() {
const divStyle = {
marginTop: '50px',
color: 'white',
top: '25px',
position: 'absolute',
width: '320px',
textAlign: 'center',
border: 'white 1px solid',
padding: '1em',
borderRadius: '3px',
display: this.state.whitebox
};
let question = null;
const show = this.state.visible;
if (show) {
question = (
<div>
<Crawler
properName1={this.state.value1}
noun1={this.state.value2}
properName2={this.state.value3}
properName3={this.state.value4}
noun2={this.state.value5}
personsName1={this.state.value6}
noun3={this.state.value7}
/>
</div>
);
}
return (
<MainContainer>
<div style={divStyle}>
<form
style={{ display: this.state.display }}
onSubmit={this.handleSubmit}
>
<InputHolder style={this.inputHolderStyle(ml1, 1)}>
<InputQuestion>1. Enter A Proper Noun</InputQuestion>
<label>
<InputStyle
name="input1"
type="text"
value={this.state.value1}
placeholder="Proper Noun"
onChange={this.handleFirstChange}
ref1={input => {
this.textInput = input;
}}
/>
<GrammarNerd>
Hint: Use words like Rebel, Hell's Angels, Vegan
</GrammarNerd>
</label>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml2, 2)}>
<InputQuestion>2. Enter A Location</InputQuestion>
<label>
<InputStyle
name="input2"
type="text"
ref={input => {
this.textInput = input;
}}
value={this.state.value2}
placeholder="Noun"
onChange={this.handleSecondChange}
ref2={input => {
this.textInput = input;
}}
/>
<GrammarNerd>
Hint: Use a word such as Base, Bunker, Foxhole, Bedroom
</GrammarNerd>
</label>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml3, 3)}>
<InputQuestion>
Enter A Proper Noun that Describes Evil
</InputQuestion>
<label>
<InputStyle
name="input3"
type="text"
placeholder="Enter a Proper Noun"
value={this.state.value3}
onChange={this.handleThirdChange}
ref3={input => {
this.textInput = input;
}}
/>
</label>
<GrammarNerd>
Hint: Use words like Empire, Ottoman, Mongols
</GrammarNerd>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml3, 4)}>
<InputQuestion>Describe Something Menacing</InputQuestion>
<label>
<InputStyle
name="input4"
type="text"
placeholder="Enter a Proper Name"
value={this.state.value4}
onChange={this.handleFourthChange}
ref4="theDiv"
/>
<GrammarNerd>
Hint: Freeze Ray, Mother of All Bombs, Leftover Fruitcake
</GrammarNerd>
</label>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml3, 5)}>
<InputQuestion>Describe a fortified area</InputQuestion>
<label>
<InputStyle
name="input5"
type="text"
placeholder="Enter a Noun"
value={this.state.value5}
onChange={this.handleFifthChange}
ref5={input => {
this.textInput = input;
}}
/>
<GrammarNerd>
Hint: Castle, Bunker, Planet, Safe Space
</GrammarNerd>
</label>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml3, 6)}>
<InputQuestion>A Woman's Name</InputQuestion>
<label>
<InputStyle
name="input6"
type="text"
placeholder="A Woman's Name"
value={this.state.value6}
onChange={this.handleSixthChange}
ref6={input => {
this.textInput = input;
}}
/>
</label>
<GrammarNerd>
Hint: Astrid, Diana, Mononoke, Peach{' '}
</GrammarNerd>
</InputHolder>
<InputHolder style={this.inputHolderStyle(ml3, 7)}>
<InputQuestion>Describe a large area of mass</InputQuestion>
<label>
<InputStyle
name="input7"
type="text"
placeholder="Enter a Noun"
value={this.state.value7}
onChange={this.handleSeventhChange}
ref7={input => {
this.textInput = input;
}}
/>
</label>
<GrammarNerd>
Galaxy, Planet, Wal Mart
</GrammarNerd>
</InputHolder>
<InputHolderSubmit>
<SubmitButton onClick={this.focus} type="submit" value="Submit" />
</InputHolderSubmit>
</form>
</div>
<NextQuestion>
{question}
</NextQuestion>
</MainContainer>
);
}
}
export default NameForm;
我留下了一些我测试过的代码(参考和自动对焦)以及一些似乎没有破坏代码但也不起作用的功能。
感谢您的帮助
【问题讨论】:
-
尝试给每个输入一个不同的参考名称
-
我试了一下,没用,我应该在我的函数中修改一些东西吗?
-
我想你也想删除所有的自动对焦,当你调用提交时,使用参考
this.yourSpecificInput.focus();聚焦适当的输入 -
所以说从问题 4 到问题 5 应该是
this.inputHolderStyle(ml3, 5).focus()? -
我也是 React 的新手,所以为混乱的代码道歉
标签: javascript reactjs ecmascript-6