FormInput.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. dataDetectorTypes: "phoneNumber",
  64. maxLength: 6,
  65. keyboardType: "phone-pad",
  66. };
  67. }
  68. if (props.onChange) {
  69. _props = {
  70. ..._props,
  71. onChangeText: (nextValue) => props.onChange(nextValue),
  72. };
  73. }
  74. return _props;
  75. }
  76. const inputProps = getInputProps(props);
  77. // const myInput = () => {
  78. // if (inputProps != null) {
  79. // return;
  80. // }
  81. // };
  82. const Label = (props) => {
  83. return (
  84. <View
  85. style={[
  86. styles.label,
  87. props.type === "img" ? { alignSelf: "flex-start" } : {},
  88. ]}
  89. >
  90. <Text category='c1' style={{ textAlign: "right" }}>
  91. {props.label}
  92. </Text>
  93. </View>
  94. );
  95. };
  96. const ForwardIcon = (props) => <Icon {...props} name='arrow-ios-forward' />;
  97. const selectList = props.selectList ? props.selectList : [];
  98. const [bottomModalX, changeBottomModalx] = React.useState("");
  99. const [selectVal, setSelectVal] = React.useState("");
  100. const selectInfo = React.useMemo(() => {
  101. if (props.type === "select" && props.value && selectList.length > 0) {
  102. const childrens = [...flattenSelect(selectList, "children")];
  103. return (
  104. childrens.find((item) => {
  105. return item.id == props.value;
  106. }) || { name: " " }
  107. );
  108. } else {
  109. return { name: " " };
  110. }
  111. }, [props.value, props.type, selectList]);
  112. const [open, ChangeOpen] = React.useState(false);
  113. function submitEvent(val) {
  114. console.log(val);
  115. }
  116. const theme = useTheme();
  117. function getMain(type, props) {
  118. if (type == "select") {
  119. return (
  120. <>
  121. <SelectItem
  122. appearance='form'
  123. style={{ flex: 1 }}
  124. accessoryRight={ForwardIcon}
  125. title={selectInfo.name}
  126. onPress={() => {
  127. bottomModalX.open();
  128. }}
  129. />
  130. <BottomModal
  131. ref={(c) => {
  132. changeBottomModalx(c);
  133. }}
  134. title={props.selectTitle}
  135. titleStyle={styles.titleStyle}
  136. cancelable={true}
  137. leftLabelText={cancel}
  138. leftLabelTextStyle={styles.leftLabelTextStyle}
  139. rightLabelText={confirm}
  140. rightLabelTextStyle={styles.rightLabelTextStyle}
  141. rightCallback={() => {
  142. props.onChange(selectVal);
  143. }}
  144. >
  145. <Cascader
  146. style={{ height: 200, marginBottom: 50 }}
  147. data={selectList}
  148. fieldKeys={{
  149. labelKey: "name",
  150. idKey: "id",
  151. activeKey: "choose",
  152. }}
  153. onChange={(value, info) => {
  154. setSelectVal(value[0]);
  155. }}
  156. />
  157. </BottomModal>
  158. </>
  159. );
  160. } else if (type == "openTime") {
  161. return (
  162. <OpenTime
  163. open={open}
  164. submit={(start, end, week) => {
  165. ChangeOpen(false);
  166. props.onChange(
  167. week.join(","),
  168. moment(start, "HH:mm").format("HH:mm:ss"),
  169. moment(end, "HH:mm").format("HH:mm:ss")
  170. );
  171. console.log(
  172. moment(start, "HH:mm").format("HH:mm:ss"),
  173. moment(end, "HH:mm").format("HH:mm:ss"),
  174. week
  175. );
  176. }}
  177. cancelEvent={() => {
  178. ChangeOpen(false);
  179. }}
  180. openModal={() => {
  181. ChangeOpen(true);
  182. }}
  183. />
  184. );
  185. } else if (type == "url") {
  186. return (
  187. <SelectItem
  188. appearance='form'
  189. style={{ flex: 1 }}
  190. accessoryRight={ForwardIcon}
  191. title=' '
  192. onPress={props.changePath}
  193. />
  194. );
  195. } else if (type == "actionSheet") {
  196. return (
  197. <>
  198. <Actionsheet
  199. list={props.list}
  200. value={props.value}
  201. onChange={props.onChange}
  202. />
  203. </>
  204. );
  205. } else if (type == "img") {
  206. return (
  207. <UpLoadImage
  208. style={styles.upload}
  209. value={props.value}
  210. changeIcon={props.onChange}
  211. />
  212. );
  213. } else {
  214. return <Input {...inputProps} size='small' style={styles.input} />;
  215. }
  216. }
  217. return (
  218. <Layout
  219. level='1'
  220. style={[
  221. styles.inputContainer,
  222. { ...props.style },
  223. props.type === "img" ? { flexDirection: "column" } : {},
  224. ]}
  225. >
  226. <Label {...props} />
  227. {(!props.value || props.value == " ") && (
  228. <Text category='c1' style={styles.sub}>
  229. {props.sub}
  230. </Text>
  231. )}
  232. {getMain(props.type, props)}
  233. {props.type == "code" && (
  234. <Button
  235. appearance='ghost'
  236. size='tiny'
  237. style={{ paddingVertical: 8, marginLeft: 5 }}
  238. >
  239. {props.btnText}
  240. </Button>
  241. )}
  242. </Layout>
  243. );
  244. });
  245. function ChnageTime(sendNum, isSend, changeSend_Num) {
  246. let num = sendNum - 1;
  247. console.log(num);
  248. changeSend_Num(true, num);
  249. setTimeout(() => {
  250. ChnageTime(num, isSend, changeSend_Num);
  251. }, 1000);
  252. }
  253. const styles = StyleSheet.create({
  254. inputContainer: {
  255. flexDirection: "row",
  256. alignItems: "center",
  257. paddingVertical: 10,
  258. paddingHorizontal: 4,
  259. },
  260. input: {
  261. flex: 1,
  262. },
  263. label: {
  264. width: 90,
  265. marginRight: 19,
  266. flexShrink: 0,
  267. },
  268. right: {
  269. flexDirection: "row",
  270. },
  271. code: {
  272. paddingHorizontal: 5,
  273. marginLeft: 5,
  274. },
  275. selectContent: {
  276. backgroundColor: "#F0F0F0",
  277. },
  278. titleStyle: {
  279. fontSize: 15,
  280. },
  281. leftLabelTextStyle: {
  282. fontSize: 13,
  283. },
  284. rightLabelTextStyle: {
  285. fontSize: 13,
  286. },
  287. sub: {
  288. color: "#787878",
  289. position: "absolute",
  290. left: 90,
  291. zIndex: 2,
  292. },
  293. upload: {
  294. marginTop: 20,
  295. },
  296. });
  297. export default FormInput;