Inputs.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, IconClose } 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. placeholder={placeholder ? placeholder : ''}
  50. // value={inputValue}
  51. password={isSecure ? true : false}
  52. type={openType == 'nickname' ? 'nickname' : isSecure ? 'safe-password' : 'text'}
  53. secureTextEntry={isSecure}
  54. onChangeText={(e)=>{
  55. onChange(e)
  56. }}
  57. placeholderTextColor="#ffffff99"
  58. placeholderStyle='color:#ffffffCC'
  59. focus={autoFocus}
  60. confirmType="done"
  61. selectionColor={global.fastColor ? global.fastColor : ColorType.fast}
  62. disabled={disabled ? disabled : false}
  63. onFocus={() => {
  64. setIsFocus(true)
  65. if (onFocus)
  66. onFocus()
  67. }}
  68. onBlur={() => {
  69. setIsFocus(false)
  70. if (onBlur)
  71. onBlur()
  72. }}
  73. >{inputValue}</TextInput>
  74. {
  75. inputValue.length > 0 && <View className="clear_bg" onClick={clear}>
  76. <IconClose width={20} height={20} color="#000"/>
  77. </View>
  78. }
  79. </View>
  80. )
  81. }
  82. return (
  83. <View className="input_bg">
  84. <Input className={isFocus ? "input input_focus" : "input"}
  85. // style={{backgroundColor:'red'}}
  86. placeholder={placeholder ? placeholder : ''}
  87. value={inputValue}
  88. password={isSecure ? true : false}
  89. type={openType == 'nickname' ? 'nickname' : isSecure ? 'safe-password' : 'text'}
  90. // type={openType?openType:'text'}
  91. onInput={handleInputChange}
  92. placeholderStyle='color:#ffffff66'
  93. // onConfirm={() => onConfirm()}
  94. focus={autoFocus}
  95. confirmType="done"
  96. selectionColor={global.fastColor ? global.fastColor : ColorType.fast}
  97. disabled={disabled ? disabled : false}
  98. onFocus={() => {
  99. setIsFocus(true)
  100. if (onFocus)
  101. onFocus()
  102. }}
  103. onBlur={() => {
  104. setIsFocus(false)
  105. if (onBlur)
  106. onBlur()
  107. }}
  108. />
  109. {
  110. inputValue.length > 0 && <View className="clear_bg" onClick={clear}>
  111. <IconClear />
  112. {/* <IconClose width={20} height={20} color="#000"/> */}
  113. {/* <IconClose width={20} height={20} color="#000"/> */}
  114. </View>
  115. }
  116. </View>
  117. )
  118. }