-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
战友
committed
Jun 17, 2017
1 parent
021e413
commit 6b5eed6
Showing
13 changed files
with
5,577 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
import * as React from 'react'; | ||
import './App.css'; | ||
import * as React from 'react' | ||
|
||
const logo = require('./logo.svg'); | ||
import { Password } from './components/password' | ||
|
||
class App extends React.Component<{}, null> { | ||
render() { | ||
return ( | ||
<div className="App"> | ||
<div className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<h2>Welcome to React</h2> | ||
</div> | ||
<p className="App-intro"> | ||
To get started, edit <code>src/App.tsx</code> and save to reload. | ||
</p> | ||
<div className='App'> | ||
<Password passwordLength={6} /> | ||
</div> | ||
); | ||
) | ||
} | ||
} | ||
|
||
export default App; | ||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './password.component' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.password-component | ||
|
||
.item | ||
width: 48px | ||
height: 48px | ||
border: 1px solid #666666 | ||
border-radius: 4px | ||
text-align center | ||
margin: 10px | ||
font-size: 30px | ||
color: transparent | ||
text-shadow: 0 0 0 black | ||
|
||
&::selection { | ||
background: transparent | ||
} | ||
|
||
&::-moz-selection { | ||
background: transparent | ||
} | ||
|
||
&::-webkit-selection { | ||
background: transparent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import * as React from 'react' | ||
import './password.component.styl' | ||
|
||
interface PasswordProps { | ||
passwordLength: number | ||
onConfirm?: (password: string) => void | ||
} | ||
|
||
interface PasswordState { | ||
valueArr: string[] | ||
} | ||
|
||
export class Password extends React.Component<PasswordProps, PasswordState> { | ||
private refArr: React.Ref<HTMLInputElement> | never[] = [] | ||
private index: number = 0 | ||
|
||
constructor(props: any) { | ||
super(props) | ||
this.state = { | ||
valueArr: [] | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.refArr[this.index].focus() | ||
} | ||
|
||
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const { passwordLength } = this.props | ||
const { valueArr } = this.state | ||
const isNumber = /^[0-9]*$/ | ||
const value = e.target.value | ||
if (isNumber.test(value) && parseInt(value, 10) >= 0) { | ||
valueArr[this.index] = value | ||
if (value.length === 1 && this.index < passwordLength - 1) { | ||
this.refArr[++this.index].focus() | ||
} | ||
} else { | ||
valueArr[this.index] = '' | ||
} | ||
this.setState({ | ||
...this.state, | ||
valueArr | ||
}) | ||
|
||
const password = this.checkPassword(valueArr) | ||
if (password) { | ||
const { onConfirm } = this.props | ||
if (onConfirm) { | ||
onConfirm(password) | ||
} | ||
} | ||
} | ||
|
||
checkPassword(valueArr: string[]) { | ||
const { passwordLength } = this.props | ||
let password = '' | ||
if (valueArr.length < passwordLength) { | ||
return false | ||
} | ||
valueArr.map((item) => { | ||
if (item !== '') { | ||
password += item | ||
} | ||
}) | ||
if (password.length < length) { | ||
return false | ||
} | ||
return password | ||
} | ||
|
||
handlePress = (target: any) => { | ||
const { passwordLength } = this.props | ||
switch (target.keyCode) { | ||
case 8: | ||
// 返回 | ||
const value = target.target.value | ||
if (value === '' && this.index > 0) { | ||
this.refArr[--this.index].focus() | ||
} | ||
break | ||
case 37: | ||
// 左移 | ||
if (this.index > 0) { | ||
this.refArr[--this.index].focus() | ||
} else { | ||
this.refArr[passwordLength - 1].focus() | ||
this.index = passwordLength - 1 | ||
} | ||
break | ||
case 39: | ||
// 右移 | ||
if (this.index < passwordLength - 1) { | ||
this.refArr[++this.index].focus() | ||
} else { | ||
this.refArr[0].focus() | ||
this.index = 0 | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
|
||
handelFocus = (i: number) => { | ||
this.index = i | ||
this.refArr[i].select() | ||
} | ||
|
||
setRef(index: number, ref: any) { | ||
this.refArr[index] = ref | ||
} | ||
|
||
render() { | ||
const { passwordLength } = this.props | ||
const { valueArr } = this.state | ||
const inputs: JSX.Element[] = [] | ||
for (let i = 0; i < passwordLength; i++) { | ||
inputs.push( | ||
<input | ||
key={i} | ||
ref={this.setRef.bind(this, i)} | ||
className='item' | ||
type='text' | ||
maxLength={1} | ||
onChange={this.handleChange} | ||
onKeyUp={this.handlePress} | ||
onFocus={this.handelFocus.bind(this, i)} | ||
value={valueArr[i]} | ||
/> | ||
) | ||
} | ||
return( | ||
<div className='password-component'> | ||
{inputs} | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import App from './App'; | ||
import registerServiceWorker from './registerServiceWorker'; | ||
import './index.css'; | ||
import * as React from 'react' | ||
import * as ReactDOM from 'react-dom' | ||
import App from './App' | ||
import registerServiceWorker from './registerServiceWorker' | ||
import './index.css' | ||
|
||
ReactDOM.render( | ||
<App />, | ||
document.getElementById('root') as HTMLElement | ||
); | ||
registerServiceWorker(); | ||
) | ||
registerServiceWorker() |
Oops, something went wrong.