InputText.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use client";
  2. import React from "react";
  3. type InputType = 'text' | 'email';
  4. function InputText({
  5. type = 'text',
  6. placeholder,
  7. onChange,
  8. onBlur,
  9. name,
  10. error,
  11. ref
  12. }: {
  13. // 用于react-hook-form时,推荐使用useForm的 defaultValues 对整个表单设置默认值
  14. type?: InputType;
  15. placeholder: string;
  16. // 用于react-hook-form时,React 事件处理器类型定义中的“双变(bivariance)”特性 和返回值void的兼容性,使typescript没有提示类型错误
  17. onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  18. onBlur: (e: React.ChangeEvent<HTMLInputElement>) => void;
  19. name: string;
  20. error?: string;
  21. ref?: React.Ref<HTMLInputElement>;
  22. }) {
  23. return (
  24. <div className="w-full">
  25. <input className="ly-input w-full"
  26. type={type}
  27. placeholder={placeholder}
  28. onChange={onChange}
  29. onBlur={onBlur}
  30. name={name}
  31. ref={ref}
  32. />
  33. {error && <p className="text-red-500 text-ly-12">{error}</p>}
  34. </div>
  35. );
  36. }
  37. // 设置 displayName,便于调试
  38. InputText.displayName = 'InputText';
  39. export default InputText;