index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { View, Textarea, InputProps } from "@tarojs/components";
  2. import style from "./index.module.less";
  3. import { useState, useRef, useEffect } from "react";
  4. import { countCharacters, getStrByMaxLength } from "@/utils/index";
  5. interface Props {
  6. placeholder?: string;
  7. value: string;
  8. cursorSpacing?: number;
  9. maxlength?: number;
  10. autoHeight?: boolean;
  11. disabled?: boolean;
  12. confirmType?: keyof InputProps.ConfirmType;
  13. extra?: () => JSX.Element | JSX.Element[] | undefined;
  14. prefix?: () => JSX.Element | JSX.Element[] | undefined;
  15. extraStyle?: Record<string, string>;
  16. onInput?: (value: string) => void;
  17. onBlur?: (value: string) => void;
  18. bgColor?: string;
  19. autoFocus?: boolean;
  20. extraClass?: string;
  21. onConfirm?: (value: string) => void;
  22. }
  23. let isInbox = false;
  24. const index = ({
  25. value,
  26. bgColor,
  27. extraClass,
  28. extraStyle,
  29. disabled,
  30. confirmType,
  31. prefix,
  32. autoHeight,
  33. autoFocus = false,
  34. placeholder = "请输入...",
  35. onInput,
  36. onBlur,
  37. cursorSpacing,
  38. maxlength,
  39. extra,
  40. onConfirm,
  41. }: Props) => {
  42. const [focus, setFocus] = useState(false);
  43. const inputRef = useRef<HTMLInputElement>(null); // 创建一个 ref
  44. const handleFocus = () => {
  45. isInbox = false;
  46. setFocus(true);
  47. };
  48. const handleBlur = () => {
  49. // console.log("textarea blur");
  50. if (!isInbox) {
  51. setFocus(false);
  52. if (onBlur && inputRef.current) {
  53. onBlur(inputRef.current.value);
  54. }
  55. }
  56. };
  57. const handleInput = (value: string) => {
  58. const len = countCharacters(value);
  59. if (maxlength && len > maxlength) {
  60. const r = getStrByMaxLength(value, maxlength);
  61. onInput && onInput(r);
  62. return;
  63. }
  64. onInput && onInput(value);
  65. };
  66. return (
  67. <View
  68. className={`${
  69. focus ? style.inputContainerFocused : style.inputContainer
  70. } p-12`}
  71. style={bgColor ? { backgroundColor: bgColor } : {}}
  72. >
  73. {prefix && prefix()}
  74. <View className={style.textareaContainer}>
  75. <Textarea
  76. ref={inputRef}
  77. value={value}
  78. disabled={disabled}
  79. confirmType={confirmType}
  80. style={extraStyle}
  81. onInput={(e: any) => handleInput(e.target.value)}
  82. placeholder={placeholder}
  83. placeholderStyle="rgba(0,0,0,.25)"
  84. className={`${style.textInput} ${extraClass}`}
  85. onFocus={handleFocus}
  86. onBlur={handleBlur}
  87. autoHeight={autoHeight}
  88. autoFocus={autoFocus}
  89. cursorSpacing={cursorSpacing}
  90. maxlength={10000}
  91. onConfirm={(e: any) => {
  92. onConfirm && onConfirm(e.detail.value);
  93. }}
  94. />
  95. <View className={`${style.textareaButtons} justify-end gap-8`}>
  96. {extra && extra()}
  97. {/* <View className={`button-rounded-mini ${!value.length ? 'disabled' :''}`} onClick={handleClear}>清除</View> */}
  98. {maxlength && (
  99. <View className="text-gray-4">
  100. {maxlength}/{countCharacters(value)}
  101. </View>
  102. )}
  103. </View>
  104. </View>
  105. </View>
  106. );
  107. };
  108. export default index;