Aucune description

index.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { useState } from "react";
  2. import { Input } from "antd";
  3. import classnames from "classnames";
  4. import { formatMoney } from "../../Utils/utils";
  5. import styles from "./PriceOptions.less";
  6. const FormattedMessage = ({ id }: any) => id;
  7. type Props = {
  8. price: number;
  9. onPriceChange: (v: number) => void;
  10. size: "small" | "normal" | "large";
  11. focusScroll: boolean;
  12. withTitle: boolean;
  13. inputRef: React.RefObject<Input>;
  14. };
  15. const PriceOptions = ({
  16. price,
  17. onPriceChange,
  18. size = "normal",
  19. focusScroll = true,
  20. withTitle = true,
  21. inputRef
  22. }: Props) => {
  23. const defaultOptions = [100, 600, 800];
  24. // 控制是否为其他金额输入情况
  25. const [inputStatus, setInputStatus] = useState(false);
  26. return (
  27. <div
  28. className={classnames(styles.options, {
  29. [styles[size]]: true
  30. })}
  31. >
  32. {withTitle ? (
  33. <p>
  34. <FormattedMessage id="pay.price.select.text" />
  35. </p>
  36. ) : null}
  37. <div className={styles.infoItem} style={{ marginBottom: "-12px" }}>
  38. <span className={styles.priceBtn}>
  39. {defaultOptions.map(item => (
  40. <span
  41. className={classnames({
  42. [styles.priceItem]: true,
  43. [styles.active]: price === item && !inputStatus
  44. })}
  45. key={item}
  46. onClick={() => {
  47. onPriceChange(item);
  48. setInputStatus(false);
  49. }}
  50. >
  51. <FormattedMessage id={`${formatMoney(item/100, 0)}¥`} />
  52. </span>
  53. ))}
  54. </span>
  55. <Input
  56. ref={inputRef}
  57. onMouseEnter={() => {
  58. if (inputStatus && inputRef && inputRef.current) {
  59. inputRef.current.focus();
  60. inputRef.current.select();
  61. }
  62. }}
  63. suffix="¥"
  64. className={styles.priceInput}
  65. value={inputStatus ? price / 100 || "" : ""}
  66. placeholder="Others"
  67. onChange={e => {
  68. const n = +e.target.value;
  69. if (Number.isNaN(n)) {
  70. return;
  71. }
  72. // 测试暂时改成1分
  73. onPriceChange(n * 100);
  74. }}
  75. onClick={e => {
  76. onPriceChange(0);
  77. setInputStatus(true);
  78. }}
  79. onFocus={e => {
  80. if (focusScroll) {
  81. e.target.scrollIntoView();
  82. }
  83. }}
  84. />
  85. </div>
  86. </div>
  87. );
  88. };
  89. export default PriceOptions;