Açıklama Yok

index.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { useState } from "react";
  2. import classnames from "classnames";
  3. import { Input } from "antd";
  4. import styles from "./PriceOptions.less";
  5. interface Props {
  6. price: number;
  7. onPriceChange: (v: number) => void;
  8. size: "small" | "normal" | "large";
  9. focusScroll: boolean;
  10. withTitle: boolean;
  11. titleText?: string | JSX.Element[] | JSX.Element;
  12. inputPlaceholderText?: string;
  13. priceOptions?: Array<any>;
  14. priceRender?: Function;
  15. inputPriceRender?: Function;
  16. inputRef: React.RefObject<any>;
  17. }
  18. const PriceOptions = ({
  19. price,
  20. onPriceChange,
  21. size = "normal",
  22. focusScroll = true,
  23. withTitle = true,
  24. titleText = "Price",
  25. inputPlaceholderText = "Others",
  26. priceOptions = [100, 600, 800],
  27. priceRender = (i: any) => i / 100,
  28. inputPriceRender = (i: any) => (i ? i / 100 : ""),
  29. inputRef
  30. }: Props) => {
  31. const defaultOptions = priceOptions;
  32. // 控制是否为其他金额输入情况
  33. const [inputStatus, setInputStatus] = useState(false);
  34. return (
  35. <div className={classnames(styles.options)}>
  36. {withTitle ? (titleText || null) : null}
  37. <div className={styles.infoItem}>
  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. {priceRender(item)}
  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 ? inputPriceRender(price) : ""}
  66. placeholder={inputPlaceholderText}
  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. e.target.placeholder = "";
  81. setInputStatus(true);
  82. if (focusScroll) {
  83. e.target.scrollIntoView();
  84. }
  85. }}
  86. onBlur={e => {
  87. e.target.placeholder = inputPlaceholderText;
  88. if (price) {
  89. setInputStatus(true);
  90. } else {
  91. setInputStatus(false);
  92. }
  93. }}
  94. />
  95. </div>
  96. </div>
  97. );
  98. };
  99. export default PriceOptions;