FormInput.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import * as WebBrowser from "expo-web-browser";
  2. import * as React from "react";
  3. import { View, StyleSheet, TouchableWithoutFeedback } from "react-native";
  4. import {
  5. Layout,
  6. Input,
  7. Button,
  8. useTheme,
  9. Icon,
  10. Text,
  11. SelectItem,
  12. } from "@ui-kitten/components";
  13. import { Cascader, BottomModal } from "beeshell";
  14. import variables from "../constants/customTheme";
  15. import { useModel } from "flooks";
  16. import OpenTime from "./OpenTime";
  17. import UpLoadImage from "./UpLoadImage";
  18. import moment from "moment";
  19. import Actionsheet from "./Actionsheet";
  20. const AlertIcon = (props) => <Icon {...props} name='alert-circle-outline' />;
  21. function* flattenSelect(array, key) {
  22. for (const item of array) {
  23. let _array = key ? item[key] : item;
  24. if (Array.isArray(_array) && _array.length > 0) {
  25. yield* flattenSelect(_array, key);
  26. } else {
  27. yield item;
  28. }
  29. }
  30. }
  31. const FormInput = React.memo((props) => {
  32. const [secureTextEntry, setSecureTextEntry] = React.useState(true);
  33. const { cancel, confirm } = useModel("wordsModel");
  34. const toggleSecureEntry = () => {
  35. setSecureTextEntry(!secureTextEntry);
  36. };
  37. const renderIcon = (props) => (
  38. <TouchableWithoutFeedback onPress={toggleSecureEntry}>
  39. <Icon {...props} name={secureTextEntry ? "eye-off" : "eye"} />
  40. </TouchableWithoutFeedback>
  41. );
  42. function getInputProps(props) {
  43. let _props = {
  44. value: props.value || "",
  45. placeholder: props.placeholder,
  46. };
  47. if (props.type === "phone") {
  48. _props = {
  49. ..._props,
  50. dataDetectorTypes: "phoneNumber",
  51. maxLength: 11,
  52. keyboardType: "phone-pad",
  53. };
  54. } else if (props.type === "password") {
  55. _props = {
  56. ..._props,
  57. accessoryRight: (ImageProps) => renderIcon(ImageProps),
  58. secureTextEntry: secureTextEntry,
  59. };
  60. } else if (props.type == "code") {
  61. _props = {
  62. ..._props,
  63. maxLength: 6,
  64. keyboardType: "numeric",
  65. };
  66. } else if (props.type == "amount" || props.type == "money") {
  67. _props = {
  68. ..._props,
  69. keyboardType: "numeric",
  70. };
  71. }
  72. if (props.onChange) {
  73. _props = {
  74. ..._props,
  75. onChangeText: (nextValue) => props.onChange(nextValue),
  76. };
  77. }
  78. return _props;
  79. }
  80. const inputProps = getInputProps(props);
  81. // const myInput = () => {
  82. // if (inputProps != null) {
  83. // return;
  84. // }
  85. // };
  86. const Label = (props) => {
  87. return (
  88. <View
  89. style={[
  90. styles.label,
  91. props.type === "img" ? { alignSelf: "flex-start" } : {},
  92. props.labelStyle || {},
  93. ]}
  94. >
  95. <Text category='c1' style={{ textAlign: "right" }}>
  96. {props.label}
  97. </Text>
  98. </View>
  99. );
  100. };
  101. const ForwardIcon = (props) => <Icon {...props} name='arrow-ios-forward' />;
  102. const selectList = props.selectList ? props.selectList : [];
  103. const [bottomModalX, changeBottomModalx] = React.useState("");
  104. const [selectVal, setSelectVal] = React.useState("");
  105. const selectInfo = React.useMemo(() => {
  106. if (props.type === "select" && props.value && selectList.length > 0) {
  107. const childrens = [...flattenSelect(selectList, "children")];
  108. return (
  109. childrens.find((item) => {
  110. return item.id == props.value;
  111. }) || { name: " " }
  112. );
  113. } else {
  114. return { name: " " };
  115. }
  116. }, [props.value, props.type, selectList]);
  117. const [open, ChangeOpen] = React.useState(false);
  118. function submitEvent(val) {
  119. console.log(val);
  120. }
  121. const theme = useTheme();
  122. function getMain(type, props) {
  123. if (type == "select") {
  124. return (
  125. <>
  126. <SelectItem
  127. appearance='form'
  128. style={{ flex: 1 }}
  129. accessoryRight={ForwardIcon}
  130. title={selectInfo.name}
  131. onPress={() => {
  132. bottomModalX.open();
  133. }}
  134. />
  135. <BottomModal
  136. ref={(c) => {
  137. changeBottomModalx(c);
  138. }}
  139. title={props.selectTitle}
  140. titleStyle={styles.titleStyle}
  141. cancelable={true}
  142. leftLabelText={cancel}
  143. leftLabelTextStyle={styles.leftLabelTextStyle}
  144. rightLabelText={confirm}
  145. rightLabelTextStyle={styles.rightLabelTextStyle}
  146. rightCallback={() => {
  147. props.onChange(selectVal);
  148. }}
  149. >
  150. <Cascader
  151. style={{ height: 200, marginBottom: 50 }}
  152. data={selectList}
  153. fieldKeys={{
  154. labelKey: "name",
  155. idKey: "id",
  156. activeKey: "choose",
  157. }}
  158. onChange={(value, info) => {
  159. setSelectVal(value[0]);
  160. }}
  161. />
  162. </BottomModal>
  163. </>
  164. );
  165. } else if (type == "openTime") {
  166. return (
  167. <OpenTime
  168. open={open}
  169. submit={(start, end, week) => {
  170. ChangeOpen(false);
  171. props.onChange(
  172. week.join(","),
  173. moment(start, "HH:mm").format("HH:mm:ss"),
  174. moment(end, "HH:mm").format("HH:mm:ss")
  175. );
  176. console.log(
  177. moment(start, "HH:mm").format("HH:mm:ss"),
  178. moment(end, "HH:mm").format("HH:mm:ss"),
  179. week
  180. );
  181. }}
  182. cancelEvent={() => {
  183. ChangeOpen(false);
  184. }}
  185. openModal={() => {
  186. ChangeOpen(true);
  187. }}
  188. defaultValue={props.defaultValue}
  189. />
  190. );
  191. } else if (type == "url") {
  192. return (
  193. <SelectItem
  194. appearance='form'
  195. style={{ flex: 1 }}
  196. accessoryRight={ForwardIcon}
  197. title={props.value || " "}
  198. onPress={props.changePath}
  199. />
  200. );
  201. } else if (type == "actionSheet") {
  202. return (
  203. <>
  204. <Actionsheet
  205. list={props.list}
  206. value={props.value}
  207. onChange={props.onChange}
  208. />
  209. </>
  210. );
  211. } else if (type == "img") {
  212. return (
  213. <UpLoadImage
  214. style={styles.upload}
  215. value={props.value}
  216. changeIcon={props.onChange}
  217. />
  218. );
  219. } else {
  220. return <Input {...inputProps} size='small' style={styles.input} />;
  221. }
  222. }
  223. return (
  224. <Layout
  225. level='1'
  226. style={[
  227. styles.inputContainer,
  228. { ...props.style },
  229. props.type === "img" ? { flexDirection: "column" } : {},
  230. ]}
  231. >
  232. <Label {...props} />
  233. {(!props.value || props.value == " ") && (
  234. <Text category='c1' style={styles.sub}>
  235. {props.sub}
  236. </Text>
  237. )}
  238. {getMain(props.type, props)}
  239. {props.type == "code" && (
  240. <Button
  241. appearance='ghost'
  242. size='tiny'
  243. style={{ paddingVertical: 8, marginLeft: 5 }}
  244. >
  245. {props.btnText}
  246. </Button>
  247. )}
  248. </Layout>
  249. );
  250. });
  251. function ChnageTime(sendNum, isSend, changeSend_Num) {
  252. let num = sendNum - 1;
  253. console.log(num);
  254. changeSend_Num(true, num);
  255. setTimeout(() => {
  256. ChnageTime(num, isSend, changeSend_Num);
  257. }, 1000);
  258. }
  259. const styles = StyleSheet.create({
  260. inputContainer: {
  261. flexDirection: "row",
  262. alignItems: "center",
  263. paddingVertical: 10,
  264. paddingHorizontal: 4,
  265. },
  266. input: {
  267. flex: 1,
  268. },
  269. label: {
  270. width: 80,
  271. marginRight: 19,
  272. flexShrink: 0,
  273. },
  274. right: {
  275. flexDirection: "row",
  276. },
  277. code: {
  278. paddingHorizontal: 5,
  279. marginLeft: 5,
  280. },
  281. selectContent: {
  282. backgroundColor: "#F0F0F0",
  283. },
  284. titleStyle: {
  285. fontSize: 15,
  286. },
  287. leftLabelTextStyle: {
  288. fontSize: 13,
  289. },
  290. rightLabelTextStyle: {
  291. fontSize: 13,
  292. },
  293. sub: {
  294. color: "#787878",
  295. position: "absolute",
  296. left: 90,
  297. zIndex: 2,
  298. },
  299. upload: {
  300. marginTop: 20,
  301. width: 67,
  302. height: 67,
  303. },
  304. });
  305. export default FormInput;