FormInput.js 9.7 KB

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