index.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. export default class Select extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. isOpen: false,
  12. value: "",
  13. options: [
  14. {
  15. label: 'patientSex',
  16. id: 'patientSex',
  17. value: 1,
  18. title: '性别',
  19. maxlength: 11
  20. },
  21. {
  22. label: 'patientSex',
  23. id: 'patientSex',
  24. value: 2,
  25. title: '性别',
  26. maxlength: 11
  27. },
  28. ]
  29. };
  30. this.toggleContainer = React.createRef();
  31. this.onClickHandler = this.onClickHandler.bind(this)
  32. this.onClickOutsideHandler = this.onClickOutsideHandler.bind(this)
  33. this.onChange = this.onChange.bind(this)
  34. }
  35. componentDidMount() {
  36. window.addEventListener("click", this.onClickOutsideHandler);
  37. this.setState({
  38. value: this.props.default,
  39. });
  40. }
  41. componentWillUnmount() {
  42. window.removeEventListener("click", this.onClickOutsideHandler);
  43. }
  44. componentWillReceiveProps(nextProps){
  45. if (nextProps.default.value !== this.state.value.value) {
  46. this.setState({
  47. value: nextProps.default
  48. })
  49. }
  50. }
  51. onClickHandler() {
  52. this.setState(currentState => ({
  53. isOpen: !currentState.isOpen
  54. }));
  55. };
  56. onClickOutsideHandler(event) {
  57. if (
  58. this.state.isOpen &&
  59. !this.toggleContainer.current.contains(event.target)
  60. ) {
  61. this.setState({ isOpen: false });
  62. }
  63. };
  64. onChange(e,val) {
  65. e.stopPropagation()
  66. this.setState({
  67. value: val,
  68. isOpen: false
  69. });
  70. this.props.onChange(val);
  71. store.dispatch({
  72. type: CLEARMENSTRUATIONTEXTDATA,
  73. });
  74. };
  75. render() {
  76. const { isOpen, value, options} = this.state;
  77. const { label, placeholder } = this.props;
  78. return (
  79. <div className={style.selectBox}>
  80. {label && <label className={style.label}>{label}:</label>}
  81. <div className={style.select} ref={this.toggleContainer}>
  82. <input
  83. className={[style.selfInput, isOpen ? style.selfInput : ''].join('')}
  84. readonly=""
  85. value={value.value=== 2? '女' : '男'}
  86. onClick={this.onClickHandler}
  87. placeholder={placeholder}
  88. />
  89. <div className={style.down} onClick={this.onClickHandler}>
  90. <img src={selectSex} alt=""/>
  91. </div>
  92. <div className={[isOpen ? '' : style.optionsHidden, isOpen ? style.options :''].join('')}>
  93. {options &&
  94. options.map((item) => {
  95. return (
  96. <div
  97. key={item.id}
  98. className={style.item}
  99. onClick={(e)=>this.onChange( e, item)}
  100. >
  101. {item.value === 2 ? '女' : '男'}
  102. </div>
  103. );
  104. })}
  105. </div>
  106. </div>
  107. </div>
  108. );
  109. }
  110. }