Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import {
  4. message,
  5. Button,
  6. Modal
  7. } from 'antd';
  8.  
  9. class Verify extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. modalvisible: false,
  14. };
  15. this.showModal = this.showModal.bind(this);
  16. this.handleOK = this.handleOK.bind(this);
  17. this.handleCancel = this.handleCancel.bind(this);
  18. this.clickFocus = this.clickFocus.bind(this);
  19. this.handleInputChange = this.handleInputChange.bind(this);
  20. this.submitTicketCode = this.submitTicketCode.bind(this);
  21. this.validateTicket = this.validateTicket.bind(this);
  22. }
  23. componentDidMount() {
  24. const self = this;
  25. ReactDOM.findDOMNode(this._showbtn).addEventListener('click', function(){
  26. setTimeout(() => {
  27. self._input.focus();
  28. }, 100);
  29. });
  30. }
  31. componentWillUnmount() {
  32. ReactDOM.findDOMNode(this._showbtn).removeEventListener('click', () => {
  33. // console.log('remove ..');
  34. });
  35. }
  36.  
  37. showModal() {
  38. this.setState({
  39. modalvisible: true,
  40. });
  41. }
  42. handleOK() {
  43. // console.log('Click OK Button, next step is confirm the code information .');
  44. this.setState({
  45. modalvisible: false,
  46. ticketvalue: '',
  47. });
  48. // 清除显示内容
  49. for (let i = 1; i <= 10; i++) {
  50. ReactDOM.findDOMNode(this[`_span${i}`]).innerHTML = ' ';
  51. }
  52. ReactDOM.findDOMNode(this._input).value = '';
  53. this._input.disabled = false;
  54. }
  55.  
  56. handleCancel() {
  57. this.setState({
  58. modalvisible: false,
  59. ticketvalue: '',
  60. });
  61. for (let i = 1; i <= 10; i++) {
  62. ReactDOM.findDOMNode(this[`_span${i}`]).innerHTML = ' ';
  63. }
  64. // 清楚input中已经输入的内容
  65. ReactDOM.findDOMNode(this._input).value = '';
  66. this._input.disabled = false;
  67. }
  68.  
  69. clickFocus() {
  70. ReactDOM
  71. .findDOMNode(this._input)
  72. .focus();
  73. }
  74. submitTicketCode() {
  75. // console.log('当length为10时,自动reqwest数据到后端进行验证....');
  76. this._input.disabled = true;
  77. // this.validateFetch({ ticket_no: `wb${this.state.ticketvalue}` });
  78. this.props.cb({ ticket_no: `wb${this.state.ticketvalue}` });
  79. this.setState({
  80. ticketvalue: '',
  81. });
  82. }
  83.  
  84. validateTicket(e, val) {
  85. if (/^[0-9]*$/.test(val)) {
  86. this.setState({
  87. ticketvalue: val,
  88. });
  89. for (let i = 1; i <= 10; i++) {
  90. if (val[i - 1]) {
  91. ReactDOM.findDOMNode(this[`_span${i}`]).innerHTML = val[i - 1];
  92. } else {
  93. ReactDOM.findDOMNode(this[`_span${i}`]).innerHTML = ' ';
  94. }
  95. }
  96. } else {
  97. // 将非法输入清除掉
  98. e.target.value = val.split('').splice(0, val.length - 1).join('');
  99. // console.log(val);
  100. message.error('要输入合法兑换码', 3);
  101. }
  102. }
  103.  
  104. handleInputChange(e) {
  105. const val = e.target.value;
  106. if (val.length < 10) {
  107. this.validateTicket(e, val);
  108. } else if (Number(val.length) === 10) {
  109. this.validateTicket(e, val);
  110. setTimeout(() => {
  111. this.submitTicketCode();
  112. }, 1000);
  113. ReactDOM.findDOMNode(this._input).value = '';
  114. setTimeout(() => {
  115. this.handleOK();
  116. // console.log(this);
  117. }, 1500);
  118. }
  119. }
  120. // validateFetch(params = {}) {
  121. // console.log('validateFetch ' + params.ticket_no);
  122. // this.setState({ loading: true });
  123. // reqwest({
  124. // url: '/business/virtual-tickets/checkout',
  125. // method: 'post',
  126. // data: params,
  127. // type: 'json',
  128. // withCredentials: true,
  129. // success: (result) => {
  130. // this.setState({ loading: false });
  131. // if (Number(result.code) === 1) {
  132. // message.success('校验成功');
  133. // this.setState({
  134. // ticketvalue: '',
  135. // });
  136. // } else {
  137. // message.error(result.msg);
  138. // }
  139. // },
  140. // error: (err) => {
  141. // this.setState({ loading: false });
  142. // switch (err.status) {
  143. // case 404:
  144. // message.error('获取数据失败,请联系官方人员!');
  145. // break;
  146. // default:
  147. // message.error('获取数据失败,请刷新重试!');
  148. // break;
  149. // }
  150. // }
  151. // });
  152. // }
  153.  
  154. render() {
  155. const { title, description, prefix, notice } = this.props;
  156. return (
  157. <div>
  158. <div style={{ height: 30, marginBottom: 20 }}>
  159. <Button
  160. type="primary"
  161. ref={(c) => this._showbtn = c}
  162. style={{ float: 'right' }}
  163. onClick={this.showModal}
  164. >
  165. 校验
  166. </Button>
  167. </div>
  168. <Modal
  169. title={title} visible={this.state.modalvisible}
  170. onOk={this.handleOK} onCancel={this.handleCancel}
  171. ref="modal"
  172. >
  173. <div
  174. style={{ position: 'relative', height: 80 }}
  175. >
  176. <p style={{ marginBottom: '5px' }}>{description}:</p>
  177. <input
  178. ref={(c) => this._input = c}
  179. style={{ width: 400, opacity: 0 }}
  180. onChange={this.handleInputChange}
  181. value={this.ticketvalue}
  182. />
  183. <p className="prefix">
  184. {prefix}
  185. </p>
  186. <div
  187. className="input"
  188. style={{ position: 'absolute', top: 35, right: 0 }}
  189. onClick={this.clickFocus}
  190. >
  191. <span ref={(c) => this._span1 = c}>&nbsp;</span>
  192. <span ref={(c) => this._span2 = c}>&nbsp;</span>
  193. <span ref={(c) => this._span3 = c}>&nbsp;</span>
  194. <span ref={(c) => this._span4 = c}>&nbsp;</span>
  195. <span ref={(c) => this._span5 = c}>&nbsp;</span>
  196. <span ref={(c) => this._span6 = c}>&nbsp;</span>
  197. <span ref={(c) => this._span7 = c}>&nbsp;</span>
  198. <span ref={(c) => this._span8 = c}>&nbsp;</span>
  199. <span ref={(c) => this._span9 = c}>&nbsp;</span>
  200. <span ref={(c) => this._span10 = c}>&nbsp;</span>
  201. </div>
  202. </div>
  203. <div>
  204. <p
  205. onClick={this.clickFocus}
  206. style={{ textAlign: 'right', color: 'red', marginTop: 10 }}
  207. >{notice}</p>
  208. </div>
  209. </Modal>
  210. </div>
  211. );
  212. }
  213. }
  214.  
  215. export default Verify;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement