SearchBar.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import { Icon, Input, Button, Row, Col } from 'antd'
  8. import Agent from '../Agent'
  9. import styles from './SearchBar.less'
  10. export default class SearchBar extends React.Component {
  11. constructor (props) {
  12. super(props)
  13. this.state = {
  14. kw: undefined,
  15. current: 0,
  16. count: 0,
  17. cursor: null,
  18. has_previous: false,
  19. has_next: false,
  20. pos: []
  21. }
  22. }
  23. gotoPrevious () {
  24. Agent.emit('search:goto_previous')
  25. }
  26. gotoNext () {
  27. Agent.emit('search:goto_next')
  28. }
  29. doSearch () {
  30. clearTimeout(this._t)
  31. this._t = setTimeout(() => {
  32. Agent.emit('search:kw', this.state.kw)
  33. }, 300)
  34. }
  35. searchEnd () {
  36. Agent.emit('search:kw', '')
  37. Agent.emit('search:end')
  38. this.setState({kw: undefined})
  39. }
  40. componentDidMount () {
  41. this.el_ipt.focus()
  42. Agent.on('search:state', d => this.setState({...d}))
  43. Agent.on('search:start', () => {
  44. let ipt = this.el_ipt
  45. ipt && ipt.focus()
  46. })
  47. }
  48. render () {
  49. let {kw, count, has_previous, has_next} = this.state
  50. let {lang} = this.props
  51. return (
  52. <div className={styles.root}>
  53. <Row gutter={16}>
  54. <Col span={12}>
  55. <Input
  56. ref={c => this.el_ipt = c}
  57. value={kw}
  58. onChange={e => this.setState({kw: e.target.value}, () => this.doSearch())}
  59. placeholder={lang.search_placeholder}
  60. allowClear={true}
  61. //onPressEnter={this.gotoNext.bind(this)}
  62. prefix={<Icon type="search"/>}
  63. onKeyDown={e => {
  64. let ne = e.nativeEvent
  65. if (ne.keyCode === 27) {
  66. // esc
  67. this.searchEnd()
  68. } else if (ne.keyCode === 13 && ne.shiftKey) {
  69. this.gotoPrevious()
  70. } else if (ne.keyCode === 13) {
  71. this.gotoNext()
  72. }
  73. }}
  74. />
  75. </Col>
  76. <Col span={4}>
  77. <Button.Group style={{minWidth: '60px'}}>
  78. <Button disabled={!has_previous} onClick={this.gotoPrevious.bind(this)} icon="up"/>
  79. <Button disabled={!has_next} onClick={this.gotoNext.bind(this)} icon="down"/>
  80. </Button.Group>
  81. </Col>
  82. <Col className={styles.count_wrapper} span={7}>
  83. {kw ? (
  84. <span>{count} {lang.matches}</span>
  85. ) : null}
  86. </Col>
  87. <Col span={1}>
  88. <Icon
  89. className={styles.btn_close}
  90. type="close"
  91. title={lang.close}
  92. onClick={this.searchEnd.bind(this)}
  93. />
  94. </Col>
  95. </Row>
  96. </div>
  97. )
  98. }
  99. }