SudoPrompt.jsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import {Input} from 'antd'
  8. import Agent from '../Agent'
  9. import MyFrame from './MyFrame'
  10. import styles from './SudoPrompt.less'
  11. export default class SudoPrompt extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.onSuccess = null
  15. this.state = {
  16. show: false,
  17. pswd: ''
  18. }
  19. }
  20. componentDidMount () {
  21. Agent.on('sudo_prompt', (success) => {
  22. this.setState({show: true})
  23. this.onSuccess = success
  24. setTimeout(() => {
  25. let el = this.refs.body
  26. el && el.querySelector('input').focus()
  27. }, 100)
  28. })
  29. }
  30. onOK () {
  31. let {pswd} = this.state
  32. if (!pswd) {
  33. //let el = body
  34. //el && el.querySelector('input').focus()
  35. this.refs.pswd.focus()
  36. return
  37. }
  38. this.setState({
  39. show: false
  40. })
  41. Agent.emit('sudo_pswd', pswd)
  42. if (typeof this.onSuccess === 'function') {
  43. this.onSuccess(pswd)
  44. }
  45. this.onSuccess = null
  46. }
  47. onCancel () {
  48. Agent.emit('sudo_cancel')
  49. this.setState({
  50. show: false
  51. })
  52. this.onSuccess = null
  53. }
  54. body () {
  55. let {lang} = this.props
  56. return (
  57. <div ref="body">
  58. <div className="ln">
  59. <div className="title">{lang.sudo_pswd}</div>
  60. <div className="cnt">
  61. <Input
  62. type="password"
  63. ref="pswd"
  64. onKeyDown={e => (e.keyCode === 13 && this.onOK() || e.keyCode === 27 && this.onCancel())}
  65. onChange={e => this.setState({pswd: e.target.value})}
  66. />
  67. </div>
  68. </div>
  69. </div>
  70. )
  71. }
  72. render () {
  73. let {lang} = this.props
  74. return (
  75. <MyFrame
  76. show={this.state.show}
  77. title={lang.input_sudo_pswd}
  78. body={this.body()}
  79. onOK={() => this.onOK()}
  80. onCancel={() => this.onCancel()}
  81. lang={lang}
  82. width="400"
  83. />
  84. )
  85. }
  86. }