Inputs.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Input, View } from "@tarojs/components";
  2. import './Inputs.scss'
  3. import { useEffect, useState } from "react";
  4. import { ColorType } from "@/context/themes/color";
  5. import { IconClear } from "../basic/Icons";
  6. interface CustomInputProps {
  7. placeholder?: string;
  8. value: string;
  9. onChange: (value: string) => void;
  10. onConfirm: Function,
  11. openType?: string;
  12. disabled?: boolean;
  13. isSecure?: boolean;
  14. autoFocus?: boolean;
  15. onBlur?: Function;
  16. onFocus?: Function;
  17. }
  18. export default function Component({
  19. placeholder,
  20. value,
  21. onChange,
  22. onConfirm,
  23. openType,
  24. disabled,
  25. isSecure,
  26. autoFocus,
  27. onBlur,
  28. onFocus
  29. }: CustomInputProps) {
  30. const [inputValue, setInputValue] = useState(value ? value : '');
  31. const [isFocus, setIsFocus] = useState(false)
  32. const handleInputChange = (e: any) => {
  33. const newValue = e.target.value;
  34. setInputValue(newValue);
  35. onChange(newValue);
  36. };
  37. useEffect(() => {
  38. setInputValue(value ? value : '')
  39. }, [value])
  40. function clear() {
  41. setInputValue('')
  42. onChange('')
  43. }
  44. if (process.env.TARO_ENV=='rn'){
  45. const TextInput = require('react-native').TextInput
  46. return (
  47. <View className="input_bg">
  48. <TextInput className={isFocus ? "input input_focus" : "input"}
  49. // style={{backgroundColor:'red'}}
  50. placeholder={placeholder ? placeholder : ''}
  51. value={inputValue}
  52. password={isSecure ? true : false}
  53. type={openType == 'nickname' ? 'nickname' : isSecure ? 'safe-password' : 'text'}
  54. // type={openType?openType:'text'}
  55. onInput={handleInputChange}
  56. placeholderStyle='color:#ffffff66'
  57. // onConfirm={() => onConfirm()}
  58. focus={autoFocus}
  59. confirmType="done"
  60. selectionColor={global.fastColor ? global.fastColor : ColorType.fast}
  61. disabled={disabled ? disabled : false}
  62. onFocus={() => {
  63. setIsFocus(true)
  64. if (onFocus)
  65. onFocus()
  66. }}
  67. onBlur={() => {
  68. setIsFocus(false)
  69. if (onBlur)
  70. onBlur()
  71. }}
  72. />
  73. {
  74. inputValue.length > 0 && <View className="clear_bg" onClick={clear}>
  75. <IconClear />
  76. </View>
  77. }
  78. </View>
  79. )
  80. }
  81. return (
  82. <View className="input_bg">
  83. <Input className={isFocus ? "input input_focus" : "input"}
  84. // style={{backgroundColor:'red'}}
  85. placeholder={placeholder ? placeholder : ''}
  86. value={inputValue}
  87. password={isSecure ? true : false}
  88. type={openType == 'nickname' ? 'nickname' : isSecure ? 'safe-password' : 'text'}
  89. // type={openType?openType:'text'}
  90. onInput={handleInputChange}
  91. placeholderStyle='color:#ffffff66'
  92. // onConfirm={() => onConfirm()}
  93. focus={autoFocus}
  94. confirmType="done"
  95. selectionColor={global.fastColor ? global.fastColor : ColorType.fast}
  96. disabled={disabled ? disabled : false}
  97. onFocus={() => {
  98. setIsFocus(true)
  99. if (onFocus)
  100. onFocus()
  101. }}
  102. onBlur={() => {
  103. setIsFocus(false)
  104. if (onBlur)
  105. onBlur()
  106. }}
  107. />
  108. {
  109. inputValue.length > 0 && <View className="clear_bg" onClick={clear}>
  110. <IconClear />
  111. </View>
  112. }
  113. </View>
  114. )
  115. }