No Description

index.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from 'react';
  2. import { Popover } from 'antd';
  3. import { PopoverProps } from 'antd/lib/popover';
  4. import WantedPublishView from '../WantedPublishView';
  5. import popClose from '../assets/icon/close@2x.png';
  6. import styles from './WantedPublishPopover.less';
  7. export interface WantedPublishPopoverProp {
  8. popoverConfig?: PopoverProps;
  9. handleConfirm?: Function;
  10. }
  11. export interface WantedPublishPopoverState {
  12. visible: boolean;
  13. current_wanted: number|string|null;
  14. }
  15. export class WantedPublishPopover extends React.Component<WantedPublishPopoverProp, WantedPublishPopoverState> {
  16. constructor(props: WantedPublishPopoverProp) {
  17. super(props);
  18. this.state = {
  19. visible: false,
  20. current_wanted: null,
  21. }
  22. }
  23. handleClose = () => {
  24. const { handleConfirm } = this.props;
  25. if (handleConfirm) {
  26. this.handleUpdateCurrentWanted(this.state.current_wanted, (value: string) => {
  27. handleConfirm(Number(value).toFixed(2));
  28. });
  29. }
  30. this.setState({ visible: false });
  31. }
  32. clearCurrentWanted = (cb: Function) => {
  33. this.setState({
  34. current_wanted: null,
  35. }, () => cb())
  36. }
  37. handleUpdateCurrentWanted = (value: string|number|null, afterUpdate?: Function) => {
  38. if (!value) return;
  39. let result: string|number;
  40. result = value;
  41. if (parseInt(`${value}`, 10) === value) {
  42. // 无小数判断
  43. result = Number(value)
  44. }
  45. if (!result) return;
  46. if (result > 10000) result = 10000;
  47. if (result < 5) result = 5;
  48. this.setState({ current_wanted: result }, () => afterUpdate && afterUpdate(result));
  49. }
  50. renderInitView() {
  51. const { current_wanted } = this.state;
  52. return (
  53. <WantedPublishView
  54. type="pop"
  55. current_wanted={current_wanted}
  56. InputWantedValueChange={(v: string) => this.setState({ current_wanted: v })}
  57. InputWantedPressEnter={(v: string) => this.handleUpdateCurrentWanted(v, this.handleClose)}
  58. InputWantedOnBlur={(v: string) => this.handleUpdateCurrentWanted(v)}
  59. InputWantedClear={() => this.clearCurrentWanted(this.handleClose)}
  60. CloseFunction={this.handleClose}
  61. />
  62. )
  63. }
  64. render() {
  65. const { children, popoverConfig = {} } = this.props;
  66. return (
  67. <Popover
  68. placement="left"
  69. trigger="click"
  70. visible={this.state.visible}
  71. onVisibleChange={(value: boolean) => {
  72. if (!value) {
  73. this.handleClose();
  74. }
  75. this.setState({ visible: value });
  76. }}
  77. {...popoverConfig}
  78. content={
  79. <div className={styles.wantedWrapper}>
  80. <div role="button" tabIndex={1} className={styles.closeBtnWrapper}>
  81. <div className={styles.closeBtn} onClick={this.handleClose}>
  82. <img src={popClose} alt="close wanted button"/>
  83. </div>
  84. </div>
  85. {this.renderInitView()}
  86. </div>
  87. }
  88. >
  89. {children}
  90. </Popover>
  91. );
  92. }
  93. }
  94. export default WantedPublishPopover;