מעולה, אתחיל בפתרון של התיבות עם הפוקוס המתחלף.
import React from 'react';
export default class Focus extends React.Component {
constructor(props){
super(props);
this.state = { focus: 0};
}
changeFocus = (e) =>{
e.persist();
this.setState( oldState => ({ focus : (oldState.focus+1) % React.Children.count(this.props.children)}));
this[[this.state.focus]].focus();
this[[this.state.focus]].value = e.key;
}
render() {
return (
<div>
{this.props.children.map((item, index) => <input
ref={(input) => { this[[index]] = input; }}
key={index}
maxLength="1"
type="text" tabIndex={index}
onKeyUp={(e) => this.changeFocus(e)}
style={{ height: 100, width: 100, borderStyle:'solid', display:'inline-block', margin:10}} /> )}
</div>
)
}
}
השימוש בפקד:
import React from 'react';
import ReactDOM from 'react-dom';
import Focus from './focus'
class App extends React.Component {
render() {
return (<Focus>
<div/>
<div/>
<div/>
<div/>
</Focus>);
}
}
ReactDOM.render(<App />, document.querySelector('main'));
וזאת על מנת שיהיה אפשר לייצר כל מספר של תיבות
