index.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { Component } from "react";
  2. // import cx from "classnames";
  3. import style from "./../index.less";
  4. import selectSex from '../../../common/images/selectSex.png'
  5. import { CLEARMENSTRUATIONTEXTDATA, } from '@store/types/menstruationHistory';
  6. import store from '@store';
  7. import { embedPush } from '../../../store/async-actions/pushMessage'
  8. export default class Select extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. isOpen: false,
  13. value: "",
  14. options: [
  15. {
  16. label: 'patientSex',
  17. id: 'patientSex',
  18. value: 1,
  19. title: '性别',
  20. maxlength: 11
  21. },
  22. {
  23. label: 'patientSex',
  24. id: 'patientSex',
  25. value: 2,
  26. title: '性别',
  27. maxlength: 11
  28. },
  29. ],
  30. isEnter: false
  31. };
  32. this.toggleContainer = React.createRef();
  33. this.onClickHandler = this.onClickHandler.bind(this)
  34. this.onClickOutsideHandler = this.onClickOutsideHandler.bind(this)
  35. this.onChange = this.onChange.bind(this)
  36. this.handleMouseEnter = this.handleMouseEnter.bind(this)
  37. this.handleMouseLeave = this.handleMouseLeave.bind(this)
  38. }
  39. componentDidMount() {
  40. window.addEventListener("click", this.onClickOutsideHandler);
  41. this.setState({
  42. value: this.props.default,
  43. });
  44. }
  45. componentWillUnmount() {
  46. window.removeEventListener("click", this.onClickOutsideHandler);
  47. }
  48. componentWillReceiveProps(nextProps){
  49. if (nextProps.default.value !== this.state.value.value) {
  50. this.setState({
  51. value: nextProps.default
  52. })
  53. }
  54. }
  55. onClickHandler() {
  56. this.setState(currentState => ({
  57. isOpen: !currentState.isOpen
  58. }));
  59. };
  60. onClickOutsideHandler(event) {
  61. if (
  62. this.state.isOpen &&
  63. !this.toggleContainer.current.contains(event.target)
  64. ) {
  65. this.setState({ isOpen: false });
  66. }
  67. };
  68. onChange(e,val) {
  69. const state = store.getState();
  70. e.stopPropagation()
  71. this.setState({
  72. value: val,
  73. isOpen: false
  74. });
  75. this.props.onChange(val);
  76. store.dispatch({
  77. type: CLEARMENSTRUATIONTEXTDATA,
  78. });
  79. store.dispatch(embedPush({
  80. action: "patientia",
  81. mode: 1
  82. }))
  83. };
  84. handleMouseEnter(){
  85. // console.log('鼠标进入');
  86. this.setState({
  87. isEnter: true
  88. })
  89. }
  90. handleMouseLeave(){
  91. // console.log('鼠标进入');
  92. this.setState({
  93. isEnter: false
  94. })
  95. }
  96. render() {
  97. const { isOpen, value, options} = this.state;
  98. const { label, placeholder } = this.props;
  99. return (
  100. <div className={style.selectBox}>
  101. {label && <label className={style.label}>{label}:</label>}
  102. <div className={style.select} ref={this.toggleContainer}>
  103. <input
  104. className={[style.selfInput, isOpen ? style.selfInput : ''].join('')}
  105. readonly=""
  106. value={value.value=== 2? '女' : '男'}
  107. onClick={this.onClickHandler}
  108. placeholder={placeholder}
  109. />
  110. <div className={this.state.isEnter ? style.downActive : style.down} onClick={this.onClickHandler} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  111. <img src={selectSex} alt=""/>
  112. </div>
  113. <div className={[isOpen ? '' : style.optionsHidden, isOpen ? style.options :''].join('')}>
  114. {options &&
  115. options.map((item) => {
  116. return (
  117. <div
  118. key={item.id}
  119. className={style.item}
  120. onClick={(e)=>this.onChange( e, item)}
  121. >
  122. {item.value === 2 ? '女' : '男'}
  123. </div>
  124. );
  125. })}
  126. </div>
  127. </div>
  128. </div>
  129. );
  130. }
  131. }