index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { useAjax } from "@/Hook/useAjax"
  2. import { txtLength } from "@/utils/utils";
  3. import { Input, List, Popover } from "antd";
  4. import React, { useEffect } from "react"
  5. import { useState } from "react";
  6. import './index.less'
  7. import { getTextApi } from "@/services/adqV3/global";
  8. interface Props {
  9. value?: any,
  10. onChange?: (value: any) => void;
  11. style?: React.CSSProperties;
  12. placeholder?: string;
  13. maxTextLength?: number;
  14. isShowAjax?: boolean
  15. }
  16. /**
  17. * 文案助手输入框
  18. * @param props
  19. * @returns
  20. */
  21. const TextAideInput: React.FC<Props> = (props) => {
  22. /************************/
  23. const { value, onChange, style, placeholder, maxTextLength = 10, isShowAjax = true } = props
  24. const [text, setText] = useState<any>(value)
  25. const [descriptionShow, setDescriptionshow] = useState(false)
  26. const getTextLsit = useAjax((params) => getTextApi(params))
  27. /************************/
  28. useEffect(() => {
  29. setText(value)
  30. }, [value])
  31. // 文案助手
  32. const textList = (keyword?: any) => {
  33. getTextLsit.run({ keyword, maxTextLength })
  34. }
  35. return <div style={{ display: 'inline-flex', alignItems: 'center', columnGap: 5 }}>
  36. <Popover
  37. placement="topLeft"
  38. overlayClassName="textAideInputPopover"
  39. style={{ minWidth: 600 }}
  40. visible={descriptionShow}
  41. content={<List
  42. loading={getTextLsit?.loading}
  43. size="small"
  44. style={{ maxHeight: 300, overflowX: 'auto', minWidth: 600 }}
  45. dataSource={getTextLsit?.data?.returnTexts}
  46. renderItem={(item: any) => <List.Item onClick={() => {
  47. setText(item.text)
  48. onChange && onChange(item.text)
  49. setTimeout(() => { setDescriptionshow(false) }, 50)
  50. }}><span >{item.text}{item.tag && <span className="crt">{'CTR 高'}</span>}</span></List.Item>}
  51. />}
  52. >
  53. <Input
  54. placeholder={placeholder}
  55. style={style}
  56. value={text}
  57. onFocus={() => {
  58. if (isShowAjax) {
  59. setDescriptionshow(true)
  60. textList(value)
  61. }
  62. }}
  63. onBlur={() => {
  64. setTimeout(() => { setDescriptionshow(false) }, 500)
  65. }}
  66. onChange={(e) => {
  67. let value = e.target.value
  68. setText(value)
  69. if (isShowAjax) {
  70. textList(value)
  71. }
  72. onChange && onChange(value)
  73. }}
  74. allowClear
  75. />
  76. </Popover>
  77. <span>{`${txtLength(text)}/${maxTextLength}`}</span>
  78. </div>
  79. }
  80. export default React.memo(TextAideInput)