No Description

index.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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<
  16. WantedPublishPopoverProp,
  17. WantedPublishPopoverState
  18. > {
  19. constructor(props: WantedPublishPopoverProp) {
  20. super(props);
  21. this.state = {
  22. visible: false,
  23. current_wanted: null
  24. };
  25. }
  26. handleClose = () => {
  27. const { handleConfirm } = this.props;
  28. if (handleConfirm) {
  29. this.handleUpdateCurrentWanted(
  30. this.state.current_wanted,
  31. (value: string) => {
  32. handleConfirm(Number(value).toFixed(2));
  33. }
  34. );
  35. }
  36. this.setState({ visible: false });
  37. };
  38. clearCurrentWanted = (cb: Function) => {
  39. this.setState(
  40. {
  41. current_wanted: null
  42. },
  43. () => cb()
  44. );
  45. };
  46. handleUpdateCurrentWanted = (
  47. value: string | number | null,
  48. afterUpdate?: Function
  49. ) => {
  50. if (!value) return;
  51. let result: string | number;
  52. result = value;
  53. if (parseInt(`${value}`, 10) === value) {
  54. // 无小数判断
  55. result = Number(value);
  56. }
  57. if (!result) return;
  58. if (result > 10000) result = 10000;
  59. if (result < 5) result = 5;
  60. this.setState(
  61. { current_wanted: result },
  62. () => afterUpdate && afterUpdate(result)
  63. );
  64. };
  65. renderInitView() {
  66. const { current_wanted } = this.state;
  67. return (
  68. <WantedPublishView
  69. type="pop"
  70. current_wanted={current_wanted}
  71. InputWantedValueChange={(v: string) =>
  72. this.setState({ current_wanted: v })
  73. }
  74. InputWantedPressEnter={(v: string) =>
  75. this.handleUpdateCurrentWanted(v, this.handleClose)
  76. }
  77. InputWantedOnBlur={(v: string) => this.handleUpdateCurrentWanted(v)}
  78. InputWantedClear={() => this.clearCurrentWanted(this.handleClose)}
  79. CloseFunction={this.handleClose}
  80. />
  81. );
  82. }
  83. render() {
  84. const { children, popoverConfig = {} } = this.props;
  85. return (
  86. <Popover
  87. placement="left"
  88. trigger="click"
  89. visible={this.state.visible}
  90. onVisibleChange={(value: boolean) => {
  91. if (!value) {
  92. this.handleClose();
  93. }
  94. this.setState({ visible: value });
  95. }}
  96. {...popoverConfig}
  97. content={
  98. <div className={styles.wantedWrapper}>
  99. <div role="button" tabIndex={1} className={styles.closeBtnWrapper}>
  100. <div className={styles.closeBtn} onClick={this.handleClose}>
  101. <img src={popClose} alt="close wanted button" />
  102. </div>
  103. </div>
  104. {this.renderInitView()}
  105. </div>
  106. }
  107. >
  108. {children}
  109. </Popover>
  110. );
  111. }
  112. }
  113. export default WantedPublishPopover;