FormInput.js 8.8 KB

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