OpenTime.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. StyleSheet,
  5. TouchableOpacity,
  6. LayoutAnimation,
  7. Platform,
  8. UIManager,
  9. } from "react-native";
  10. import { range, convert2Digit } from "beeshell/dist/common/utils";
  11. import {
  12. CheckBox,
  13. Layout,
  14. Modal,
  15. Button,
  16. Card,
  17. Text,
  18. MenuItem,
  19. Icon,
  20. SelectItem,
  21. } from "@ui-kitten/components";
  22. import { Scrollpicker } from "beeshell";
  23. import { useModel } from "flooks";
  24. import moment from "moment";
  25. if (Platform.OS === "android") {
  26. if (UIManager.setLayoutAnimationEnabledExperimental) {
  27. UIManager.setLayoutAnimationEnabledExperimental(true);
  28. }
  29. }
  30. const ForwardIcon = (props) => <Icon {...props} name='arrow-ios-forward' />;
  31. const titleText = (props, title, value) => (
  32. <>
  33. <Text
  34. {...props}
  35. category='s1'
  36. style={{ whiteSpace: "nowrap", flex: 1, marginRight: 10 }}
  37. >
  38. {title}
  39. </Text>
  40. <Text category='c1' numberOfLines={1} ellipsizeMode='tail'>
  41. {value}
  42. </Text>
  43. </>
  44. );
  45. export default function OpenTime({
  46. open,
  47. submit,
  48. cancelEvent,
  49. startTime,
  50. endTime,
  51. week,
  52. openModal,
  53. defaultValue,
  54. }) {
  55. const {
  56. MON,
  57. TUE,
  58. WED,
  59. THU,
  60. FRI,
  61. SAT,
  62. SUN,
  63. every,
  64. confirm,
  65. cancel,
  66. start,
  67. end,
  68. hour,
  69. min,
  70. weekName,
  71. } = useModel("wordsModel");
  72. const weeks = [
  73. {
  74. key: "MON",
  75. label: MON,
  76. },
  77. {
  78. key: "TUE",
  79. label: TUE,
  80. },
  81. {
  82. key: "WED",
  83. label: WED,
  84. },
  85. {
  86. key: "THU",
  87. label: THU,
  88. },
  89. {
  90. key: "FRI",
  91. label: FRI,
  92. },
  93. {
  94. key: "SAT",
  95. label: SAT,
  96. },
  97. {
  98. key: "SUN",
  99. label: SUN,
  100. },
  101. ];
  102. const [checkList, changeChecked] = React.useState([]);
  103. const listItems = weeks.map((item, index) => (
  104. <CheckBox
  105. style={styles.checkBoxItem}
  106. key={index}
  107. checked={checkList.includes(item.key)}
  108. onChange={(checked) => {
  109. console.log(checked);
  110. let _checkList = [...checkList];
  111. if (checked) {
  112. _checkList.push(item.key);
  113. } else {
  114. _checkList.splice(_checkList.indexOf(item.key), 1);
  115. }
  116. changeChecked(_checkList);
  117. }}
  118. >
  119. {item.label}
  120. </CheckBox>
  121. ));
  122. const list = React.useMemo(() => {
  123. let hourSum = 24;
  124. let minuteSum = 60;
  125. let secondSum = 60;
  126. let hours = range(hourSum / 1).map((item) => {
  127. item = convert2Digit(item * 1);
  128. return {
  129. label: `${item} ${hour}`,
  130. value: item,
  131. };
  132. });
  133. let minutes = range(minuteSum / 10).map((item) => {
  134. item = convert2Digit(item * 10);
  135. return {
  136. label: `${item} ${min}`,
  137. value: item,
  138. };
  139. });
  140. return [hours, minutes];
  141. }, [open]);
  142. const [time1, setTime1] = React.useState([8, 0]);
  143. const [time2, setTime2] = React.useState([20, 0]);
  144. const [showTime1, changeShowTime1] = React.useState(true);
  145. const [showTime2, changeShowTime2] = React.useState(false);
  146. const [showWeek, changeShowWeek] = React.useState(false);
  147. const allChecked = React.useMemo(() => {
  148. if (weeks.length == checkList.length) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }, [weeks, checkList]);
  154. const indeterminate = React.useMemo(() => {
  155. if (!allChecked && checkList.length > 0) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }, [allChecked, checkList]);
  161. const canSubmit = React.useMemo(() => {
  162. if (time1.length == 2 && time2.length == 2 && checkList.length > 0) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }, [time1, time2, checkList]);
  168. const time1Str = React.useMemo(() => {
  169. if (time1.length == 2) {
  170. let str = time1[0] + ":" + time1[1] * 10;
  171. return moment(str, "H:m").format("HH:mm");
  172. } else {
  173. return "";
  174. }
  175. }, [time1]);
  176. const time2Str = React.useMemo(() => {
  177. if (time2.length == 2) {
  178. let str = time2[0] + ":" + time2[1] * 10;
  179. return moment(str, "H:m").format("HH:mm");
  180. } else {
  181. return "";
  182. }
  183. }, [time2]);
  184. const weekStr = React.useMemo(() => {
  185. if (allChecked) {
  186. return every;
  187. } else {
  188. let _weeks = weeks.filter((item) => {
  189. return checkList.includes(item.key);
  190. });
  191. return _weeks
  192. .map((item) => {
  193. return item.label;
  194. })
  195. .join(",");
  196. }
  197. }, []);
  198. React.useEffect(() => {
  199. if (defaultValue.length > 0) {
  200. setShowName(defaultValue);
  201. }
  202. }, [defaultValue]);
  203. function hideElseShow(key) {
  204. changeShowTime1(key === 1);
  205. changeShowTime2(key === 2);
  206. changeShowWeek(key === 3);
  207. LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  208. }
  209. const [showName, setShowName] = React.useState([" ", ""]);
  210. return (
  211. <>
  212. <SelectItem
  213. appearance='form'
  214. style={{ flex: 1 }}
  215. accessoryRight={ForwardIcon}
  216. title={(props) => titleText(props, showName[0], showName[1])}
  217. onPress={openModal}
  218. />
  219. <Modal
  220. visible={open && list.length > 0}
  221. backdropStyle={styles.backdrop}
  222. onBackdropPress={cancelEvent}
  223. >
  224. <Card appearance='modalCrad' style={{ width: 242 }}>
  225. <Layout>
  226. <MenuItem
  227. appearance='cardContent'
  228. title={(props) => titleText(props, start, time1Str)}
  229. accessoryRight={ForwardIcon}
  230. onPress={() => {
  231. hideElseShow(1);
  232. }}
  233. />
  234. </Layout>
  235. <Card
  236. appearance='modalCrad'
  237. style={[
  238. { width: 222 },
  239. showTime1 ? {} : styles.hideHeight,
  240. ]}
  241. >
  242. <Scrollpicker
  243. key='time1'
  244. list={list}
  245. proportion={[1, 1]}
  246. offsetCount={1}
  247. value={time1}
  248. onChange={(index, value) => {
  249. let _time1 = [...time1];
  250. _time1[index] = value;
  251. setTime1(_time1);
  252. }}
  253. />
  254. </Card>
  255. <Layout>
  256. <MenuItem
  257. appearance='cardContent'
  258. title={(props) => titleText(props, end, time2Str)}
  259. accessoryRight={ForwardIcon}
  260. onPress={() => {
  261. hideElseShow(2);
  262. }}
  263. />
  264. </Layout>
  265. <Card
  266. appearance='modalCrad'
  267. style={[
  268. { width: 222 },
  269. showTime2 ? {} : styles.hideHeight,
  270. ]}
  271. >
  272. <Scrollpicker
  273. key='time2'
  274. list={list}
  275. proportion={[1, 1]}
  276. offsetCount={1}
  277. value={time2}
  278. onChange={(index, value) => {
  279. let _time2 = [...time2];
  280. _time2[index] = value;
  281. setTime2(_time2);
  282. }}
  283. />
  284. </Card>
  285. <Layout>
  286. <MenuItem
  287. appearance='cardContent'
  288. title={(props) =>
  289. titleText(props, weekName, weekStr)
  290. }
  291. accessoryRight={ForwardIcon}
  292. onPress={() => {
  293. hideElseShow(3);
  294. }}
  295. />
  296. </Layout>
  297. <Card
  298. appearance='modalCrad'
  299. style={[showWeek ? {} : styles.hideHeight]}
  300. >
  301. <CheckBox
  302. style={styles.checkBoxAll}
  303. checked={allChecked}
  304. indeterminate={indeterminate}
  305. onChange={(checked) => {
  306. changeChecked(
  307. checked
  308. ? weeks.map((item) => {
  309. return item.key;
  310. })
  311. : []
  312. );
  313. }}
  314. >
  315. {every}
  316. </CheckBox>
  317. {listItems}
  318. </Card>
  319. <Layout style={styles.btnList}>
  320. <Button
  321. style={styles.btn}
  322. appearance='outline'
  323. status='info'
  324. onPress={cancelEvent}
  325. >
  326. {cancel}
  327. </Button>
  328. <Button
  329. style={[styles.btn, { marginLeft: 10 }]}
  330. disabled={!canSubmit}
  331. onPress={() => {
  332. submit(time1Str, time2Str, checkList);
  333. setShowName([
  334. time1Str + "~" + time2Str,
  335. weekStr,
  336. ]);
  337. }}
  338. >
  339. {confirm}
  340. </Button>
  341. </Layout>
  342. </Card>
  343. </Modal>
  344. </>
  345. );
  346. }
  347. const styles = StyleSheet.create({
  348. checkBoxAll: {
  349. paddingVertical: 10,
  350. },
  351. checkBoxItem: {
  352. paddingVertical: 2,
  353. paddingHorizontal: 10,
  354. },
  355. time: {
  356. // width: 100,
  357. // height: 300,
  358. },
  359. backdrop: {
  360. backgroundColor: "rgba(0, 0, 0, 0.5)",
  361. },
  362. btnList: {
  363. flexDirection: "row",
  364. justifyContent: "space-between",
  365. paddingTop: 15,
  366. },
  367. hideHeight: {
  368. height: 0,
  369. borderWidth: 0,
  370. },
  371. btn: {
  372. flex: 1,
  373. minWidth: 50,
  374. },
  375. });