diff --git a/src/App.js b/src/App.js index 9fe7672..a906732 100644 --- a/src/App.js +++ b/src/App.js @@ -1,8 +1,88 @@ import React, { Component } from "react"; +import "./input.css"; + +const CODE_LENGTH = new Array(6).fill(0); class App extends Component { + input = React.createRef(); + state = { + value: "", + focused: false, + }; + + handleClick = () => { + this.input.current.focus(); + }; + handleFocus = () => { + this.setState({ focused: true }); + }; + handleBlur = () => { + this.setState({ + focused: false, + }); + }; + handleKeyUp = e => { + if (e.key === "Backspace") { + this.setState(state => { + return { + value: state.value.slice(0, state.value.length - 1), + }; + }); + } + }; + handleChange = e => { + const value = e.target.value; + + this.setState(state => { + if (state.value.length >= CODE_LENGTH.length) return null; + return { + value: (state.value + value).slice(0, CODE_LENGTH.length), + }; + }); + }; render() { - return
; + const { value, focused } = this.state; + + const values = value.split(""); + + const selectedIndex = + values.length < CODE_LENGTH.length ? values.length : CODE_LENGTH.length - 1; + + const hideInput = !(values.length < CODE_LENGTH.length); + + return ( +